C++ Class Design: 2D Point with Coordinates and Hashing

Coding · Medium · Free problem
Design a C++ class `Point2D` that represents a point in a 2D plane. Your class should support: 1. **Cartesian and Polar representations**: Store the point internally however you like, but provide methods to get and set coordinates in both Cartesian $(x, y)$ and Polar $(r, \theta)$ form, including conversion between them. 2. **Compatibility with `std::set`**: What do you need to implement so that `Point2D` objects can be stored in a `std::set<Point2D>`? 3. **Compatibility with `std::unordered_set`**: What additional machinery do you need for `std::unordered_set<Point2D>`? Provide a complete implementation with all necessary operators and functors. Discuss any subtleties around floating-point comparison and hashing.

Open the full interactive solver, hints, and worked solution →