
Problem
Solution
Submissions
Point in 2D Space and Distance Calculation
Certification: Advanced Level
Accuracy: 100%
Submissions: 2
Points: 10
Implement a Python class called Point2D
that represents a point in a 2D coordinate system. The class should include methods to calculate the distance to another point, move the point, and represent the point as a string. Additionally, implement a method to find the midpoint between two points.
Example 1
- Input: p1 = Point2D(3, 4)
- Operations: p1.distance_to(Point2D(0, 0))
- Output: 5.0
- Explanation:
- Step 1: Create a point at (3, 4).
- Step 2: Calculate distance to origin (0, 0).
- Step 3: Return the calculated distance.
Example 2
- Input: p1 = Point2D(1, 2)
- Operations: p1.move(2, 2)
- Output: Point(3.0, 4.0)
- Explanation:
- Step 1: Create a point at (1, 2).
- Step 2: Move the point by (2, 2).
- Step 3: Return the new point coordinates.
Constraints
- Coordinates can be integers or floating-point numbers.
- Distance calculations should be accurate to at least 2 decimal places.
- Time Complexity: O(1) for all operations.
- Space Complexity: O(1) for all operations.
- The class should handle edge cases like calculating distance to itself.
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use the Euclidean distance formula:
d = √[(x₂ - x₁)² + (y₂ - y₁)²]
- Implement dunder methods like
__str__
for string representation - Create a class method for midpoint calculation:
((x₁ + x₂)/2, (y₁ + y₂)/2)
- Consider implementing other methods like translation and rotation
- Use the
math
module for square root and other mathematical operations - Consider implementing operator overloading for point addition and subtraction