Skip to content

Instantly share code, notes, and snippets.

@shogonir
Created December 23, 2016 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shogonir/9f109826391f26adaf3b767dda06634e to your computer and use it in GitHub Desktop.
Save shogonir/9f109826391f26adaf3b767dda06634e to your computer and use it in GitHub Desktop.
def drawLine (point1, point2) :
points = []
try :
slope = 1.0 * (point2.y - point1.y) / (point2.x - point1.x)
if abs(slope) > 1.0 :
top, bottom = (point1, point2) if point1.y > point2.y else (point2, point1)
for y in range(bottom.y, top.y + 1) :
progress = 1.0 * (y - bottom.y) / (top.y - bottom.y)
x = int(bottom.x + progress * (top.x - bottom.x))
points.append(Vector2(x, y))
else :
right, left = (point1, point2) if point1.x > point2.x else (point2, point1)
for x in range(left.x, right.x + 1) :
progress = 1.0 * (x - left.x) / (right.x - left.x)
y = int(left.y + progress * (right.y - left.y))
points.append(Vector2(x, y))
except ZeroDivisionError :
pass
return points
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment