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, Increments' resolution, 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 self.
m_m_ = (y1 - y0) / (x1 - x0)
83 self.
b_b_ = y1 - (self.
m_m_ * x1)
84 elif x1 == x0
and y1 != y0:
86 elif y1 == y0
and x1 != x0:
88 self.
m_m_ = (y1 - y0) / (x1 - x0)
89 self.
b_b_ = y1 - (self.
m_m_ * x1)
92 raise ValueError(
'Line has zero length (All 4 points have same coordinates)')
95 """Check if line is valid."""
99 """Advance to the next point in the line."""
100 if self.
x1_x1_ > self.
x0_x0_:
101 if self.
x_x_ < self.
x1_x1_:
105 self.
y_y_ = round(self.
m_m_ * self.
x_x_ + self.
b_b_, 5)
108 elif self.
x1_x1_ < self.
x0_x0_:
109 if self.
x_x_ > self.
x1_x1_:
113 self.
y_y_ = round(self.
m_m_ * self.
x_x_ + self.
b_b_, 5)
117 if self.
y1_y1_ > self.
y0_y0_:
118 if self.
y_y_ < self.
y1_y1_:
124 elif self.
y1_y1_ < self.
y0_y0_:
125 if self.
y_y_ > self.
y1_y1_:
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)