Add exercise 1
This commit is contained in:
parent
6bc82c7446
commit
f48f1bbb94
2 changed files with 31 additions and 0 deletions
BIN
__pycache__/test2.cpython-310-pytest-8.0.0.pyc
Normal file
BIN
__pycache__/test2.cpython-310-pytest-8.0.0.pyc
Normal file
Binary file not shown.
31
thinkcspy/Chapter-17-Exercises/distance.py
Normal file
31
thinkcspy/Chapter-17-Exercises/distance.py
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
import math
|
||||||
|
|
||||||
|
class Point:
|
||||||
|
""" Point class for representing and manipulating x,y coordinates. """
|
||||||
|
|
||||||
|
def __init__(self, initX, initY):
|
||||||
|
""" Create a new point at the given coordinates. """
|
||||||
|
self.x = initX
|
||||||
|
self.y = initY
|
||||||
|
|
||||||
|
def getX(self):
|
||||||
|
return self.x
|
||||||
|
|
||||||
|
def getY(self):
|
||||||
|
return self.y
|
||||||
|
|
||||||
|
def distanceFromOrigin(self):
|
||||||
|
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
|
||||||
|
|
||||||
|
def distanceFromPoint(self, pnt):
|
||||||
|
dx = self.x - pnt.getX()
|
||||||
|
dy = self.y - pnt.getY()
|
||||||
|
|
||||||
|
return math.sqrt(dx**2 +dy**2)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "x=" + str(self.x) + ", y=" + str(self.y)
|
||||||
|
|
||||||
|
p = Point(7, 6)
|
||||||
|
q = Point(10, 3)
|
||||||
|
print(p.distanceFromPoint(q))
|
||||||
Loading…
Reference in a new issue