Skip to content

Instantly share code, notes, and snippets.

@rajatdiptabiswas
Created March 1, 2018 12:39
Show Gist options
  • Save rajatdiptabiswas/d37d46f4dd8c4c9e4df5f4b2f3bea6da to your computer and use it in GitHub Desktop.
Save rajatdiptabiswas/d37d46f4dd8c4c9e4df5f4b2f3bea6da to your computer and use it in GitHub Desktop.
A point class with x and y coordinates for 2D planes
import math
class Point(object):
'''Creates a point on a coordinate plane with values x and y'''
def __init__(self, x, y):
'''Defines the x and y coordinates in the variables'''
self.X = x
self.Y = y
def move(self, dx, dy):
'''Determines where x and y move'''
self.X = self.X + dx
self.Y = self.Y + dy
def __str__(self):
return "Point(%s,%s)"%(self.X, self.Y)
def getX(self):
return self.X
def getY(self):
return self.Y
def distance(self, other):
'''Calculates the distance between two points'''
dx = self.X - other.X
dy = self.Y - other.Y
return math.sqrt(dx**2 + dy**2)
def length(self):
'''Calculates the length of the point from the origin'''
return math.hypot(self.X, self.Y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment