summaryrefslogtreecommitdiff
path: root/rect.c
blob: c72c66ec0b3f0541a409710f119364c4bdcc2d33 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "maths.h"
#include "rect.h"

Rect* rect_clip(Rect* r, const Rect* c) {
	int n;
	int x = r->x, y = r->y;
	if ((n = c->x - r->x) > 0) {
		r->w -= n;
		r->x += n;
	}
	if ((n = c->y - r->y) > 0) {
		r->h -= n;
		r->y += n;
	}
	if ((n = x + r->w - (c->x + c->w)) > 0)
		r->w -= n;
	if ((n = y + r->h - (c->y + c->h)) > 0)
		r->h -= n;
	return r;
}

Rect* rect_clips(Rect* r, Rect* s, const Rect* c) {
	int n;
	int x = r->x, y = r->y;
	if ((n = c->x - r->x) > 0) {
		r->w -= n;
		r->x += n;
		s->w -= n;
		s->x += n;
	}
	if ((n = c->y - r->y) > 0) {
		r->h -= n;
		r->y += n;
		s->h -= n;
		s->y += n;
	}
	if ((n = x + r->w - (c->x + c->w)) > 0) {
		r->w -= n;
		s->w -= n;
	}
	if ((n = y + r->h - (c->y + c->h)) > 0) {
		r->h -= n;
		s->h -= n;
	}
	return r;
}