Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import math class Point 2 D: def _ _ init _ _ ( self , x , y ) : self.x = x self.y =

import math
class Point2D:
def __init__(self, x, y):
self.x = x
self.y = y
class Line:
def __init__(self, point1, point2):
self.p1= point1
self.p2= point2
pass
def getDistance(self):
#--- YOUR CODE CONTINUES HERE
def getSlope(self):
#--- YOUR CODE CONTINUES HERE
def __str__(self):
#--- YOUR CODE CONTINUES HERE
__repr__=__str__
def __mul__(self,other):
#--- YOUR CODE CONTINUES HERE
def __contains__(self,point):
#--- YOUR CODE CONTINUES HERE
The Line class represents a 2D line that stores two Point2D objects and provides the distance between the two points and the slope of the line using the property methods getDistance and getSlope. The constructor of the Point2D class has been provided in the starter code. You might use the math module
getDistance(self)
A property method that gets the distance between the two Point2D objects that created the Line. The formula to calculate the distance between two points in a two-dimensional space is:
Returns a float rounded to 3 decimals. To round you can use the round method as round(value,
#ofDigits)
getSlope(self)
A property method that gets the slope (gradient) of the Line object. The formula to calculate the slope using two points in a two-dimensional space is:
If the slope exists, returns a float rounded to 3 decimals, otherwise, returns infinity as a float (inf float). To round you can use the round method as round(value, #ofDigits)
Examples:
>>> p1= Point2D(-7,-9)
>>> p2= Point2D(1,5.6)
>>> line1= Line(p1, p2)
>>> line1.getDistance
16.648
>>> line1.getSlope
1.825
__str__(self) and __repr__(self)
Special methods that provide a legible representation for instances of the Line class. Objects will be represented using the "slope-intercept" equation of the line: y = mx + b
To find b, substitute m with the slope and y and x for any of the points and solve for b. b must be rounded to 3 decimals.. To round you can use the round method as round(value, #ofDigits). The representation will be the string Undefined if the slope of the line is undefined. You are allowed to define a property method to compute the interception b. Note that the representation must account for all ranges of values for m and b: y = b when x =0, y = mx b when b is negative, etc.
Examples:
>>> p1= Point2D(-7,-9)
>>> p2= Point2D(1,5.6)
>>> line1= Line(p1, p2)
>>> line1
y =1.825x +3.775
>>> line5=Line(Point2D(6,48),Point2D(9,21))
>>> line5
y =-9.0x +102.0
>>> line6=Line(Point2D(2,6), Point2D(2,3))
>>> line6.getDistance
3.0
>>> line6.getSlope inf >>> line6
Undefined
>>> line7=Line(Point2D(6,5), Point2D(9,5))
>>> line7 y =5.0
__mul__
A special method to support the * operator. Returns a new Line object where the x,y attributes of every Point2D object is multiplied by an integer. The only operation allowed is Line*integer, any other non-integer values return None.
__contains__
A special method to support the in operator. Returns True if the Point object lies on the Line object, False otherwise. You cannot make any assumptions about the value that will be on the right side of the operator. If the slope is undefined, return False.
Examples:
>>> p1= Point2D(-7,-9)
>>> p2= Point2D(1,5.6)
>>> line1= Line(p1, p2)
>>> line2= line1*4
>>> isinstance(line2, Line)
True >>> line2 y =1.825x +15.1
>>> line3= line1*4
>>> line3
y =1.825x +15.1
>>> line5=Line(Point2D(6,48),Point2D(9,21))
>>> line5
y =-9.0x +102.0
>>> Point2D(45,3) in line5
False
>>> Point2D(34,-204) in line5
True
>>>(9,5) in line5
False

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions