Numerical ray.
| 316 | |
| 317 | |
| 318 | class HalfLine(Line): |
| 319 | """Numerical ray.""" |
| 320 | |
| 321 | def __init__(self, tail: Point, head: Point): # pylint: disable=super-init-not-called |
| 322 | self.line = Line(tail, head) |
| 323 | self.coefficients = self.line.coefficients |
| 324 | self.tail = tail |
| 325 | self.head = head |
| 326 | |
| 327 | def intersect(self, obj: Union[Line, HalfLine, Circle, HoleCircle]) -> Point: |
| 328 | if isinstance(obj, (HalfLine, Line)): |
| 329 | return line_line_intersection(self.line, obj) |
| 330 | |
| 331 | exclude = [self.tail] |
| 332 | if isinstance(obj, HoleCircle): |
| 333 | exclude += [obj.hole] |
| 334 | |
| 335 | a, b = line_circle_intersection(self.line, obj) |
| 336 | if any([a.close(x) for x in exclude]): |
| 337 | return b |
| 338 | if any([b.close(x) for x in exclude]): |
| 339 | return a |
| 340 | |
| 341 | v = self.head - self.tail |
| 342 | va = a - self.tail |
| 343 | vb = b - self.tail |
| 344 | if v.dot(va) > 0: |
| 345 | return a |
| 346 | if v.dot(vb) > 0: |
| 347 | return b |
| 348 | raise InvalidLineIntersectError() |
| 349 | |
| 350 | def sample_within(self, points: list[Point], n: int = 5) -> list[Point]: |
| 351 | center = sum(points, Point(0.0, 0.0)) * (1.0 / len(points)) |
| 352 | radius = max([p.distance(center) for p in points]) |
| 353 | if close_enough(center.distance(self.line), radius): |
| 354 | center = center.foot(self) |
| 355 | a, b = line_circle_intersection(self, Circle(center.foot(self), radius)) |
| 356 | |
| 357 | if (a - self.tail).dot(self.head - self.tail) > 0: |
| 358 | a, b = self.tail, a |
| 359 | else: |
| 360 | a, b = self.tail, b # pylint: disable=self-assigning-variable |
| 361 | |
| 362 | result = None |
| 363 | best = -1.0 |
| 364 | for _ in range(n): |
| 365 | x = a + (b - a) * unif(0.0, 1.0) |
| 366 | mind = min([x.distance(p) for p in points]) |
| 367 | if mind > best: |
| 368 | best = mind |
| 369 | result = x |
| 370 | |
| 371 | return [result] |
| 372 | |
| 373 | |
| 374 | def _perpendicular_bisector(p1: Point, p2: Point) -> Line: |
no outgoing calls
no test coverage detected