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) ->
None:
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 == y1
and x1 != x0:
88 self.
m_m_ = (y1 - y0) / (x1 - x0)
89 self.
b_b_ = y1 - (self.
m_m_ * x1)
93 'Line has zero length (All 4 points have same coordinates)'
97 """Check if line is valid."""
101 """Advance to the next point in the line."""
102 if self.
x1_x1_ > self.
x0_x0_:
103 if self.
x_x_ < self.
x1_x1_:
107 self.
y_y_ = round(self.
m_m_ * self.
x_x_ + self.
b_b_, 5)
110 elif self.
x1_x1_ < self.
x0_x0_:
111 if self.
x_x_ > self.
x1_x1_:
115 self.
y_y_ = round(self.
m_m_ * self.
x_x_ + self.
b_b_, 5)
119 if self.
y1_y1_ > self.
y0_y0_:
120 if self.
y_y_ < self.
y1_y1_:
126 elif self.
y1_y1_ < self.
y0_y0_:
127 if self.
y_y_ > self.
y1_y1_:
137 """Get the abscissa of the current point."""
141 """Get the ordinate of the current point."""
145 """Get the abscissa of the initial point."""
149 """Get the ordinate of the initial point."""
153 """Get the abscissa of the final point."""
157 """Get the ordinate of the final point."""
161 """Get the length of the line."""
162 return sqrt(pow(self.
x1_x1_ - self.
x0_x0_, 2) + pow(self.
y1_y1_ - self.
y0_y0_, 2))
164 def clamp(self, n: float, min_n: float, max_n: float) -> float:
166 Clamp n to be between min_n and max_n.
170 n (float): input value
171 min_n (float): minimum value
172 max_n (float): maximum value
176 n (float): input value clamped between given min and max
complex get_line_length(self)
float clamp(self, float n, float min_n, float max_n)
None __init__(self, float x0, float y0, float x1, float y1, float step_size=1.0)