summaryrefslogtreecommitdiffstats
path: root/generic/tkQTreePriv.h
blob: 81cae218b6781cf0d4d869e9ab8cb59cefe5e998 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * tkQTreePriv.h --
 *
 *	Private implementation for Q-Tree.
 *
 * Copyright (c) 2015-2017 Gregor Cramer
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#ifndef _TKQTREE
# error "do not include this private header file"
#endif


#ifdef _TK_NEED_IMPLEMENTATION

#include <assert.h>

#ifdef _MSC_VER
# if _MSC_VER >= 1900
#  define inline __inline
# else
#  define inline
# endif
#elif __STDC_VERSION__ < 199901L
# define inline /* we are not C99 conform */
#endif


inline
bool
TkQTreeRectIsEmpty(
    const TkQTreeRect *rect)
{
    assert(rect);

    /*
     * Normally this natural statement will be used here:
     *
     *     return rect->xmin >= rect->xmax || rect->ymin >= rect->ymax;
     *
     * GCC gives the following warning when optimization is enabled:
     *
     *     assuming signed overflow does not occur when assuming that (X + c) < X
     *     is always false [-Wstrict-overflow]
     *
     * I guess that GCC is converting (optimizing) this statement into:
     *
     *     return rect->xmin - rect->xmax >= 0 || rect->ymin - rect->ymax >= 0;
     *
     * So I will use the latter statement to avoid this warning. Unfortunately
     * this has the drawback that on machines with different architectures
     * the transformed statement can be a bit slower than the natural statement,
     * but I will assume that on such architectures GCC will transform it (back)
     * to the natural one (when optimization is enabled), without giving a warning,
     * because the natural statement doesn't use arithmetic.
     *
     * Note that this problem only happen with signed values.
     */

    return rect->xmin - rect->xmax >= 0 || rect->ymin - rect->ymax >= 0;
}


inline
bool
TkQTreeRectIsEqual(
    const TkQTreeRect *rect1,
    const TkQTreeRect *rect2)
{
    assert(rect1);
    assert(rect2);

    return rect1->xmin == rect2->xmin
    	&& rect1->xmax == rect2->xmax
	&& rect1->ymin == rect2->ymin
	&& rect1->ymax == rect2->ymax;
}


inline
bool
TkQTreeRectContainsPoint(
    const TkQTreeRect *rect,
    TkQTreeCoord x,
    TkQTreeCoord y)
{
    assert(rect);

    return y < rect->ymax && rect->ymin <= y && x < rect->xmax && rect->xmin <= x;
}


inline
bool
TkQTreeRectContainsRect(
    const TkQTreeRect *rect1,
    const TkQTreeRect *rect2)
{
    assert(rect1);
    assert(rect2);

    return rect1->xmin <= rect2->xmin
    	&& rect2->xmax <= rect1->xmax
	&& rect1->ymin <= rect2->ymin
	&& rect2->ymax <= rect1->ymax;
}


inline
bool
TkQTreeRectIntersects(
    const TkQTreeRect *rect1,
    const TkQTreeRect *rect2)
{
    assert(rect1);
    assert(rect2);

    return rect1->xmin < rect2->xmax
	&& rect2->xmin < rect1->xmax
	&& rect1->ymin < rect2->ymax
	&& rect2->ymin < rect1->ymax;
}


inline
TkQTreeRect *
TkQTreeRectSet(
    TkQTreeRect *rect,
    TkQTreeCoord xmin,
    TkQTreeCoord ymin,
    TkQTreeCoord xmax,
    TkQTreeCoord ymax)
{
    assert(rect);
    assert(xmin <= xmax);
    assert(ymin <= ymax);

    rect->xmin = xmin;
    rect->ymin = ymin;
    rect->xmax = xmax;
    rect->ymax = ymax;

    return rect;
}


inline
TkQTreeRect *
TkQTreeRectTranslate(
    TkQTreeRect *rect,
    TkQTreeCoord dx,
    TkQTreeCoord dy)
{
    assert(rect);

    rect->xmin += dx;
    rect->xmax += dx;
    rect->ymin += dy;
    rect->ymax += dy;

    return rect;
}

#undef _TK_NEED_IMPLEMENTATION
#endif /* _TK_NEED_IMPLEMENTATION */
/* vi:set ts=8 sw=4: */