You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.8 KiB
122 lines
2.8 KiB
7 years ago
|
#include <limits.h>
|
||
|
#include <math.h>
|
||
|
#include <stdbool.h>
|
||
7 years ago
|
#include <stdlib.h>
|
||
4 years ago
|
#include <wlr/util/box.h>
|
||
7 years ago
|
#include <wlr/util/log.h>
|
||
|
|
||
7 years ago
|
void wlr_box_closest_point(const struct wlr_box *box, double x, double y,
|
||
7 years ago
|
double *dest_x, double *dest_y) {
|
||
5 years ago
|
// if box is empty, then it contains no points, so no closest point either
|
||
|
if (box->width <= 0 || box->height <= 0) {
|
||
|
*dest_x = NAN;
|
||
|
*dest_y = NAN;
|
||
|
return;
|
||
|
}
|
||
|
|
||
7 years ago
|
// find the closest x point
|
||
|
if (x < box->x) {
|
||
|
*dest_x = box->x;
|
||
7 years ago
|
} else if (x >= box->x + box->width) {
|
||
|
*dest_x = box->x + box->width - 1;
|
||
7 years ago
|
} else {
|
||
|
*dest_x = x;
|
||
|
}
|
||
|
|
||
|
// find closest y point
|
||
|
if (y < box->y) {
|
||
|
*dest_y = box->y;
|
||
7 years ago
|
} else if (y >= box->y + box->height) {
|
||
|
*dest_y = box->y + box->height - 1;
|
||
7 years ago
|
} else {
|
||
|
*dest_y = y;
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
bool wlr_box_empty(const struct wlr_box *box) {
|
||
7 years ago
|
return box == NULL || box->width <= 0 || box->height <= 0;
|
||
|
}
|
||
|
|
||
6 years ago
|
bool wlr_box_intersection(struct wlr_box *dest, const struct wlr_box *box_a,
|
||
|
const struct wlr_box *box_b) {
|
||
7 years ago
|
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);
|
||
7 years ago
|
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);
|
||
7 years ago
|
|
||
|
dest->x = x1;
|
||
|
dest->y = y1;
|
||
|
dest->width = x2 - x1;
|
||
|
dest->height = y2 - y1;
|
||
|
|
||
|
return !wlr_box_empty(dest);
|
||
|
}
|
||
|
|
||
7 years ago
|
bool wlr_box_contains_point(const struct wlr_box *box, double x, double y) {
|
||
7 years ago
|
if (wlr_box_empty(box)) {
|
||
|
return false;
|
||
|
} else {
|
||
7 years ago
|
return x >= box->x && x < box->x + box->width &&
|
||
|
y >= box->y && y < box->y + box->height;
|
||
7 years ago
|
}
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
void wlr_box_transform(struct wlr_box *dest, const struct wlr_box *box,
|
||
|
enum wl_output_transform transform, int width, int height) {
|
||
7 years ago
|
struct wlr_box src = *box;
|
||
|
|
||
7 years ago
|
if (transform % 2 == 0) {
|
||
7 years ago
|
dest->width = src.width;
|
||
|
dest->height = src.height;
|
||
7 years ago
|
} else {
|
||
7 years ago
|
dest->width = src.height;
|
||
|
dest->height = src.width;
|
||
7 years ago
|
}
|
||
|
|
||
|
switch (transform) {
|
||
|
case WL_OUTPUT_TRANSFORM_NORMAL:
|
||
7 years ago
|
dest->x = src.x;
|
||
|
dest->y = src.y;
|
||
7 years ago
|
break;
|
||
|
case WL_OUTPUT_TRANSFORM_90:
|
||
5 years ago
|
dest->x = height - src.y - src.height;
|
||
|
dest->y = src.x;
|
||
7 years ago
|
break;
|
||
|
case WL_OUTPUT_TRANSFORM_180:
|
||
7 years ago
|
dest->x = width - src.x - src.width;
|
||
|
dest->y = height - src.y - src.height;
|
||
7 years ago
|
break;
|
||
|
case WL_OUTPUT_TRANSFORM_270:
|
||
5 years ago
|
dest->x = src.y;
|
||
|
dest->y = width - src.x - src.width;
|
||
7 years ago
|
break;
|
||
|
case WL_OUTPUT_TRANSFORM_FLIPPED:
|
||
7 years ago
|
dest->x = width - src.x - src.width;
|
||
|
dest->y = src.y;
|
||
7 years ago
|
break;
|
||
|
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
|
||
5 years ago
|
dest->x = src.y;
|
||
|
dest->y = src.x;
|
||
7 years ago
|
break;
|
||
|
case WL_OUTPUT_TRANSFORM_FLIPPED_180:
|
||
7 years ago
|
dest->x = src.x;
|
||
|
dest->y = height - src.y - src.height;
|
||
7 years ago
|
break;
|
||
|
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
|
||
5 years ago
|
dest->x = height - src.y - src.height;
|
||
|
dest->y = width - src.x - src.width;
|
||
7 years ago
|
break;
|
||
|
}
|
||
|
}
|