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
34 def __init__(self, x0, y0, x1, y1, step_size=1.0):
36 Initialize the LineIterator.
40 x0 (float): Abscissa of the initial point
41 y0 (float): Ordinate of the initial point
42 x1 (float): Abscissa of the final point
43 y1 (float): Ordinate of the final point
44 step_size (float): Optional, Increments' resolution, defaults to 1
48 TypeError: When one (or more) of the inputs is not a number
49 ValueError: When step_size is not a positive number
52 if type(x0)
not in [int, float]:
53 raise TypeError(
'x0 must be a number (int or float)')
55 if type(y0)
not in [int, float]:
56 raise TypeError(
'y0 must be a number (int or float)')
58 if type(x1)
not in [int, float]:
59 raise TypeError(
'x1 must be a number (int or float)')
61 if type(y1)
not in [int, float]:
62 raise TypeError(
'y1 must be a number (int or float)')
64 if type(step_size)
not in [int, float]:
65 raise TypeError(
'step_size must be a number (int or float)')
68 raise ValueError(
'step_size must be a positive number')
78 if x1 != x0
and y1 != y0:
80 self.
m_m_ = (y1 - y0) / (x1 - x0)
81 self.
b_b_ = y1 - (self.
m_m_ * x1)
82 elif x1 == x0
and y1 != y0:
84 elif y1 == y1
and x1 != x0:
86 self.
m_m_ = (y1 - y0) / (x1 - x0)
87 self.
b_b_ = y1 - (self.
m_m_ * x1)
91 '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 intial 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))
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 __init__(self, x0, y0, x1, y1, step_size=1.0)
def get_line_length(self)
def clamp(self, n, min_n, max_n)