[Lab Evaluation] Operator Overloading - Geometry
Submit solution
Python
Points:
10 (partial)
Time limit:
0.5s
Memory limit:
12M
Author:
Problem type
Allowed languages
Question
You are working on a project to model geometric entities in 2D space. Implement two classes, Point
and Line
, to represent points and line segments, respectively.
Point
Class:
- Define a
Point
class with attributesx
andy
to represent the coordinates of a point in the Cartesian plane. - Implement the
__init__
method to initialize the point with coordinates. - Implement the
__str__
method to represent the point as a string in the format "(x, y)". - Implement the
__eq__
method to check if two points are equal.
Line
Class:
- Define a
Line
class that represents a line segment connecting two points. - Include attributes
start
andend
representing the starting and ending points of the line. - Implement the
__init__
method to initialize the line with twoPoint
objects. - Implement the
__str__
method to represent the line as a string in the format "Line: (start.x, start.y) to (end.x, end.y)". - Implement the
__add__
method to allow concatenating two lines. The result should be a newLine
object connecting the end of the first line to the start of the second line. - Ensure that the
Line
class uses composition to utilize thePoint
class for representing its starting and ending points.
Example Usage:
# Create points
point_start = Point(1, 2)
point_end = Point(3, 4)
# Create lines
line1 = Line(point_start, point_end)
line2 = Line(Point(5, 6), Point(7, 8))
# Display information
print(line1) # Output: Line: (1, 2) to (3, 4)
print(line2) # Output: Line: (3, 4) to (7, 8)
# Concatenate lines
concatenated_line = line1 + line2
print(concatenated_line) # Output: Line: (1, 2) to (7, 8)
Testcases
Input 1
1
2
3
4
3
4
7
8
Output 1
Line: (1, 2) to (3, 4)
Line: (3, 4) to (7, 8)
Concatenated Line: (1, 2) to (7, 8)
Input 2
1
2
4
5
3
5
10
11
Output 2
Line: (1, 2) to (4, 5)
Line: (3, 5) to (10, 11)
End point of the first line must match the start point of the second line for concatenation.
Concatenated None
Comments