Write the class Line that stores the coordinates of two points in a line and pro
ID: 3751395 • Letter: W
Question
Write the class Line that stores the coordinates of two points in a line and provides the distance between the two points and the slope of the line using the property methods called distance and slope. Unless the slope is equal to infinity, both methods must return the value as float.
EXAMPLES: >>> line1=Line((8,3),(0,-4)) #Coordinates provided as tuple >>> line1.distance 10.63 >>> line1.slope 0.875 >>> line2=Line((-7,-9),(1,5.6)) >>> line2.distance 16.648 >>> line2.slope 1.825 >>> line3=Line((2,6),(2,3)) >>> line3.distance 3.0 >>> line3.slope 'Infinity
Explanation / Answer
class Line: def __init__(self, coord1, coord2): self.point1 = coord1 self.point2 = coord2 @property def distance(self): return ((self.point2[1] - self.point1[1]) ** 2 + (self.point2[0] - self.point1[0]) ** 2) ** 0.5 @property def slope(self): try: return (self.point2[1] - self.point1[1]) / (self.point2[0] - self.point1[0]) except: return 'Infinity'
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.