18 This is a Python3 API for a line iterator.
20 It provides the ability to iterate
21 through the points of a line.
24 from cmath
import sqrt
31 LineIterator Python3 API for iterating along the points of a given line
36 step_size: float = 1.0):
38 Initialize the LineIterator.
42 x0 (float): Abscissa of the initial point
43 y0 (float): Ordinate of the initial point
44 x1 (float): Abscissa of the final point
45 y1 (float): Ordinate of the final point
46 step_size (float): Optional, step along the longer axis, defaults to 1
50 TypeError: When one (or more) of the inputs is not a number
51 ValueError: When step_size is not a positive number
54 if type(x0)
not in [int, float]:
55 raise TypeError(
'x0 must be a number (int or float)')
57 if type(y0)
not in [int, float]:
58 raise TypeError(
'y0 must be a number (int or float)')
60 if type(x1)
not in [int, float]:
61 raise TypeError(
'x1 must be a number (int or float)')
63 if type(y1)
not in [int, float]:
64 raise TypeError(
'y1 must be a number (int or float)')
66 if type(step_size)
not in [int, float]:
67 raise TypeError(
'step_size must be a number (int or float)')
70 raise ValueError(
'step_size must be a positive number')
80 if x1 == x0
and y1 == y0:
82 raise ValueError(
'Line has zero length (All 4 points have same coordinates)')
85 self.
steep_steep_ = abs(y1 - y0) > abs(x1 - x0)
87 self.
m_m_ = (x1 - x0) / (y1 - y0)
88 self.
b_b_ = x1 - (self.
m_m_ * y1)
90 self.
m_m_ = (y1 - y0) / (x1 - x0)
91 self.
b_b_ = y1 - (self.
m_m_ * x1)
94 """Check if line is valid."""
98 """Advance along the longer axis and interpolate the other coordinate."""
100 start, end, current = self.
y0_y0_, self.
y1_y1_, self.
y_y_
102 start, end, current = self.
x0_x0_, self.
x1_x1_, self.
x_x_
108 next_coordinate = self.
clampclamp(current + self.
step_size_step_size_, start, end)
113 next_coordinate = self.
clampclamp(current - self.
step_size_step_size_, end, start)
115 if next_coordinate != end:
116 rounded = round(next_coordinate, 5)
118 if (end > start
and rounded > current)
or (end < start
and rounded < current):
119 next_coordinate = self.
clampclamp(rounded, min(start, end), max(start, end))
121 if next_coordinate == end:
122 self.
x_x_, self.
y_y_ = self.
x1_x1_, self.
y1_y1_
126 self.
y_y_ = next_coordinate
127 if self.
x0_x0_ != self.
x1_x1_:
128 self.
x_x_ = round(self.
m_m_ * self.
y_y_ + self.
b_b_, 5)
130 self.
x_x_ = next_coordinate
131 if self.
y0_y0_ != self.
y1_y1_:
132 self.
y_y_ = round(self.
m_m_ * self.
x_x_ + self.
b_b_, 5)
135 """Get the abscissa of the current point."""
139 """Get the ordinate of the current point."""
143 """Get the abscissa of the initial point."""
147 """Get the ordinate of the initial point."""
151 """Get the abscissa of the final point."""
155 """Get the ordinate of the final point."""
159 """Get the length of the line."""
160 return sqrt(pow(self.
x1_x1_ - self.
x0_x0_, 2) + pow(self.
y1_y1_ - self.
y0_y0_, 2))
162 def clamp(self, n: float, min_n: float, max_n: float):
164 Clamp n to be between min_n and max_n.
168 n (float): input value
169 min_n (float): minimum value
170 max_n (float): maximum value
174 n (float): input value clamped between given min and max
def clamp(self, float n, float min_n, float max_n)
def get_line_length(self)
def __init__(self, float x0, float y0, float x1, float y1, float step_size=1.0)