IDS peak ICV v1.1.0
Loading...
Searching...
No Matches
rectangle.py
Go to the documentation of this file.
1from __future__ import annotations
2
3from typing import Union, Any
4
5from ids_peak_common._internal.utils import validate_parameter_types
8
9
11 """!
12 @brief
13 A rectangle defined by a point and a size.
14
15 Represents a rectangle composed of a position (top-left corner) and a size.
16 The position and size types are customizable via template parameters.
17
18 @ingroup ids_peak_common_python_types_geometry
19 """
20
21 def __init__(self, point: Point, size: Size) -> None:
22 """!
23 @brief
24 Creates a rectangle from a point and a size.
25 @param point: The top-left corner of the rectangle.
26 @param size: The dimensions of the rectangle.
27 """
28
29 if isinstance(point, Point) and isinstance(size, Size):
30 validate_parameter_types(
31 point.x, point.y, size.width, size.height,
32 accepted_types=(int, float))
33 self._point: Point = point
34 self._size: Size = size
35 else:
36 raise TypeError("Rectangle expected (Point, Size)")
37
38 @classmethod
40 cls,
41 x: Union[int, float],
42 y: Union[int, float],
43 width: Union[int, float],
44 height: Union[int, float]
45 ) -> Rectangle:
46 """!
47 @brief
48 Constructs a rectangle from x, y, width and height.
49 @param x: x coordinate of the upper left corner
50 @param y: y coordinate of the upper left corner
51 @param width: Width of the rectangle
52 @param height: Height of the rectangle
53 """
54 validate_parameter_types(x, y, width, height,
55 accepted_types=(int, float))
56 return Rectangle(Point(x, y), Size(width, height))
57
58 def __eq__(self, other: Any) -> bool:
59 """!
60 @brief
61 Check if two rectangles are equal.
62
63 @param other: Another rectangle object OR
64 List of coordinates [x,y,width,height] OR
65 Tuple of coordinates (x,y,width,height)
66
67 @return True if rectangles are equal, False otherwise
68 """
69 if isinstance(other, Rectangle):
70 return self._point == other.position and self._size == other.size
71 if isinstance(other, (list, tuple)):
72 if len(other) != 4:
73 return False
74 return Point.__eq__(self._point, other[:2]) and Size.__eq__(self._size, other[2:])
75
76 return False
77
78 def __ne__(self, other: Any) -> bool:
79 """!
80 @brief
81 Check if two rectangles are not equal.
82
83 @param other: Another rectangle object
84
85 @return True if rectangles are not equal, False otherwise
86 """
87 return not self.__eq__(other)
88
89 def __str__(self) -> str:
90 """!
91 @brief
92 Get a string representation of the rectangle.
93
94 @return String representation of the rectangle
95 """
96 return (f"(x={self.x}, y={self.y}, width={self.width}, "
97 f"height={self.height})")
98
99 def __repr__(self) -> str:
100 """!
101 @brief
102 Return an unambiguous string of the Rectangle object.
103 """
104 return (f"{self.__class__.__name__}(point={self._point!r}, "
105 f"size={self._size!r})")
106
107 @property
108 def position(self) -> Point:
109 """!
110 @brief
111 Returns the top-left corner of the rectangle.
112 """
113 return self._point
114
115 @property
116 def size(self) -> Size:
117 """!
118 @brief
119 Returns the dimensions of the rectangle.
120 """
121 return self._size
122
123 @property
124 def x(self) -> Union[int, float]:
125 """!
126 @brief
127 Returns the x-coordinate of the rectangle's top-left corner.
128 """
129 return self._point.x
130
131 @property
132 def y(self) -> Union[int, float]:
133 """!
134 @brief
135 Returns the y-coordinate of the rectangle's top-left corner.
136 """
137 return self._point.y
138
139 @property
140 def width(self) -> Union[int, float]:
141 """!
142 @brief
143 Returns the width of the rectangle.
144 """
145 return self._size.width
146
147 @property
148 def height(self) -> Union[int, float]:
149 """!
150 @brief
151 Returns the height of the rectangle.
152 """
153 return self._size.height
Represents a point in 2D space with x and y coordinates.
Definition point.py:6
A rectangle defined by a point and a size.
Definition rectangle.py:10
None __init__(self, Point point, Size size)
Creates a rectangle from a point and a size.
Definition rectangle.py:21
Union[int, float] height(self)
Returns the height of the rectangle.
Definition rectangle.py:148
Point position(self)
Returns the top-left corner of the rectangle.
Definition rectangle.py:108
Union[int, float] x(self)
Returns the x-coordinate of the rectangle's top-left corner.
Definition rectangle.py:124
bool __ne__(self, Any other)
Check if two rectangles are not equal.
Definition rectangle.py:78
Union[int, float] y(self)
Returns the y-coordinate of the rectangle's top-left corner.
Definition rectangle.py:132
Union[int, float] width(self)
Returns the width of the rectangle.
Definition rectangle.py:140
Rectangle create_from_coordinates_and_dimensions(cls, Union[int, float] x, Union[int, float] y, Union[int, float] width, Union[int, float] height)
Constructs a rectangle from x, y, width and height.
Definition rectangle.py:45
str __str__(self)
Get a string representation of the rectangle.
Definition rectangle.py:89
str __repr__(self)
Return an unambiguous string of the Rectangle object.
Definition rectangle.py:99
bool __eq__(self, Any other)
Check if two rectangles are equal.
Definition rectangle.py:58
A size consists of width and height where width and height have to be greater than or equal to zero.
Definition size.py:9