]> git.decadent.org.uk Git - ion3.git/blob - ioncore/rectangle.c
Imported Upstream version 20090110
[ion3.git] / ioncore / rectangle.c
1 /*
2  * ion/ioncore/rectangle.c
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2009. 
5  *
6  * See the included file LICENSE for details.
7  */
8
9 #include <libtu/minmax.h>
10 #include <libextl/extl.h>
11
12 #include "common.h"
13 #include "rectangle.h"
14
15
16 void rectangle_constrain(WRectangle *g, const WRectangle *bounds)
17 {
18     const WRectangle tmpg=*g;
19     
20     g->x=minof(maxof(tmpg.x, bounds->x), tmpg.x+tmpg.w-1);
21     g->y=minof(maxof(tmpg.y, bounds->y), tmpg.y+tmpg.h-1);
22     g->w=maxof(1, minof(bounds->x+bounds->w, tmpg.x+tmpg.w)-g->x);
23     g->h=maxof(1, minof(bounds->y+bounds->h, tmpg.y+tmpg.h)-g->y);
24 }
25
26
27 bool rectangle_contains(const WRectangle *g, int x, int y)
28 {
29     return (x>=g->x && x<g->x+g->w && y>=g->y && y<g->y+g->h);
30 }
31
32
33 void rectangle_debugprint(const WRectangle *g, const char *n)
34 {
35     fprintf(stderr, "%s %d, %d; %d, %d\n", n, g->x, g->y, g->w, g->h);
36 }
37
38
39 int rectangle_compare(const WRectangle *g, const WRectangle *h)
40 {
41     return ((g->x!=h->x ? RECTANGLE_X_DIFF : 0) |
42             (g->y!=h->y ? RECTANGLE_Y_DIFF : 0) |
43             (g->w!=h->w ? RECTANGLE_W_DIFF : 0) |
44             (g->h!=h->h ? RECTANGLE_H_DIFF : 0));
45 }