roundedrect: Fix gsk_rounded_rect_intersects_rect()

The previous code would return FALSE for
  gsk_rounded_rect_intersects_rect (&rounded, &rounded.bounds);
if rounded was indeed rounded.
This commit is contained in:
Benjamin Otte 2020-02-13 05:21:49 +01:00
parent 16e38701b6
commit b0369fc300

View File

@ -324,6 +324,65 @@ ellipsis_contains_point (const graphene_size_t *ellipsis,
+ (point->y * point->y) / (ellipsis->height * ellipsis->height) <= 1; + (point->y * point->y) / (ellipsis->height * ellipsis->height) <= 1;
} }
typedef enum
{
INSIDE,
OUTSIDE_TOP_LEFT,
OUTSIDE_TOP_RIGHT,
OUTSIDE_BOTTOM_LEFT,
OUTSIDE_BOTTOM_RIGHT,
OUTSIDE
} Location;
static Location
gsk_rounded_rect_locate_point (const GskRoundedRect *self,
const graphene_point_t *point)
{
if (point->x < self->bounds.origin.x ||
point->y < self->bounds.origin.y ||
point->x >= self->bounds.origin.x + self->bounds.size.width ||
point->y >= self->bounds.origin.y + self->bounds.size.height)
return OUTSIDE;
if (self->bounds.origin.x + self->corner[GSK_CORNER_TOP_LEFT].width > point->x &&
self->bounds.origin.y + self->corner[GSK_CORNER_TOP_LEFT].height > point->y &&
!ellipsis_contains_point (&self->corner[GSK_CORNER_TOP_LEFT],
&GRAPHENE_POINT_INIT (
self->bounds.origin.x + self->corner[GSK_CORNER_TOP_LEFT].width - point->x,
self->bounds.origin.y + self->corner[GSK_CORNER_TOP_LEFT].height- point->y
)))
return OUTSIDE_TOP_LEFT;
if (self->bounds.origin.x + self->bounds.size.width - self->corner[GSK_CORNER_TOP_RIGHT].width < point->x &&
self->bounds.origin.y + self->corner[GSK_CORNER_TOP_RIGHT].height > point->y &&
!ellipsis_contains_point (&self->corner[GSK_CORNER_TOP_RIGHT],
&GRAPHENE_POINT_INIT (
self->bounds.origin.x + self->bounds.size.width - self->corner[GSK_CORNER_TOP_RIGHT].width - point->x,
self->bounds.origin.y + self->corner[GSK_CORNER_TOP_RIGHT].height- point->y
)))
return OUTSIDE_TOP_RIGHT;
if (self->bounds.origin.x + self->corner[GSK_CORNER_BOTTOM_LEFT].width > point->x &&
self->bounds.origin.y + self->bounds.size.height - self->corner[GSK_CORNER_BOTTOM_LEFT].height < point->y &&
!ellipsis_contains_point (&self->corner[GSK_CORNER_BOTTOM_LEFT],
&GRAPHENE_POINT_INIT (
self->bounds.origin.x + self->corner[GSK_CORNER_BOTTOM_LEFT].width - point->x,
self->bounds.origin.y + self->bounds.size.height - self->corner[GSK_CORNER_BOTTOM_LEFT].height- point->y
)))
return OUTSIDE_BOTTOM_LEFT;
if (self->bounds.origin.x + self->bounds.size.width - self->corner[GSK_CORNER_BOTTOM_RIGHT].width < point->x &&
self->bounds.origin.y + self->bounds.size.height - self->corner[GSK_CORNER_BOTTOM_RIGHT].height < point->y &&
!ellipsis_contains_point (&self->corner[GSK_CORNER_BOTTOM_RIGHT],
&GRAPHENE_POINT_INIT (
self->bounds.origin.x + self->bounds.size.width - self->corner[GSK_CORNER_BOTTOM_RIGHT].width - point->x,
self->bounds.origin.y + self->bounds.size.height - self->corner[GSK_CORNER_BOTTOM_RIGHT].height- point->y
)))
return OUTSIDE_BOTTOM_RIGHT;
return INSIDE;
}
/** /**
* gsk_rounded_rect_contains_point: * gsk_rounded_rect_contains_point:
* @self: a #GskRoundedRect * @self: a #GskRoundedRect
@ -338,49 +397,7 @@ gboolean
gsk_rounded_rect_contains_point (const GskRoundedRect *self, gsk_rounded_rect_contains_point (const GskRoundedRect *self,
const graphene_point_t *point) const graphene_point_t *point)
{ {
if (point->x < self->bounds.origin.x || return gsk_rounded_rect_locate_point (self, point) == INSIDE;
point->y < self->bounds.origin.y ||
point->x >= self->bounds.origin.x + self->bounds.size.width ||
point->y >= self->bounds.origin.y + self->bounds.size.height)
return FALSE;
if (self->bounds.origin.x + self->corner[GSK_CORNER_TOP_LEFT].width > point->x &&
self->bounds.origin.y + self->corner[GSK_CORNER_TOP_LEFT].height > point->y &&
!ellipsis_contains_point (&self->corner[GSK_CORNER_TOP_LEFT],
&GRAPHENE_POINT_INIT (
self->bounds.origin.x + self->corner[GSK_CORNER_TOP_LEFT].width - point->x,
self->bounds.origin.y + self->corner[GSK_CORNER_TOP_LEFT].height- point->y
)))
return FALSE;
if (self->bounds.origin.x + self->bounds.size.width - self->corner[GSK_CORNER_TOP_RIGHT].width < point->x &&
self->bounds.origin.y + self->corner[GSK_CORNER_TOP_RIGHT].height > point->y &&
!ellipsis_contains_point (&self->corner[GSK_CORNER_TOP_RIGHT],
&GRAPHENE_POINT_INIT (
self->bounds.origin.x + self->bounds.size.width - self->corner[GSK_CORNER_TOP_RIGHT].width - point->x,
self->bounds.origin.y + self->corner[GSK_CORNER_TOP_RIGHT].height- point->y
)))
return FALSE;
if (self->bounds.origin.x + self->corner[GSK_CORNER_BOTTOM_LEFT].width > point->x &&
self->bounds.origin.y + self->bounds.size.height - self->corner[GSK_CORNER_BOTTOM_LEFT].height < point->y &&
!ellipsis_contains_point (&self->corner[GSK_CORNER_BOTTOM_LEFT],
&GRAPHENE_POINT_INIT (
self->bounds.origin.x + self->corner[GSK_CORNER_BOTTOM_LEFT].width - point->x,
self->bounds.origin.y + self->bounds.size.height - self->corner[GSK_CORNER_BOTTOM_LEFT].height- point->y
)))
return FALSE;
if (self->bounds.origin.x + self->bounds.size.width - self->corner[GSK_CORNER_BOTTOM_RIGHT].width < point->x &&
self->bounds.origin.y + self->bounds.size.height - self->corner[GSK_CORNER_BOTTOM_RIGHT].height < point->y &&
!ellipsis_contains_point (&self->corner[GSK_CORNER_BOTTOM_RIGHT],
&GRAPHENE_POINT_INIT (
self->bounds.origin.x + self->bounds.size.width - self->corner[GSK_CORNER_BOTTOM_RIGHT].width - point->x,
self->bounds.origin.y + self->bounds.size.height - self->corner[GSK_CORNER_BOTTOM_RIGHT].height- point->y
)))
return FALSE;
return TRUE;
} }
/** /**
@ -431,10 +448,12 @@ gsk_rounded_rect_intersects_rect (const GskRoundedRect *self,
if (!graphene_rect_intersection (&self->bounds, rect, NULL)) if (!graphene_rect_intersection (&self->bounds, rect, NULL))
return FALSE; return FALSE;
if (!gsk_rounded_rect_contains_point (self, &rect->origin) && /* If the bounding boxes intersect but the rectangles don't, one of the rect's corners
!gsk_rounded_rect_contains_point (self, &GRAPHENE_POINT_INIT (rect->origin.x + rect->size.width, rect->origin.y)) && * must be in the opposite corner's outside region */
!gsk_rounded_rect_contains_point (self, &GRAPHENE_POINT_INIT (rect->origin.x, rect->origin.y + rect->size.height)) && if (gsk_rounded_rect_locate_point (self, &rect->origin) == OUTSIDE_BOTTOM_RIGHT ||
!gsk_rounded_rect_contains_point (self, &GRAPHENE_POINT_INIT (rect->origin.x + rect->size.width, rect->origin.y + rect->size.height))) gsk_rounded_rect_locate_point (self, &GRAPHENE_POINT_INIT (rect->origin.x + rect->size.width, rect->origin.y)) == OUTSIDE_BOTTOM_LEFT ||
gsk_rounded_rect_locate_point (self, &GRAPHENE_POINT_INIT (rect->origin.x, rect->origin.y + rect->size.height)) == OUTSIDE_TOP_RIGHT ||
gsk_rounded_rect_locate_point (self, &GRAPHENE_POINT_INIT (rect->origin.x + rect->size.width, rect->origin.y + rect->size.height)) == OUTSIDE_TOP_LEFT)
return FALSE; return FALSE;
return TRUE; return TRUE;