Add a bounding box type

graphene_rect_t does not quite work for this purpose.
This commit is contained in:
Matthias Clasen
2022-03-21 15:35:34 -04:00
parent 4b6223e07d
commit ba405bfbf1
3 changed files with 125 additions and 0 deletions

80
gsk/gskboundingbox.c Normal file
View File

@@ -0,0 +1,80 @@
#include "config.h"
#include "gskboundingboxprivate.h"
GskBoundingBox *
gsk_bounding_box_init (GskBoundingBox *self,
const graphene_point_t *a,
const graphene_point_t *b)
{
self->min.x = MIN (a->x, b->x);
self->min.y = MIN (a->y, b->y);
self->max.x = MAX (a->x, b->x);
self->max.y = MAX (a->y, b->y);
return self;
}
GskBoundingBox *
gsk_bounding_box_init_copy (GskBoundingBox *self,
const GskBoundingBox *src)
{
self->min = src->min;
self->max = src->max;
return self;
}
GskBoundingBox *
gsk_bounding_box_init_from_rect (GskBoundingBox *self,
const graphene_rect_t *bounds)
{
self->min = bounds->origin;
self->max.x = bounds->origin.x + bounds->size.width;
self->max.y = bounds->origin.y + bounds->size.height;
return self;
}
void
gsk_bounding_box_expand (GskBoundingBox *self,
const graphene_point_t *p)
{
self->min.x = MIN (self->min.x, p->x);
self->min.y = MIN (self->min.y, p->y);
self->max.x = MAX (self->max.x, p->x);
self->max.y = MAX (self->max.y, p->y);
}
graphene_rect_t *
gsk_bounding_box_to_rect (const GskBoundingBox *self,
graphene_rect_t *rect)
{
rect->origin = self->min;
rect->size.width = self->max.x - self->min.x;
rect->size.height = self->max.y - self->min.y;
return rect;
}
gboolean
gsk_bounding_box_contains_point (const GskBoundingBox *self,
const graphene_point_t *p)
{
return self->min.x <= p->x && p->x <= self->max.x &&
self->min.y <= p->y && p->y <= self->max.y;
}
gboolean
gsk_bounding_box_intersection (const GskBoundingBox *a,
const GskBoundingBox *b,
GskBoundingBox *res)
{
graphene_point_t min, max;
min.x = MAX (a->min.x, b->min.x);
min.y = MAX (a->min.y, b->min.y);
max.x = MIN (a->max.x, b->max.x);
max.y = MIN (a->max.y, b->max.y);
if (res)
gsk_bounding_box_init (res, &min, &max);
return min.x <= max.x && min.y <= max.y;
}

View File

@@ -0,0 +1,44 @@
#pragma once
#include <gsk/gsktypes.h>
G_BEGIN_DECLS
typedef struct _GskBoundingBox GskBoundingBox;
struct _GskBoundingBox {
graphene_point_t min;
graphene_point_t max;
};
GDK_AVAILABLE_IN_ALL
GskBoundingBox * gsk_bounding_box_init (GskBoundingBox *self,
const graphene_point_t *a,
const graphene_point_t *b);
GDK_AVAILABLE_IN_ALL
GskBoundingBox * gsk_bounding_box_init_copy (GskBoundingBox *self,
const GskBoundingBox *src);
GDK_AVAILABLE_IN_ALL
GskBoundingBox * gsk_bounding_box_init_from_rect (GskBoundingBox *self,
const graphene_rect_t *bounds);
GDK_AVAILABLE_IN_ALL
void gsk_bounding_box_expand (GskBoundingBox *self,
const graphene_point_t *p);
GDK_AVAILABLE_IN_ALL
graphene_rect_t *gsk_bounding_box_to_rect (const GskBoundingBox *self,
graphene_rect_t *rect);
GDK_AVAILABLE_IN_ALL
gboolean gsk_bounding_box_contains_point (const GskBoundingBox *self,
const graphene_point_t *p);
GDK_AVAILABLE_IN_ALL
gboolean gsk_bounding_box_intersection (const GskBoundingBox *a,
const GskBoundingBox *b,
GskBoundingBox *res);
G_END_DECLS

View File

@@ -41,6 +41,7 @@ gsk_public_sources = files([
])
gsk_private_sources = files([
'gskboundingbox.c',
'gskcairoblur.c',
'gskcontour.c',
'gskcurve.c',