Skip to content

Commit 36b39f8

Browse files
committed
add intersect and operator== into the litehtml::position
1 parent c33894e commit 36b39f8

File tree

1 file changed

+47
-26
lines changed

1 file changed

+47
-26
lines changed

include/litehtml/types.h

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,19 @@ namespace litehtml
129129
{
130130
using vector = std::vector<position>;
131131

132-
int x;
133-
int y;
134-
int width;
135-
int height;
136-
137-
position()
138-
{
139-
x = y = width = height = 0;
140-
}
141-
142-
position(int x, int y, int width, int height)
143-
{
144-
this->x = x;
145-
this->y = y;
146-
this->width = width;
147-
this->height = height;
148-
}
132+
int x = 0;
133+
int y = 0;
134+
int width = 0;
135+
int height = 0;
136+
137+
position() = default;
138+
139+
position(int _x, int _y, int _width, int _height) :
140+
x(_x),
141+
y(_y),
142+
width(_width),
143+
height(_height)
144+
{}
149145

150146
int right() const { return x + width; }
151147
int bottom() const { return y + height; }
@@ -178,12 +174,18 @@ namespace litehtml
178174
height = sz.height;
179175
}
180176

177+
bool operator==(const position& val)
178+
{
179+
return x == val.x && y == val.y && width == val.width && height == val.height;
180+
}
181+
181182
void move_to(int _x, int _y)
182183
{
183184
x = _x;
184185
y = _y;
185186
}
186187

188+
[[nodiscard]]
187189
bool does_intersect(const position* val) const
188190
{
189191
if(!val) return true;
@@ -200,22 +202,41 @@ namespace litehtml
200202
val->top() <= bottom() );
201203
}
202204

203-
bool empty() const
205+
[[nodiscard]]
206+
position intersect(const position& src) const
204207
{
205-
if(!width && !height)
208+
position dest;
209+
int dest_x = std::max(src.x, x);
210+
int dest_y = std::max(src.y, y);
211+
int dest_x2 = std::min(src.right(), right());
212+
int dest_y2 = std::min(src.bottom(), bottom());
213+
214+
if (dest_x2 > dest_x && dest_y2 > dest_y)
215+
{
216+
dest.x = dest_x;
217+
dest.y = dest_y;
218+
dest.width = dest_x2 - dest_x;
219+
dest.height = dest_y2 - dest_y;
220+
}
221+
else
206222
{
207-
return true;
223+
dest.width = 0;
224+
dest.height = 0;
208225
}
209-
return false;
226+
227+
return dest;
210228
}
211229

230+
[[nodiscard]]
231+
bool empty() const
232+
{
233+
return !width && !height;
234+
}
235+
236+
[[nodiscard]]
212237
bool is_point_inside(int _x, int _y) const
213238
{
214-
if(_x >= left() && _x < right() && _y >= top() && _y < bottom())
215-
{
216-
return true;
217-
}
218-
return false;
239+
return (_x >= left() && _x < right() && _y >= top() && _y < bottom());
219240
}
220241
};
221242

0 commit comments

Comments
 (0)