-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcartesian.py
More file actions
52 lines (40 loc) · 1.47 KB
/
cartesian.py
File metadata and controls
52 lines (40 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
""" Cartesian - a simple representation of a cartesian plane """
from collections import namedtuple
point_tuple = namedtuple("origin", ['x', 'y'])
origin = point_tuple(0, 0)
def sqrt(number):
""" Returns the square root of x without using the math module """
return pow(number, 0.5)
class Point(object):
""" A basic point """
def __init__(self, x=0, y=0):
""" Sets up the point """
self.x = x
self.y = y
def __repr__(self):
return "Point(" + str(self.x) + ", " + str(self.y) + ")"
def __str__(self):
return "Point at x=" + str(self.x) + " and y=" + str(self.y)
def __eq__(self, other):
if self.x == other.x:
if self.y == other.y:
return True
return False
def quadrant(self):
""" Determines the quadrant that the point is located in """
if self.x > 0:
if self.y > 0:
return 1
elif self.y < 0:
return 4
elif self.x < 0:
if self.y > 0:
return 2
elif self.y < 0:
return 3
def distance(self, point=origin):
""" Determines the distance between this point and another point """
return sqrt(pow((point.x - self.x), 2) + pow((point.y - self.y), 2))
def midpoint(self, point=origin):
""" Determines the midpoint between this point and another point """
return Point(((point.x+self.x)/2), ((point.y+self.y)/2))