Numerical line.
| 145 | |
| 146 | |
| 147 | class Line: |
| 148 | """Numerical line.""" |
| 149 | |
| 150 | def __init__( |
| 151 | self, |
| 152 | p1: Point = None, |
| 153 | p2: Point = None, |
| 154 | coefficients: tuple[int, int, int] = None, |
| 155 | ): |
| 156 | if p1 is None and p2 is None and coefficients is None: |
| 157 | self.coefficients = None, None, None |
| 158 | return |
| 159 | |
| 160 | a, b, c = coefficients or ( |
| 161 | p1.y - p2.y, |
| 162 | p2.x - p1.x, |
| 163 | p1.x * p2.y - p2.x * p1.y, |
| 164 | ) |
| 165 | |
| 166 | # Make sure a is always positive (or always negative for that matter) |
| 167 | # With a == 0, Assuming a = +epsilon > 0 |
| 168 | # Then b such that ax + by = 0 with y>0 should be negative. |
| 169 | if a < 0.0 or a == 0.0 and b > 0.0: |
| 170 | a, b, c = -a, -b, -c |
| 171 | |
| 172 | self.coefficients = a, b, c |
| 173 | |
| 174 | def parallel_line(self, p: Point) -> Line: |
| 175 | a, b, _ = self.coefficients |
| 176 | return Line(coefficients=(a, b, -a * p.x - b * p.y)) # pylint: disable=invalid-unary-operand-type |
| 177 | |
| 178 | def perpendicular_line(self, p: Point) -> Line: |
| 179 | a, b, _ = self.coefficients |
| 180 | return Line(p, p + Point(a, b)) |
| 181 | |
| 182 | def greater_than(self, other: Line) -> bool: |
| 183 | a, b, _ = self.coefficients |
| 184 | x, y, _ = other.coefficients |
| 185 | # b/a > y/x |
| 186 | return b * x > a * y |
| 187 | |
| 188 | def __gt__(self, other: Line) -> bool: |
| 189 | return self.greater_than(other) |
| 190 | |
| 191 | def __lt__(self, other: Line) -> bool: |
| 192 | return other.greater_than(self) |
| 193 | |
| 194 | def same(self, other: Line) -> bool: |
| 195 | a, b, c = self.coefficients |
| 196 | x, y, z = other.coefficients |
| 197 | return close_enough(a * y, b * x) and close_enough(b * z, c * y) |
| 198 | |
| 199 | def equal(self, other: Line) -> bool: |
| 200 | a, b, _ = self.coefficients |
| 201 | x, y, _ = other.coefficients |
| 202 | # b/a == y/x |
| 203 | return b * x == a * y |
| 204 |
no outgoing calls
no test coverage detected