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)")
94 """Check if line is valid."""
98 """Advance to the next point in the line."""
99 if self.
x1_x1_ > self.
x0_x0_:
100 if self.
x_x_ < self.
x1_x1_:
101 self.
x_x_ = round(self.
clampclamp(
103 self.
y_y_ = round(self.
m_m_ * self.
x_x_ + self.
b_b_, 5)
106 elif self.
x1_x1_ < self.
x0_x0_:
107 if self.
x_x_ > self.
x1_x1_:
108 self.
x_x_ = round(self.
clampclamp(
110 self.
y_y_ = round(self.
m_m_ * self.
x_x_ + self.
b_b_, 5)
114 if self.
y1_y1_ > self.
y0_y0_:
115 if self.
y_y_ < self.
y1_y1_:
116 self.
y_y_ = round(self.
clampclamp(
120 elif self.
y1_y1_ < self.
y0_y0_:
121 if self.
y_y_ > self.
y1_y1_:
122 self.
y_y_ = round(self.
clampclamp(
130 """Get the abscissa of the current point."""
134 """Get the ordinate of the current point."""
138 """Get the abscissa of the initial point."""
142 """Get the ordinate of the intial point."""
146 """Get the abscissa of the final point."""
150 """Get the ordinate of the final point."""
154 """Get the length of the line."""
155 return sqrt(pow(self.
x1_x1_ - self.
x0_x0_, 2) + pow(self.
y1_y1_ - self.
y0_y0_, 2))
159 Clamp n to be between min_n and max_n.
163 n (float): input value
164 min_n (float): minimum value
165 max_n (float): maximum value
169 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)