A point in 2D space. >>> Point(0, 0) Point(x=0.0, y=0.0) >>> Point(1.5, 2.5) Point(x=1.5, y=2.5)
| 26 | |
| 27 | @dataclass |
| 28 | class Point: |
| 29 | """ |
| 30 | A point in 2D space. |
| 31 | |
| 32 | >>> Point(0, 0) |
| 33 | Point(x=0.0, y=0.0) |
| 34 | >>> Point(1.5, 2.5) |
| 35 | Point(x=1.5, y=2.5) |
| 36 | """ |
| 37 | |
| 38 | x: float |
| 39 | y: float |
| 40 | |
| 41 | def __init__(self, x_coordinate: float, y_coordinate: float) -> None: |
| 42 | """ |
| 43 | Initialize a 2D point. |
| 44 | |
| 45 | Args: |
| 46 | x_coordinate: The x-coordinate (horizontal position) of the point |
| 47 | y_coordinate: The y-coordinate (vertical position) of the point |
| 48 | """ |
| 49 | self.x = float(x_coordinate) |
| 50 | self.y = float(y_coordinate) |
| 51 | |
| 52 | def __eq__(self, other: object) -> bool: |
| 53 | """ |
| 54 | Check if two points are equal. |
| 55 | |
| 56 | >>> Point(1, 2) == Point(1, 2) |
| 57 | True |
| 58 | >>> Point(1, 2) == Point(2, 1) |
| 59 | False |
| 60 | """ |
| 61 | if not isinstance(other, Point): |
| 62 | return NotImplemented |
| 63 | return self.x == other.x and self.y == other.y |
| 64 | |
| 65 | def __lt__(self, other: Point) -> bool: |
| 66 | """ |
| 67 | Compare two points for sorting (bottom-most, then left-most). |
| 68 | |
| 69 | >>> Point(1, 2) < Point(1, 3) |
| 70 | True |
| 71 | >>> Point(1, 2) < Point(2, 2) |
| 72 | True |
| 73 | >>> Point(2, 2) < Point(1, 2) |
| 74 | False |
| 75 | """ |
| 76 | if self.y == other.y: |
| 77 | return self.x < other.x |
| 78 | return self.y < other.y |
| 79 | |
| 80 | def euclidean_distance(self, other: Point) -> float: |
| 81 | """ |
| 82 | Calculate Euclidean distance between two points. |
| 83 | |
| 84 | >>> Point(0, 0).euclidean_distance(Point(3, 4)) |
| 85 | 5.0 |
no outgoing calls