在while循环期间组合像素之间的距离

时间:2016-02-08 03:28:35

标签: python jython jes

我正在使用python中的uni分配,我需要测量给定点的像素之间的距离,如果我使用两个点我可以做得很好但是如果我使用两个以上我的代码只测量每个对< / p>

如何让它结合距离并仅在循环结束时显示它?

    def mapLevel2():
# City X and Y Values
  cityXvalue = [45,95,182,207,256,312,328,350,374,400]  
  CityYvalue = [310,147,84,201,337,375,434,348,335,265]

# Display Map
  map = makePicture("D:/Dropbox/Dropbox/Uni Files/Assesment to do/Introduction to Programming/Assignment 3/images/map.png")

# City Details
  show(map)
  numCity = requestInteger ("Enter the number of cities you wish to visit:")
  i = 0

# Request City Details
  while i < numCity:
    cityOne = requestInteger ("Enter the city you would like to visit")
    cityTwo = requestInteger ("Enter the city you would like to visit next")
    x1 = cityOne
    y1 = cityOne
    x2 = cityTwo 
    y2 = cityTwo 
    addLine(map, cityXvalue[x1], cityYvalue[y1], cityXvalue[x2], cityYvalue[y2])
    i = i + 1
    KMs = getDistanceBetween(cityXvalue[x1],cityYvalue[y1],cityXvalue[x2],cityYvalue[y2])
    addText(map,21,34,"%.9f" %KMs)
    repaint(map)

    i = 2
    while i < numCity:
      cityOne = cityTwo
      cityTwo = requestInteger ("Enter the city you would like to visit next")
      x1 = cityOne
      y1 = cityOne
      x2 = cityTwo 
      y2 = cityTwo 
      addLine(map, cityXvalue[x1], cityYvalue[y1], cityXvalue[x2], cityYvalue[y2])
      KMs = getDistanceBetween(cityXvalue[x1],cityYvalue[y1],cityXvalue[x2],cityYvalue[y2])
      addText(map,21,34,"%.9f" %KMs)
      repaint(map)
      i = i + 1 

# Output File
  #writePictureTo(map,"D:/Dropbox/Dropbox/Uni Files/Assesment to do/Introduction to Programming/Assignment 3/images/marked-map.png")

def getDistanceBetween(x1,y1,x2,y2):
  return 10*sqrt(pow(x1-x2,2)+pow(y1-y2,2))

1 个答案:

答案 0 :(得分:0)

第一个问题:你有两个while循环,你应该只有一个循环。让你的第一个城市在循环之外,然后循环中的每个后续城市,cityOne = cityTwo应该是循环内的最后一个东西。

第二个问题:这是错误的(或至少是误导性的):

x1 = cityOne   #
y1 = cityOne    # wrong!
x2 = cityTwo    #
y2 = cityTwo   # 
addLine(map, cityXvalue[x1], cityYvalue[y1], cityXvalue[x2], cityYvalue[y2])

应该是

x1 = cityXvalue[cityOne]
y1 = cityYvalue[cityOne]
x2 = cityXvalue[cityTwo]
y2 = cityYvalue[cityTwo]
addLine(map, x1, y1, x2, y2)

然后我们可以解决您的问题:在total_dist = 0循环之前需要while,在循环中需要total_dist += KMs,然后移动

addText(map,21,34,"%.9f" % total_dist)
repaint(map)
循环后

为了进行比较,这里有一个OO版本,可能更容易理解:

IN_MAP  = "D:/Dropbox/Dropbox/Uni Files/Assesment to do/Introduction to Programming/Assignment 3/images/map.png"
OUT_MAP = "D:/Dropbox/Dropbox/Uni Files/Assesment to do/Introduction to Programming/Assignment 3/images/marked-map.png"

class City:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def distance(self, other):
        return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5
    def half_way(self, other):
        return (self.x + other.x) / 2, (self.y + other.y) / 2

cities = [
    City( 45, 310), City( 95, 147), City(182,  84), City(207, 201),
    City(256, 337), City(312, 375), City(328, 434), City(350, 348),
    City(374, 335), City(400, 265)
]

def mapLevel2(cities=cities, in_map=IN_MAP, out_map=OUT_MAP):
    # show map
    map = makePicture(in_map)
    show(map)
    # get tour length
    num_stops = requestInteger("Enter the number of cities you wish to visit: ")
    # start touring!
    total_dist = 0.
    city_a = cities[requestInteger("Enter the city you are starting from: ")]
    for _ in range(1, num_stops):
        city_b = cities[requestInteger("Enter the city you would like to visit next: ")]
        # get distances
        dist = city_a.distance(city_b)
        total_dist += dist
        # show route on map
        addLine(map, city_a.x, city_a.y, city_b.x, city_b.y)
        hx, hy = city_a.half_way(city_b)
        addText(map, hx, hy, "{:3.1f}".format(dist))  # changed! place distance label at midpoint on route line
        repaint(map)
        # prepare for next leg of the tour
        city_a = city_b
    # show overall distance
    addText(map, 21, 34, "Total: {:3.1f} km".format(total_dist))
    repaint(map)
    # Output File
    if out_map:
        writePictureTo(map, out_map)