-
-
Save MarkMoretto/967f2a321cbf9efce07f54f8a17c5182 to your computer and use it in GitHub Desktop.
2D Point class in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from math import sqrt | |
class Point: | |
def __init__(self, X, y): | |
self.X = X | |
self.y = y | |
def shift(self, X = 0, y = 0): | |
""" | |
Shift coordinates by X, y, or both. Default is zero shift to | |
allow one coordinate to be moved without impacting the other. | |
""" | |
self.x += x | |
self.y += y | |
def __repr__(self): | |
return f"{self.__class__.__name__}({self.X}, {self.y})" | |
p1 = Point(10, 3) | |
p2 = Point(1, 0) | |
def distance(a, b): | |
return ((a.X - b.X)**2 + (a.y-b.y)**2)**(1/2) | |
print(p1.X, p1.y, p2.X, p2.y) | |
print(distance(p1, p2)) | |
p2.shift(2,3) | |
print(p2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment