Skip to content

Commit 815e8af

Browse files
committed
two point distance
1 parent 7a57e7b commit 815e8af

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

newcodes/answers/q68.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
import math
5+
6+
class Point:
7+
def __init__(self, x=0.0, y=0.0):
8+
self.x = x
9+
self.y = y
10+
11+
def __str__(self):
12+
return "({:.2f}, {:.2f})".format(self.x, self.y)
13+
14+
def distance(self, p):
15+
delta_x = self.x - p.x
16+
delta_y = self.y - p.y
17+
return math.sqrt(delta_x ** 2 + delta_y ** 2)
18+
19+
def sum(self, p):
20+
x_new = self.x + p.x
21+
y_new = self.y + p.y
22+
return Point(x_new, y_new)
23+
24+
if __name__ == "__main__":
25+
p1 = Point(2.0, 4.0)
26+
p2 = Point(1.0, 5.0)
27+
d = p1.distance(p2)
28+
print("The distance is {0}".format(d))
29+
s = p1.sum(p2)
30+
print("The new point is:{0}".format(s))

0 commit comments

Comments
 (0)