Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
466 lines (412 loc) · 11.2 KB

SDL_rect.c

File metadata and controls

466 lines (412 loc) · 11.2 KB
 
Jun 22, 2011
Jun 22, 2011
1
2
/*
Simple DirectMedia Layer
Dec 31, 2011
Dec 31, 2011
3
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
Jun 22, 2011
Jun 22, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
#include "SDL_rect.h"
Oct 31, 2011
Oct 31, 2011
24
#include "SDL_rect_c.h"
Jun 22, 2011
Jun 22, 2011
25
26
27
28
29
30
31
SDL_bool
SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
{
int Amin, Amax, Bmin, Bmax;
Sep 5, 2011
Sep 5, 2011
32
33
34
35
36
if (!A || !B) {
// TODO error message
return SDL_FALSE;
}
Sep 18, 2011
Sep 18, 2011
37
38
39
40
41
/* Special cases for empty rects */
if (SDL_RectEmpty(A) || SDL_RectEmpty(B)) {
return SDL_FALSE;
}
Jun 22, 2011
Jun 22, 2011
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
/* Horizontal intersection */
Amin = A->x;
Amax = Amin + A->w;
Bmin = B->x;
Bmax = Bmin + B->w;
if (Bmin > Amin)
Amin = Bmin;
if (Bmax < Amax)
Amax = Bmax;
if (Amax <= Amin)
return SDL_FALSE;
/* Vertical intersection */
Amin = A->y;
Amax = Amin + A->h;
Bmin = B->y;
Bmax = Bmin + B->h;
if (Bmin > Amin)
Amin = Bmin;
if (Bmax < Amax)
Amax = Bmax;
if (Amax <= Amin)
return SDL_FALSE;
return SDL_TRUE;
}
SDL_bool
SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
{
int Amin, Amax, Bmin, Bmax;
Sep 5, 2011
Sep 5, 2011
74
75
76
77
78
if (!A || !B || !result) {
// TODO error message
return SDL_FALSE;
}
Sep 18, 2011
Sep 18, 2011
79
80
81
82
83
/* Special cases for empty rects */
if (SDL_RectEmpty(A) || SDL_RectEmpty(B)) {
return SDL_FALSE;
}
Jun 22, 2011
Jun 22, 2011
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
/* Horizontal intersection */
Amin = A->x;
Amax = Amin + A->w;
Bmin = B->x;
Bmax = Bmin + B->w;
if (Bmin > Amin)
Amin = Bmin;
result->x = Amin;
if (Bmax < Amax)
Amax = Bmax;
result->w = Amax - Amin;
/* Vertical intersection */
Amin = A->y;
Amax = Amin + A->h;
Bmin = B->y;
Bmax = Bmin + B->h;
if (Bmin > Amin)
Amin = Bmin;
result->y = Amin;
if (Bmax < Amax)
Amax = Bmax;
result->h = Amax - Amin;
return !SDL_RectEmpty(result);
}
void
SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
{
int Amin, Amax, Bmin, Bmax;
Sep 5, 2011
Sep 5, 2011
116
117
118
119
if (!A || !B || !result) {
return;
}
Sep 18, 2011
Sep 18, 2011
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Special cases for empty Rects */
if (SDL_RectEmpty(A)) {
if (SDL_RectEmpty(B)) {
/* A and B empty */
return;
} else {
/* A empty, B not empty */
*result = *B;
return;
}
} else {
if (SDL_RectEmpty(B)) {
/* A not empty, B empty */
*result = *A;
return;
}
}
Jun 22, 2011
Jun 22, 2011
138
139
140
141
142
143
144
145
146
147
148
149
/* Horizontal union */
Amin = A->x;
Amax = Amin + A->w;
Bmin = B->x;
Bmax = Bmin + B->w;
if (Bmin < Amin)
Amin = Bmin;
result->x = Amin;
if (Bmax > Amax)
Amax = Bmax;
result->w = Amax - Amin;
Sep 18, 2011
Sep 18, 2011
150
/* Vertical union */
Jun 22, 2011
Jun 22, 2011
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
Amin = A->y;
Amax = Amin + A->h;
Bmin = B->y;
Bmax = Bmin + B->h;
if (Bmin < Amin)
Amin = Bmin;
result->y = Amin;
if (Bmax > Amax)
Amax = Bmax;
result->h = Amax - Amin;
}
SDL_bool
SDL_EnclosePoints(const SDL_Point * points, int count, const SDL_Rect * clip,
SDL_Rect * result)
{
int minx = 0;
int miny = 0;
int maxx = 0;
int maxy = 0;
int x, y, i;
Sep 12, 2011
Sep 12, 2011
173
if (!points) {
Sep 18, 2011
Sep 18, 2011
174
/* TODO error message */
Sep 5, 2011
Sep 5, 2011
175
176
177
return SDL_FALSE;
}
Jun 22, 2011
Jun 22, 2011
178
if (count < 1) {
Sep 18, 2011
Sep 18, 2011
179
/* TODO error message */
Jun 22, 2011
Jun 22, 2011
180
181
182
183
return SDL_FALSE;
}
if (clip) {
Sep 18, 2011
Sep 18, 2011
184
185
186
187
188
189
SDL_bool added = SDL_FALSE;
const int clip_minx = clip->x;
const int clip_miny = clip->y;
const int clip_maxx = clip->x+clip->w-1;
const int clip_maxy = clip->y+clip->h-1;
Sep 18, 2011
Sep 18, 2011
190
191
192
193
194
/* Special case for empty rectangle */
if (SDL_RectEmpty(clip)) {
return SDL_FALSE;
}
Jun 22, 2011
Jun 22, 2011
195
196
197
198
199
200
201
202
203
for (i = 0; i < count; ++i) {
x = points[i].x;
y = points[i].y;
if (x < clip_minx || x > clip_maxx ||
y < clip_miny || y > clip_maxy) {
continue;
}
if (!added) {
Sep 12, 2011
Sep 12, 2011
204
205
206
207
208
209
/* Special case: if no result was requested, we are done */
if (result == NULL) {
return SDL_TRUE;
}
/* First point added */
Jun 22, 2011
Jun 22, 2011
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
minx = maxx = x;
miny = maxy = y;
added = SDL_TRUE;
continue;
}
if (x < minx) {
minx = x;
} else if (x > maxx) {
maxx = x;
}
if (y < miny) {
miny = y;
} else if (y > maxy) {
maxy = y;
}
}
if (!added) {
return SDL_FALSE;
}
} else {
Sep 12, 2011
Sep 12, 2011
230
231
232
233
234
/* Special case: if no result was requested, we are done */
if (result == NULL) {
return SDL_TRUE;
}
Jun 22, 2011
Jun 22, 2011
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* No clipping, always add the first point */
minx = maxx = points[0].x;
miny = maxy = points[0].y;
for (i = 1; i < count; ++i) {
x = points[i].x;
y = points[i].y;
if (x < minx) {
minx = x;
} else if (x > maxx) {
maxx = x;
}
if (y < miny) {
miny = y;
} else if (y > maxy) {
maxy = y;
}
}
}
if (result) {
result->x = minx;
result->y = miny;
result->w = (maxx-minx)+1;
result->h = (maxy-miny)+1;
}
return SDL_TRUE;
}
/* Use the Cohen-Sutherland algorithm for line clipping */
#define CODE_BOTTOM 1
#define CODE_TOP 2
#define CODE_LEFT 4
#define CODE_RIGHT 8
static int ComputeOutCode(const SDL_Rect * rect, int x, int y)
{
int code = 0;
if (y < 0) {
code |= CODE_TOP;
} else if (y >= rect->y + rect->h) {
code |= CODE_BOTTOM;
}
if (x < 0) {
code |= CODE_LEFT;
} else if (x >= rect->x + rect->w) {
code |= CODE_RIGHT;
}
return code;
}
SDL_bool
SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
int *Y2)
{
int x = 0;
int y = 0;
int x1, y1;
int x2, y2;
int rectx1;
int recty1;
int rectx2;
int recty2;
int outcode1, outcode2;
if (!rect || !X1 || !Y1 || !X2 || !Y2) {
Sep 5, 2011
Sep 5, 2011
302
// TODO error message
Jun 22, 2011
Jun 22, 2011
303
304
305
return SDL_FALSE;
}
Sep 18, 2011
Sep 18, 2011
306
307
308
309
310
/* Special case for empty rect */
if (SDL_RectEmpty(rect)) {
return SDL_FALSE;
}
Jun 22, 2011
Jun 22, 2011
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
x1 = *X1;
y1 = *Y1;
x2 = *X2;
y2 = *Y2;
rectx1 = rect->x;
recty1 = rect->y;
rectx2 = rect->x + rect->w - 1;
recty2 = rect->y + rect->h - 1;
/* Check to see if entire line is inside rect */
if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
return SDL_TRUE;
}
/* Check to see if entire line is to one side of rect */
if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
(y1 < recty1 && y2 < recty1) || (y1 > recty2 && y2 > recty2)) {
return SDL_FALSE;
}
if (y1 == y2) {
/* Horizontal line, easy to clip */
if (x1 < rectx1) {
*X1 = rectx1;
} else if (x1 > rectx2) {
*X1 = rectx2;
}
if (x2 < rectx1) {
*X2 = rectx1;
} else if (x2 > rectx2) {
*X2 = rectx2;
}
return SDL_TRUE;
}
if (x1 == x2) {
/* Vertical line, easy to clip */
if (y1 < recty1) {
*Y1 = recty1;
} else if (y1 > recty2) {
*Y1 = recty2;
}
if (y2 < recty1) {
*Y2 = recty1;
} else if (y2 > recty2) {
*Y2 = recty2;
}
return SDL_TRUE;
}
/* More complicated Cohen-Sutherland algorithm */
outcode1 = ComputeOutCode(rect, x1, y1);
outcode2 = ComputeOutCode(rect, x2, y2);
while (outcode1 || outcode2) {
if (outcode1 & outcode2) {
return SDL_FALSE;
}
if (outcode1) {
if (outcode1 & CODE_TOP) {
y = recty1;
x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
} else if (outcode1 & CODE_BOTTOM) {
y = recty2;
x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
} else if (outcode1 & CODE_LEFT) {
x = rectx1;
y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
} else if (outcode1 & CODE_RIGHT) {
x = rectx2;
y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
}
x1 = x;
y1 = y;
outcode1 = ComputeOutCode(rect, x, y);
} else {
if (outcode2 & CODE_TOP) {
y = recty1;
x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
} else if (outcode2 & CODE_BOTTOM) {
y = recty2;
x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
} else if (outcode2 & CODE_LEFT) {
x = rectx1;
y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
} else if (outcode2 & CODE_RIGHT) {
x = rectx2;
y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
}
x2 = x;
y2 = y;
outcode2 = ComputeOutCode(rect, x, y);
}
}
*X1 = x1;
*Y1 = y1;
*X2 = x2;
*Y2 = y2;
return SDL_TRUE;
}
SDL_bool
SDL_GetSpanEnclosingRect(int width, int height,
int numrects, SDL_Rect * rects, SDL_Rect *span)
{
int i;
int span_y1, span_y2;
int rect_y1, rect_y2;
Sep 5, 2011
Sep 5, 2011
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
if (width < 1 || height < 1) {
// TODO error message
return SDL_FALSE;
}
if (!rects || !span) {
// TODO error message
return SDL_FALSE;
}
if (numrects < 1) {
// TODO error message
return SDL_FALSE;
}
Jun 22, 2011
Jun 22, 2011
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/* Initialize to empty rect */
span_y1 = height;
span_y2 = 0;
for (i = 0; i < numrects; ++i) {
rect_y1 = rects[i].y;
rect_y2 = rect_y1 + rects[i].h;
/* Clip out of bounds rectangles, and expand span rect */
if (rect_y1 < 0) {
span_y1 = 0;
} else if (rect_y1 < span_y1) {
span_y1 = rect_y1;
}
if (rect_y2 > height) {
span_y2 = height;
} else if (rect_y2 > span_y2) {
span_y2 = rect_y2;
}
}
if (span_y2 > span_y1) {
span->x = 0;
span->y = span_y1;
span->w = width;
span->h = (span_y2 - span_y1);
return SDL_TRUE;
}
return SDL_FALSE;
}
/* vi: set ts=4 sw=4 expandtab: */