-
-
Save nickodell/6d3848148e2d8cae7b256d02e16beaa5 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
#!/usr/bin/env python3 | |
from math import sqrt | |
class Point: | |
def __init__(self,x_init,y_init): | |
self.x = x_init | |
self.y = y_init | |
def shift(self, x, y): | |
self.x += x | |
self.y += y | |
def __repr__(self): | |
return "".join(["Point(", str(self.x), ",", str(self.y), ")"]) | |
def distance(self, other): | |
a, b = self, other | |
return sqrt((a.x-b.x)**2+(a.y-b.y)**2) | |
if __name__ == "__main__": | |
p1 = Point(10, 3) | |
p2 = Point(1, 0) | |
print(p1.x, p1.y, p2.x, p2.y) | |
print(p1.distance(p2)) | |
print(p2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment