2 * ion/ioncore/rectangle.c
4 * Copyright (c) Tuomo Valkonen 1999-2007.
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.
12 #include <libtu/minmax.h>
13 #include <libextl/extl.h>
16 #include "rectangle.h"
19 void rectangle_constrain(WRectangle *g, const WRectangle *bounds)
21 const WRectangle tmpg=*g;
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);
30 bool rectangle_contains(const WRectangle *g, int x, int y)
32 return (x>=g->x && x<g->x+g->w && y>=g->y && y<g->y+g->h);
36 void rectangle_debugprint(const WRectangle *g, const char *n)
38 fprintf(stderr, "%s %d, %d; %d, %d\n", n, g->x, g->y, g->w, g->h);
42 int rectangle_compare(const WRectangle *g, const WRectangle *h)
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));