demos/point.py

21 lines
590 B
Python
Raw Permalink Normal View History

2023-12-12 16:27:18 +00:00
"""Demo implementation of a simple Point class."""
2023-12-12 15:15:53 +00:00
class Point:
2023-12-12 16:27:18 +00:00
"""A 2D point."""
def __init__(self, x: float, y: float): # Dunder init "Double underscore init"
2023-12-12 15:15:53 +00:00
self.x = x
self.y = y
def __repr__(self):
return f"Point(x={self.x}, y={self.y})"
2023-12-12 16:27:18 +00:00
def dist_from_origin(self) -> float:
"""Compute distance between self and origin."""
2023-12-12 15:15:53 +00:00
return (self.x**2 + self.y**2) ** 0.5
2023-12-12 16:27:18 +00:00
def dist(self, other) -> float:
2023-12-12 15:15:53 +00:00
"""Compute distance between two points."""
return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5