19 throw std::invalid_argument(
"Rectangle dimensions must be non-negative");
38 return !(*
this == other);
43 double this_area =
area();
44 double other_area = other.
area();
46 return this_area < other_area;
90 return point.
x >=
x && point.
x <=
x +
width &&
95 return other.
x >=
x && other.
y >=
y &&
110 double left = std::max(
x, other.
x);
112 double bottom = std::max(
y, other.
y);
122 double left = std::min(
x, other.
x);
124 double bottom = std::min(
y, other.
y);
146 double new_width =
width * factor;
147 double new_height =
height * factor;
149 return Rectangle(center_point.
x - new_width / 2.0,
150 center_point.
y - new_height / 2.0,
151 new_width, new_height);
156 double new_width =
width * x_factor;
157 double new_height =
height * y_factor;
159 return Rectangle(center_point.
x - new_width / 2.0,
160 center_point.
y - new_height / 2.0,
161 new_width, new_height);
170 double dx = 0.0, dy = 0.0;
172 if (other.
x + other.
width <
x) {
173 dx =
x - (other.
x + other.
width);
174 }
else if (other.
x >
x +
width) {
184 return std::sqrt(dx * dx + dy * dy);
192 double dx = std::max(0.0, std::max(
x - point.
x, point.
x - (
x +
width)));
193 double dy = std::max(0.0, std::max(
y - point.
y, point.
y - (
y +
height)));
195 return std::sqrt(dx * dx + dy * dy);
199 auto corner_points =
corners();
204 std::ostringstream oss;
205 oss <<
"Rectangle(x=" <<
x <<
", y=" <<
y
206 <<
", width=" <<
width <<
", height=" <<
height <<
")";
222 if (points.empty()) {
226 double min_x = points[0].x, max_x = points[0].x;
227 double min_y = points[0].y, max_y = points[0].y;
229 for (
const auto& point : points) {
230 min_x = std::min(min_x, point.x);
231 max_x = std::max(max_x, point.x);
232 min_y = std::min(min_y, point.y);
233 max_y = std::max(max_y, point.y);
236 return Rectangle(min_x, min_y, max_x - min_x, max_y - min_y);
240 if (rectangles.empty()) {
245 for (
size_t i = 1; i < rectangles.size(); ++i) {
255 std::hash<double> hasher;
256 std::size_t h1 = hasher(rect.
x);
257 std::size_t h2 = hasher(rect.
y);
258 std::size_t h3 = hasher(rect.
width);
259 std::size_t h4 = hasher(rect.
height);
262 return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3);
2D point with high-precision coordinates and utility methods
static constexpr double TOLERANCE
Default precision tolerance for floating point comparisons.
Polygon class supporting both convex and concave polygons.
Axis-aligned rectangle for bounding boxes and simple EDA components.
double top() const
Get top edge Y coordinate.
Point bottom_left() const
Get bottom-left corner point.
double left() const
Get left edge X coordinate.
bool is_valid() const
Check if rectangle is valid (positive dimensions)
double area() const
Calculate rectangle area.
double y
Y coordinate of bottom-left corner.
std::string to_string() const
Get string representation.
Polygon to_polygon() const
Convert rectangle to polygon representation.
std::vector< Point > corners() const
Get all four corner points.
Rectangle scale(double factor) const
Scale rectangle by given factor (around center)
Point center() const
Get center point of rectangle.
Point top_right() const
Get top-right corner point.
Rectangle intersection(const Rectangle &other) const
Calculate intersection rectangle with another rectangle.
bool operator!=(const Rectangle &other) const
Inequality operator.
bool operator<(const Rectangle &other) const
Less than operator for sorting.
Rectangle()
Default constructor - creates empty rectangle at origin.
double height
Height of rectangle.
double right() const
Get right edge X coordinate.
Rectangle translate(const Point &offset) const
Translate rectangle by given offset.
static Rectangle bounding_box(const std::vector< Point > &points)
Create bounding rectangle for a set of points.
bool is_empty() const
Check if rectangle is empty (zero area)
Rectangle union_with(const Rectangle &other) const
Calculate union rectangle that contains both rectangles.
static Rectangle from_center(const Point ¢er, double width, double height)
Create rectangle from center point and dimensions.
bool intersects(const Rectangle &other) const
Check if this rectangle intersects with another rectangle.
bool contains_rectangle(const Rectangle &other) const
Check if another rectangle is completely inside this rectangle.
bool contains_point(const Point &point) const
Check if point is inside rectangle (inclusive of boundary)
Rectangle expand(double margin) const
Expand rectangle by given margin in all directions.
double x
X coordinate of bottom-left corner.
double distance_to(const Rectangle &other) const
Calculate minimum distance to another rectangle.
bool operator==(const Rectangle &other) const
Equality operator.
double width
Width of rectangle.
std::ostream & operator<<(std::ostream &os, const Point &point)
Main namespace for ZLayout library.
Polygon class for complex geometric shapes and EDA components.
Axis-aligned rectangle class for bounding boxes and simple components.
std::size_t operator()(const Rectangle &rect) const