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
@property
def getDistance(self):
return round(math.sqrt(pow(self.p2.x - self.p1.x,2)+ pow(self.p2.y - self.p1.y,2)),3)
pass
@property
def getSlope(self):
if self.p2.x - self.p1.x ==0:
return math.inf
return round((self.p2.y - self.p1.y)/(self.p2.x - self.p1.x),3)
pass
def __str__(self):
if self.p2.x - self.p1.x ==0:
return "Undefined"
elif self.getSlope ==0.0:
return f"y ={round(self.p1.y - self.getSlope * self.p1.x,3)}"
return f"y ={self.getSlope}x +{round(self.p1.y - self.getSlope*self.p1.x,3)}"
__repr__=__str__
def __mul__(self,other):
#--- YOUR CODE CONTINUES HERE
def __contains__(self,point):
#--- YOUR CODE CONTINUES HERE
__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.
Recall that floating-point numbers are stored in binary, as a result, the binary number may not accurately represent the original base 10 number. For example:
>>>0.7+0.2==0.9
False
This error, known as floating-point representation error, happens way more often than you might realize. To implement this method, you will need to compare floating-point numbers. Avoid checking for equality using == with floats. Instead use the isclose() method available in the math module:
>>> import math
>>> math.isclose(0.7+0.2,0.9)
True
math.isclose() checks if the first argument is acceptably close to the second argument by examining the distance between the first argument and the second argument, which is equivalent to the absolute value of the difference of the values:
>>> x =0.7+0.2
>>> y =0.9
>>> abs(x-y)
1.1102230246251565e-16
If abs(x - y) is smaller than some percentage of the larger of x or y, then x is considered sufficiently close to y to be "equal" to y. This percentage is called relative tolerance.
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

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

Students also viewed these Databases questions