Define a two-dimensional directed line segment defined by two points. This class is mostly used as a way to cache information that is regularly required when working on geometrical problems. .. caution:: Lines should be used as if they were completely immutable to ensure
| 11 | from pygorithm.geometry import (vector2, axisall) |
| 12 | |
| 13 | class Line2(object): |
| 14 | """ |
| 15 | Define a two-dimensional directed line segment defined by two points. |
| 16 | This class is mostly used as a way to cache information that is |
| 17 | regularly required when working on geometrical problems. |
| 18 | |
| 19 | .. caution:: |
| 20 | |
| 21 | Lines should be used as if they were completely immutable to ensure |
| 22 | correctness. All attributes of Line2 can be reconstructed from the two |
| 23 | points, and thus cannot be changed on their own and must be recalculated |
| 24 | if there were any changes to `start` or `end`. |
| 25 | |
| 26 | .. tip:: |
| 27 | |
| 28 | To prevent unnecessary recalculations, many functions on lines accept an |
| 29 | 'offset' argument, which is used to perform calculations on lines that |
| 30 | are simply shifts of other lines. |
| 31 | |
| 32 | .. note:: |
| 33 | |
| 34 | The minimum x is guarranteed to be on either (or both) of |
| 35 | the start and end. However, minimum x and minimum y might not |
| 36 | come from the same point. The same is true for the maximum x |
| 37 | and maximum y. |
| 38 | |
| 39 | :ivar start: the start of this line |
| 40 | :vartype start: :class:`pygorithm.geometry.vector2.Vector2` |
| 41 | |
| 42 | :ivar end: the end of this line |
| 43 | :vartype end: :class:`pygorithm.geometry.vector2.Vector2` |
| 44 | """ |
| 45 | |
| 46 | def __init__(self, start, end): |
| 47 | """ |
| 48 | Create a new line from start to end. |
| 49 | |
| 50 | :param start: the start point |
| 51 | :type start: :class:`pygorithm.geometry.vector2.Vector2` |
| 52 | :param end: the end point |
| 53 | :type end: :class:`pygorithm.geometry.vector2.Vector2` |
| 54 | |
| 55 | :raises ValueError: if start and end are at the same point |
| 56 | """ |
| 57 | |
| 58 | if start.x == end.x and start.y == end.y: |
| 59 | raise ValueError('start and end are the same point') |
| 60 | |
| 61 | self.start = start |
| 62 | self.end = end |
| 63 | self._delta = None |
| 64 | self._axis = None |
| 65 | self._normal = None |
| 66 | self._magnitude_squared = None |
| 67 | self._magnitude = None |
| 68 | self._min_x = None |
| 69 | self._min_y = None |
| 70 | self._max_x = None |