pythonjythonjes

combining distance between pixels during while loop


I'm working on a uni assignment in python, I need to measure the distance between pixels for given points, I can do this fine if I use two points however if I use more then two my code only measures each pair

how do I get it to combine the distance and only display it at the end of the loop?

    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))

Solution

  • First problem: you have two while loops where you should only have one. Get your first city outside the loop, then each subsequent city inside the loop, and cityOne = cityTwo should be the last thing inside the loop.

    Second problem: this is wrong (or at least misleading):

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

    It should instead be

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

    Then we can solve your question: you need total_dist = 0 before your while loop, total_dist += KMs inside the loop, and move

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

    after the loop.


    For comparison, here is an OO version which might be a bit easier to follow:

    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)