We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7a57e7b commit 815e8afCopy full SHA for 815e8af
newcodes/answers/q68.py
@@ -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