MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / line

Function line

inprogress/rotatingcubebext.py:39–92  ·  view source on GitHub ↗

Returns a list of points in a line between the given points. Uses the Bresenham line algorithm. More info at: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm

(x1, y1, x2, y2)

Source from the content-addressed store, hash-verified

37
38
39def line(x1, y1, x2, y2):
40 """Returns a list of points in a line between the given points.
41
42 Uses the Bresenham line algorithm. More info at:
43 https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm"""
44 x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # TODO - Do we want this line?
45
46 isSteep = abs(y2-y1) > abs(x2-x1)
47 if isSteep:
48 x1, y1 = y1, x1
49 x2, y2 = y2, x2
50 isReversed = x1 > x2
51
52 if isReversed:
53 x1, x2 = x2, x1
54 y1, y2 = y2, y1
55
56 deltax = x2 - x1
57 deltay = abs(y2-y1)
58 error = int(deltax / 2)
59 y = y2
60 ystep = None
61 if y1 < y2:
62 ystep = 1
63 else:
64 ystep = -1
65 for x in range(x2, x1 - 1, -1):
66 if isSteep:
67 yield (y, x)
68 else:
69 yield (x, y)
70 error -= deltay
71 if error <= 0:
72 y -= ystep
73 error += deltax
74 else:
75 deltax = x2 - x1
76 deltay = abs(y2-y1)
77 error = int(deltax / 2)
78 y = y1
79 ystep = None
80 if y1 < y2:
81 ystep = 1
82 else:
83 ystep = -1
84 for x in range(x1, x2 + 1):
85 if isSteep:
86 yield (y, x)
87 else:
88 yield (x, y)
89 error -= deltay
90 if error < 0:
91 y += ystep
92 error += deltax
93
94
95def rotateXYZ(x, y, z, ax, ay, az):

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected