]> git.decadent.org.uk Git - ion3.git/blob - ioncore/rectangle.c
[svn-upgrade] Integrating new upstream version, ion3 (20070203)
[ion3.git] / ioncore / rectangle.c
1 /*
2  * ion/ioncore/rectangle.c
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2007. 
5  *
6  * Ion is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include <libtu/minmax.h>
13 #include <libextl/extl.h>
14
15 #include "common.h"
16 #include "rectangle.h"
17
18
19 void rectangle_constrain(WRectangle *g, const WRectangle *bounds)
20 {
21     const WRectangle tmpg=*g;
22     
23     g->x=minof(maxof(tmpg.x, bounds->x), tmpg.x+tmpg.w-1);
24     g->y=minof(maxof(tmpg.y, bounds->y), tmpg.y+tmpg.h-1);
25     g->w=maxof(1, minof(bounds->x+bounds->w, tmpg.x+tmpg.w)-g->x);
26     g->h=maxof(1, minof(bounds->y+bounds->h, tmpg.y+tmpg.h)-g->y);
27 }
28
29
30 bool rectangle_contains(const WRectangle *g, int x, int y)
31 {
32     return (x>=g->x && x<g->x+g->w && y>=g->y && y<g->y+g->h);
33 }
34
35
36 void rectangle_debugprint(const WRectangle *g, const char *n)
37 {
38     fprintf(stderr, "%s %d, %d; %d, %d\n", n, g->x, g->y, g->w, g->h);
39 }
40
41
42 int rectangle_compare(const WRectangle *g, const WRectangle *h)
43 {
44     return ((g->x!=h->x ? RECTANGLE_X_DIFF : 0) |
45             (g->y!=h->y ? RECTANGLE_Y_DIFF : 0) |
46             (g->w!=h->w ? RECTANGLE_W_DIFF : 0) |
47             (g->h!=h->h ? RECTANGLE_H_DIFF : 0));
48 }