parent
a51b76083e
commit
9b65d0b3f0
@ -0,0 +1,20 @@
|
||||
#ifndef _WLR_TYPES_GEOMETRY_H
|
||||
#define _WLR_TYPES_GEOMETRY_H
|
||||
#include <stdbool.h>
|
||||
|
||||
struct wlr_box {
|
||||
int x, y;
|
||||
int width, height;
|
||||
};
|
||||
|
||||
void wlr_box_closest_point(struct wlr_box *box, double x, double y,
|
||||
double *dest_x, double *dest_y);
|
||||
|
||||
bool wlr_box_intersection(struct wlr_box *box_a,
|
||||
struct wlr_box *box_b, struct wlr_box **dest);
|
||||
|
||||
bool wlr_box_contains_point(struct wlr_box *box, double x, double y);
|
||||
|
||||
bool wlr_box_empty(struct wlr_box *box);
|
||||
|
||||
#endif
|
@ -1,20 +0,0 @@
|
||||
#ifndef _WLR_TYPES_GEOMETRY_H
|
||||
#define _WLR_TYPES_GEOMETRY_H
|
||||
#include <stdbool.h>
|
||||
|
||||
struct wlr_geometry {
|
||||
int x, y;
|
||||
int width, height;
|
||||
};
|
||||
|
||||
void wlr_geometry_closest_boundary(struct wlr_geometry *geo, double x, double y,
|
||||
int *dest_x, int *dest_y, double *distance);
|
||||
|
||||
bool wlr_geometry_intersection(struct wlr_geometry *geo_a,
|
||||
struct wlr_geometry *geo_b, struct wlr_geometry **dest);
|
||||
|
||||
bool wlr_geometry_contains_point(struct wlr_geometry *geo, int x, int y);
|
||||
|
||||
bool wlr_geometry_empty(struct wlr_geometry *geo);
|
||||
|
||||
#endif
|
@ -0,0 +1,67 @@
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <wlr/types/wlr_box.h>
|
||||
#include <wlr/util/log.h>
|
||||
|
||||
void wlr_box_closest_point(struct wlr_box *box, double x, double y,
|
||||
double *dest_x, double *dest_y) {
|
||||
// find the closest x point
|
||||
if (x < box->x) {
|
||||
*dest_x = box->x;
|
||||
} else if (x > box->x + box->width) {
|
||||
*dest_x = box->x + box->width;
|
||||
} else {
|
||||
*dest_x = x;
|
||||
}
|
||||
|
||||
// find closest y point
|
||||
if (y < box->y) {
|
||||
*dest_y = box->y;
|
||||
} else if (y > box->y + box->height) {
|
||||
*dest_y = box->y + box->height;
|
||||
} else {
|
||||
*dest_y = y;
|
||||
}
|
||||
}
|
||||
|
||||
bool wlr_box_empty(struct wlr_box *box) {
|
||||
return box == NULL || box->width <= 0 || box->height <= 0;
|
||||
}
|
||||
|
||||
bool wlr_box_intersection(struct wlr_box *box_a,
|
||||
struct wlr_box *box_b, struct wlr_box **box_dest) {
|
||||
struct wlr_box *dest = *box_dest;
|
||||
bool a_empty = wlr_box_empty(box_a);
|
||||
bool b_empty = wlr_box_empty(box_b);
|
||||
|
||||
if (a_empty || b_empty) {
|
||||
dest->x = 0;
|
||||
dest->y = 0;
|
||||
dest->width = -100;
|
||||
dest->height = -100;
|
||||
return false;
|
||||
}
|
||||
|
||||
int x1 = fmax(box_a->x, box_b->x);
|
||||
int y1 = fmax(box_a->y, box_b->y);
|
||||
int x2 = fmin(box_a->x + box_a->width, box_b->x + box_b->width);
|
||||
int y2 = fmin(box_a->y + box_a->height, box_b->y + box_b->height);
|
||||
|
||||
dest->x = x1;
|
||||
dest->y = y1;
|
||||
dest->width = x2 - x1;
|
||||
dest->height = y2 - y1;
|
||||
|
||||
return !wlr_box_empty(dest);
|
||||
}
|
||||
|
||||
bool wlr_box_contains_point(struct wlr_box *box, double x, double y) {
|
||||
if (wlr_box_empty(box)) {
|
||||
return false;
|
||||
} else {
|
||||
return x >= box->x && x <= box->x + box->width &&
|
||||
y >= box->y && y <= box->y + box->height;
|
||||
}
|
||||
}
|
@ -1,86 +0,0 @@
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <wlr/types/wlr_geometry.h>
|
||||
#include <wlr/util/log.h>
|
||||
|
||||
static double get_distance(double x1, double y1, double x2, double y2) {
|
||||
double distance;
|
||||
distance = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
|
||||
return distance;
|
||||
}
|
||||
|
||||
void wlr_geometry_closest_boundary(struct wlr_geometry *geo, double x, double y,
|
||||
int *dest_x, int *dest_y, double *distance) {
|
||||
// find the closest x point
|
||||
if (x < geo->x) {
|
||||
*dest_x = geo->x;
|
||||
} else if (x > geo->x + geo->width) {
|
||||
*dest_x = geo->x + geo->width;
|
||||
} else {
|
||||
*dest_x = x;
|
||||
}
|
||||
|
||||
// find closest y point
|
||||
if (y < geo->y) {
|
||||
*dest_y = geo->y;
|
||||
} else if (y > geo->y + geo->height) {
|
||||
*dest_y = geo->y + geo->height;
|
||||
} else {
|
||||
*dest_y = y;
|
||||
}
|
||||
|
||||
// calculate distance
|
||||
if (distance) {
|
||||
*distance = get_distance(*dest_x, *dest_y, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef max
|
||||
#define max(a,b) ((a) > (b) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#ifndef min
|
||||
#define min(a,b) ((a) < (b) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
bool wlr_geometry_empty(struct wlr_geometry *geo) {
|
||||
return geo == NULL || geo->width <= 0 || geo->height <= 0;
|
||||
}
|
||||
|
||||
bool wlr_geometry_intersection(struct wlr_geometry *geo_a,
|
||||
struct wlr_geometry *geo_b, struct wlr_geometry **geo_dest) {
|
||||
struct wlr_geometry *dest = *geo_dest;
|
||||
bool a_empty = wlr_geometry_empty(geo_a);
|
||||
bool b_empty = wlr_geometry_empty(geo_b);
|
||||
|
||||
if (a_empty || b_empty) {
|
||||
dest->x = 0;
|
||||
dest->y = 0;
|
||||
dest->width = -100;
|
||||
dest->height = -100;
|
||||
return false;
|
||||
}
|
||||
|
||||
int x1 = max(geo_a->x, geo_b->x);
|
||||
int y1 = max(geo_a->y, geo_b->y);
|
||||
int x2 = min(geo_a->x + geo_a->width, geo_b->x + geo_b->width);
|
||||
int y2 = min(geo_a->y + geo_a->height, geo_b->y + geo_b->height);
|
||||
|
||||
dest->x = x1;
|
||||
dest->y = y1;
|
||||
dest->width = x2 - x1;
|
||||
dest->height = y2 - y1;
|
||||
|
||||
return !wlr_geometry_empty(dest);
|
||||
}
|
||||
|
||||
bool wlr_geometry_contains_point(struct wlr_geometry *geo, int x, int y) {
|
||||
if (wlr_geometry_empty(geo)) {
|
||||
return false;
|
||||
} else {
|
||||
return x >= geo->x && x <= geo->x + geo->width &&
|
||||
y >= geo->y && y <= geo->y + geo->height;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue