python - combining distance between pixels during while loop -
i'm working on uni assignment in python, need measure distance between pixels given points, can fine if use 2 points if use more 2 code measures each pair
how combine distance , display @ end of loop?
def maplevel2(): # city x , 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 do/introduction programming/assignment 3/images/map.png") # city details show(map) numcity = requestinteger ("enter number of cities wish visit:") = 0 # request city details while < numcity: cityone = requestinteger ("enter city visit") citytwo = requestinteger ("enter city visit next") x1 = cityone y1 = cityone x2 = citytwo y2 = citytwo addline(map, cityxvalue[x1], cityyvalue[y1], cityxvalue[x2], cityyvalue[y2]) = + 1 kms = getdistancebetween(cityxvalue[x1],cityyvalue[y1],cityxvalue[x2],cityyvalue[y2]) addtext(map,21,34,"%.9f" %kms) repaint(map) = 2 while < numcity: cityone = citytwo citytwo = requestinteger ("enter city 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) = + 1 # output file #writepictureto(map,"d:/dropbox/dropbox/uni files/assesment do/introduction programming/assignment 3/images/marked-map.png") def getdistancebetween(x1,y1,x2,y2): return 10*sqrt(pow(x1-x2,2)+pow(y1-y2,2))
first problem: have 2 while
loops should have one. first city outside loop, each subsequent city inside loop, , cityone = citytwo
should last thing inside loop.
second problem: wrong (or @ 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 can solve question: need total_dist = 0
before while
loop, total_dist += kms
inside loop, , move
addtext(map,21,34,"%.9f" % total_dist) repaint(map)
after loop.
for comparison, here oo version might bit easier follow:
in_map = "d:/dropbox/dropbox/uni files/assesment do/introduction programming/assignment 3/images/map.png" out_map = "d:/dropbox/dropbox/uni files/assesment do/introduction 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) # tour length num_stops = requestinteger("enter number of cities wish visit: ") # start touring! total_dist = 0. city_a = cities[requestinteger("enter city starting from: ")] _ in range(1, num_stops): city_b = cities[requestinteger("enter city visit next: ")] # 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 @ midpoint on route line repaint(map) # prepare next leg of 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)
Comments
Post a Comment