Paths in python -
consider rectangular grid.
i want short , elegant way of generating straight path [x0,y0]
[x1,y1]
, either x0=x1
or y0 = y1
.
for example, on input [1,3], [3,3]
output [[1,3],[2,3],[3,3]
should generated. likewise if input [3,3], [1,3]
.
i have tried [[i,j] in range(self.origin[0],self.end[0]+1) j in range(self.origin[1], self.end[1]+1)]
, works case input ordered.
your question states solution x -> y
should same solution y -> x
, i.e. we're interested in defining points on path, not in ordering of points. if that's true, find out path has smaller x
(or y
) , designate origin.
origin = (3,3) dest = (1,3) origin, dest = sorted([origin, dest]) path = {(i,j) in range(origin[0], dest[0]+1) j in range(origin[1], dest[1]+1)} # note set comprehension, since doesn't make sense # use list of unique hashable items order irrelevant
of course, solves obstructionless 2-d pathfinding. if know 1 direction changing, in direction.
origin, dest = sorted((origin, dest)) if origin[0] == dest[0]: # y changing path = {(origin[0], j) j in range(origin[1], dest[1]+1)} else: # x changing path = {(i, origin[1]) in range(origin[0], dest[0]+1)}
Comments
Post a Comment