python - All integer points on a line segment -
i'm looking short smart way find integer points on line segment. 2 points integers, , line can @ angle of 0,45,90,135 etc. degrees.
here long code(so far 90 degree cases):
def getpoints(p1,p2) if p1[0] == p2[0]: if p1[1] < p2[1]: return [(p1[0],x) x in range(p1[1],p2[1])] else: return [(p1[0],x) x in range(p1[1],p2[1],-1)] if p2[1] == p2[1]: if p1[0] < p2[0]: return [(x,p1[1]) x in range(p1[0],p2[0])] else: return [(x,p1[1]) x in range(p1[0],p2[0],-1)]
edit: haven't mentioned clear enough, slope integer -1, 0 or 1, there 8 cases need checked.
reduce slope lowest terms (p/q), step 1 endpoint of line segment other in increments of p vertically , q horizontally. same code can work vertical line segments if reduce-to-lowest-terms code reduces 5/0 1/0.
Comments
Post a Comment