File: rectangle.c

package info (click to toggle)
notion 4.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,656 kB
  • sloc: ansic: 47,365; sh: 2,093; makefile: 594; perl: 270
file content (63 lines) | stat: -rw-r--r-- 1,633 bytes parent folder | download | duplicates (3)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * ion/ioncore/rectangle.c
 *
 * Copyright (c) Tuomo Valkonen 1999-2007.
 *
 * See the included file LICENSE for details.
 */

#include <libtu/minmax.h>
#include <libextl/extl.h>

#include "common.h"
#include "rectangle.h"


void rectangle_constrain(WRectangle *g, const WRectangle *bounds)
{
    const WRectangle tmpg=*g;

    g->x=MINOF(MAXOF(tmpg.x, bounds->x), tmpg.x+tmpg.w-1);
    g->y=MINOF(MAXOF(tmpg.y, bounds->y), tmpg.y+tmpg.h-1);
    g->w=MAXOF(1, MINOF(bounds->x+bounds->w, tmpg.x+tmpg.w)-g->x);
    g->h=MAXOF(1, MINOF(bounds->y+bounds->h, tmpg.y+tmpg.h)-g->y);
}

void rectangle_clamp_or_center(WRectangle *g, const WRectangle *bounds)
{
    if(g->w>bounds->w){
        g->x=(bounds->x+(bounds->w/2))-(g->w/2);
    }else if(g->x<bounds->x){
        g->x=bounds->x;
    }else if(g->x+g->w>bounds->x+bounds->w){
        g->x=(bounds->x+bounds->w)-g->w;
    }

    if(g->h>bounds->h){
        g->y=(bounds->y+(bounds->h/2))-(g->h/2);
    }else if(g->y<bounds->y){
        g->y=bounds->y;
    }else if(g->y+g->h>bounds->y+bounds->h){
        g->y=(bounds->y+bounds->h)-g->h;
    }
}

bool rectangle_contains(const WRectangle *g, int x, int y)
{
    return (x>=g->x && x<g->x+g->w && y>=g->y && y<g->y+g->h);
}


void rectangle_debugprint(const WRectangle *g, const char *n)
{
    fprintf(stderr, "%s %d, %d; %d, %d\n", n, g->x, g->y, g->w, g->h);
}


int rectangle_compare(const WRectangle *g, const WRectangle *h)
{
    return ((g->x!=h->x ? RECTANGLE_X_DIFF : 0) |
            (g->y!=h->y ? RECTANGLE_Y_DIFF : 0) |
            (g->w!=h->w ? RECTANGLE_W_DIFF : 0) |
            (g->h!=h->h ? RECTANGLE_H_DIFF : 0));
}