Skip to content

Instantly share code, notes, and snippets.

@jeffallen
Created February 1, 2024 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffallen/be9afca969a1477a2387d337e508fddb to your computer and use it in GitHub Desktop.
Save jeffallen/be9afca969a1477a2387d337e508fddb to your computer and use it in GitHub Desktop.
class Point:
"""
A class representing a point in 2D space.
"""
def __init__(self, x, y):
"""
Initializes a new Point object.
Args:
x: The x-coordinate of the point.
y: The y-coordinate of the point.
"""
self.x = x
self.y = y
def distance_to(self, other):
"""
Calculates the distance between this point and another point.
Args:
other: The other point.
Returns:
The distance between the two points.
"""
dx = self.x - other.x
dy = self.y - other.y
return (dx**2 + dy**2)**0.5
def __str__(self):
"""
Returns a string representation of the point.
Returns:
A string representation of the point in the format "(x, y)".
"""
return f"({self.x}, {self.y})"
def main():
"""
The main function of the program.
"""
# Create two points
p1 = Point(1, 2)
p2 = Point(3, 4)
# Print the points
print(f"Point 1: {p1}")
print(f"Point 2: {p2}")
# Calculate the distance between the points
distance = p1.distance_to(p2)
print(f"Distance between points: {distance}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment