[Lab Evaluation] Operator Overloading - Geometry


Submit solution

Points: 10 (partial)
Time limit: 0.5s
Memory limit: 12M

Author:
Problem type
Allowed languages
Python

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:
  1. Define a Point class with attributes x and y to represent the coordinates of a point in the Cartesian plane.
  2. Implement the __init__ method to initialize the point with coordinates.
  3. Implement the __str__ method to represent the point as a string in the format "(x, y)".
  4. Implement the __eq__ method to check if two points are equal.
Line Class:
  1. Define a Line class that represents a line segment connecting two points.
  2. Include attributes start and end representing the starting and ending points of the line.
  3. Implement the __init__ method to initialize the line with two Point objects.
  4. Implement the __str__ method to represent the line as a string in the format "Line: (start.x, start.y) to (end.x, end.y)".
  5. Implement the __add__ method to allow concatenating two lines. The result should be a new Line object connecting the end of the first line to the start of the second line.
  6. Ensure that the Line class uses composition to utilize the Point 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

There are no comments at the moment.