ZLayout EDA Library v1.0.0
Advanced Electronic Design Automation Layout Library with Bilingual Documentation
Loading...
Searching...
No Matches
rectangle.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
9#include <vector>
10#include <string>
11
12namespace zlayout {
13namespace geometry {
14
15// Forward declaration
16class Polygon;
17
26class Rectangle {
27public:
28 double x;
29 double y;
30 double width;
31 double height;
32
36 Rectangle() : x(0.0), y(0.0), width(0.0), height(0.0) {}
37
45 Rectangle(double x, double y, double width, double height);
46
53
57 Rectangle(const Rectangle& other) = default;
58
62 Rectangle& operator=(const Rectangle& other) = default;
63
67 bool operator==(const Rectangle& other) const;
68
72 bool operator!=(const Rectangle& other) const;
73
77 bool operator<(const Rectangle& other) const;
78
79 // Boundary accessors
83 double left() const { return x; }
84
88 double right() const { return x + width; }
89
93 double bottom() const { return y; }
94
98 double top() const { return y + height; }
99
103 Point center() const;
104
108 Point bottom_left() const { return Point(x, y); }
109
113 Point bottom_right() const { return Point(x + width, y); }
114
118 Point top_left() const { return Point(x, y + height); }
119
123 Point top_right() const { return Point(x + width, y + height); }
124
129 std::vector<Point> corners() const;
130
134 double area() const { return width * height; }
135
139 double perimeter() const { return 2.0 * (width + height); }
140
144 bool is_empty() const;
145
149 bool is_valid() const;
150
156 bool contains_point(const Point& point) const;
157
163 bool contains_rectangle(const Rectangle& other) const;
164
170 bool intersects(const Rectangle& other) const;
171
177 Rectangle intersection(const Rectangle& other) const;
178
184 Rectangle union_with(const Rectangle& other) const;
185
191 Rectangle expand(double margin) const;
192
201 Rectangle expand(double left, double right, double bottom, double top) const;
202
208 Rectangle translate(const Point& offset) const;
209
215 Rectangle scale(double factor) const;
216
223 Rectangle scale(double x_factor, double y_factor) const;
224
230 double distance_to(const Rectangle& other) const;
231
237 double distance_to(const Point& point) const;
238
243 Polygon to_polygon() const;
244
248 std::string to_string() const;
249
253 friend std::ostream& operator<<(std::ostream& os, const Rectangle& rect);
254
255 // Static utility functions
256
264 static Rectangle from_center(const Point& center, double width, double height);
265
271 static Rectangle bounding_box(const std::vector<Point>& points);
272
278 static Rectangle bounding_box(const std::vector<Rectangle>& rectangles);
279};
280
285 std::size_t operator()(const Rectangle& rect) const;
286};
287
288} // namespace geometry
289} // namespace zlayout
2D point with high-precision coordinates and utility methods
Definition point.hpp:23
Polygon class supporting both convex and concave polygons.
Definition polygon.hpp:25
Axis-aligned rectangle for bounding boxes and simple EDA components.
Definition rectangle.hpp:26
double top() const
Get top edge Y coordinate.
Definition rectangle.hpp:98
Point bottom_left() const
Get bottom-left corner point.
double left() const
Get left edge X coordinate.
Definition rectangle.hpp:83
bool is_valid() const
Check if rectangle is valid (positive dimensions)
Definition rectangle.cpp:85
double area() const
Calculate rectangle area.
double y
Y coordinate of bottom-left corner.
Definition rectangle.hpp:29
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.
Definition rectangle.cpp:72
Rectangle scale(double factor) const
Scale rectangle by given factor (around center)
Point center() const
Get center point of rectangle.
Definition rectangle.cpp:68
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.
Definition rectangle.cpp:37
Rectangle & operator=(const Rectangle &other)=default
Assignment operator.
bool operator<(const Rectangle &other) const
Less than operator for sorting.
Definition rectangle.cpp:41
Rectangle()
Default constructor - creates empty rectangle at origin.
Definition rectangle.hpp:36
friend std::ostream & operator<<(std::ostream &os, const Rectangle &rect)
Stream output operator.
double height
Height of rectangle.
Definition rectangle.hpp:31
double right() const
Get right edge X coordinate.
Definition rectangle.hpp:88
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.
Point top_left() const
Get top-left corner point.
bool is_empty() const
Check if rectangle is empty (zero area)
Definition rectangle.cpp:81
Rectangle union_with(const Rectangle &other) const
Calculate union rectangle that contains both rectangles.
static Rectangle from_center(const Point &center, 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.
Point bottom_right() const
Get bottom-right corner point.
bool contains_rectangle(const Rectangle &other) const
Check if another rectangle is completely inside this rectangle.
Definition rectangle.cpp:94
bool contains_point(const Point &point) const
Check if point is inside rectangle (inclusive of boundary)
Definition rectangle.cpp:89
Rectangle(const Rectangle &other)=default
Copy constructor.
Rectangle expand(double margin) const
Expand rectangle by given margin in all directions.
double x
X coordinate of bottom-left corner.
Definition rectangle.hpp:28
double distance_to(const Rectangle &other) const
Calculate minimum distance to another rectangle.
double perimeter() const
Calculate rectangle perimeter.
bool operator==(const Rectangle &other) const
Equality operator.
Definition rectangle.cpp:30
double width
Width of rectangle.
Definition rectangle.hpp:30
double bottom() const
Get bottom edge Y coordinate.
Definition rectangle.hpp:93
Main namespace for ZLayout library.
Definition component.hpp:20
2D Point class for geometric calculations
Hash function for Rectangle (for use in std::unordered_map, etc.)
std::size_t operator()(const Rectangle &rect) const