Exercises¶
Add a
distanceFromPoint
method that works similar todistanceFromOrigin
except that it takes aPoint
as a parameter and computes the distance between that point and self.
(classes_q1)
1import math
2
3class Point:
4""" Point class for representing and manipulating x,y coordinates. """
5
6def __init__(self, initX, initY):
7""" Create a new point at the given coordinates. """
8self.x = initX
9self.y = initY
10
11def getX(self):
12return self.x
13
14def getY(self):
15return self.y
16
17def distanceFromOrigin(self):
18return ((self.x ** 2) + (self.y ** 2)) ** 0.5
19
20def distanceFromPoint(self, otherP):
21dx = (otherP.getX() - self.x)
22dy = (otherP.getY() - self.y)
23return math.sqrt(dy**2 + dx**2)
24
25p = Point(3, 3)
26q = Point(6, 7)
27print(p.distanceFromPoint(q))
28
(ch_cl_ex_1_answer)
Add a method
reflect_x
to Point which returns a new Point, one which is the reflection of the point about the x-axis. For example,Point(3, 5).reflect_x()
is (3, -5)
(ch_cl_02)
Add a method
slope_from_origin
which returns the slope of the line joining the origin to the point. For example,>>> Point(4, 10).slope_from_origin() 2.5
What cases will cause your method to fail? Return None when it happens.
(classes_q3)
271class Point:
2""" Point class for representing and manipulating x,y coordinates. """
3
4def __init__(self, initX, initY):
5""" Create a new point at the given coordinates. """
6self.x = initX
7self.y = initY
8
9def getX(self):
10return self.x
11
12def getY(self):
13return self.y
14
15def distanceFromOrigin(self):
16return ((self.x ** 2) + (self.y ** 2)) ** 0.5
17
18def slope_from_origin(self):
19if self.x == 0:
20return None
21else:
22return self.y / self.x
23
24
25p = Point(4, 10)
26print(p.slope_from_origin())
27
(ch_cl_ex_3_answer)
The equation of a straight line is “y = ax + b”, (or perhaps “y = mx + c”). The coefficients a and b completely describe the line. Write a method in the Point class so that if a point instance is given another point, it will compute the equation of the straight line joining the two points. It must return the two coefficients as a tuple of two values. For example,
>>> print(Point(4, 11).get_line_to(Point(6, 15))) >>> (2, 3)
This tells us that the equation of the line joining the two points is “y = 2x + 3”. When will your method fail?
(ch_cl_04)
Add a method called
move
that will take two parameters, call themdx
anddy
. The method will cause the point to move in the x and y direction the number of units given. (Hint: you will change the values of the state of the point)
(classes_q5)
301class Point:
2""" Point class for representing and manipulating x,y coordinates. """
3
4def __init__(self, initX, initY):
5""" Create a new point at the given coordinates. """
6self.x = initX
7self.y = initY
8
9def getX(self):
10return self.x
11
12def getY(self):
13return self.y
14
15def distanceFromOrigin(self):
16return ((self.x ** 2) + (self.y ** 2)) ** 0.5
17
18def move(self, dx, dy):
19self.x = self.x + dx
20self.y = self.y + dy
21
22def __str__(self):
23return str(self.x) + "," + str(self.y)
24
25
26p = Point(7, 6)
27print(p)
28p.move(5, 10)
29print(p)
30
(ch_cl_05_answer)
Given three points that fall on the circumference of a circle, find the center and radius of the circle.
(classes_q6)