Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Line class (2.5 pts) The Line class represents a 2D line that stores two Point2D objects and provides the distance between the two points

The Line class (2.5 pts) 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 Note: Read the description of all method first, then outline your strategy. In this section, you are expected to reduce the amount of repeated code by calling methods in the class to reuse code. Both functionality and design are part of the grade for this exercise. 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: ???? = √(????2 − ????1 ) 2 + (????2 − ????1 ) 2 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: ???? = ????2 − ????1 ????2 − ????1 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 Line - Your attributes here - getDistance -> float getSlope -> float __str__() -> str representation of Line __repr__ -> str representation of Line __mul__(other: any) -> Line __contains__(point: any) -> bool Point2D x : int/float y : int/float - Your methods here - 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.

Step by Step Solution

3.45 Rating (145 Votes )

There are 3 Steps involved in it

Step: 1

To implement the Line class as described well follow these steps Define the Line class which takes t... 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

Complex Variables and Applications

Authors: James Brown, Ruel Churchill

8th edition

73051942, 978-0073051949

More Books

Students also viewed these Algorithms questions

Question

Why do some people resist change?

Answered: 1 week ago