added .cvsignore files
[btb/d2x.git] / 2d / clip.h
1 /* $Id: clip.h,v 1.2 2002-07-17 21:55:19 bradleyb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
12 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14 /*
15  *
16  * Macros used for clipping
17  *
18  * Old Log:
19  *
20  * Revision 1.3  1993/10/15  16:22:28  john
21  * *** empty log message ***
22  * 
23  * Revision 1.2  1993/09/29  16:14:31  john
24  * made work with fix point
25  * 
26  * Revision 1.1  1993/09/08  11:40:53  john
27  * Initial revision
28  * 
29  *
30  */
31
32
33 /*#define MIN(a,b)        (((a) < (b)) ? (a) : (b))
34 #define MAX(a,b)        (((a) > (b)) ? (a) : (b))
35 */
36 #define IABS(x)         (((x) < 0) ?  -(x) : (x))
37
38 #define EXCHG(a,b) do {                                                 \
39     int __temp__ = (a);                                                 \
40     (a) = (b);                                                          \
41     (b) = __temp__;                                                     \
42 } while(0)
43
44 // sort two values
45 #define SORT2(a,b) do {                                                 \
46     if((a) > (b)) EXCHG(a,b);                                           \
47 } while(0)
48
49 # define SCALE(var,arg,num,den)                                         \
50     ((var) = ((arg) * (num)) / (den))
51
52 # define USCALE(var,arg,num,den)                                        \
53         ((var) = ((unsigned)(arg) * (unsigned)(num)) / (unsigned)(den))
54
55 # define FSCALE(var,arg,num,den) ((var) = fixmuldiv((arg),(num),(den)))
56
57 #define CLIPDOT(xx,yy,WHEN_OUTSIDE,WHEN_CLIPPED) {                      \
58     if((xx > XMAX) || (xx < XMIN)) { WHEN_OUTSIDE; }                    \
59     if((yy > YMAX) || (yy < YMIN)) { WHEN_OUTSIDE; }                    \
60 }
61
62 #define CLIPHLINE(x1,x2,yy,WHEN_OUTSIDE,WHEN_CLIPPED) {                 \
63     if(x1 > x2) EXCHG(x1,x2);                                           \
64     if((x1 > XMAX) || (x2 < XMIN)) { WHEN_OUTSIDE; }                    \
65     if((yy > YMAX) || (yy < YMIN)) { WHEN_OUTSIDE; }                    \
66     if(x1 < XMIN) { x1 = XMIN; WHEN_CLIPPED; }                          \
67     if(x2 > XMAX) { x2 = XMAX; WHEN_CLIPPED; }                          \
68 }
69
70 #define CLIPVLINE(xx,y1,y2,WHEN_OUTSIDE,WHEN_CLIPPED) {                 \
71     if(y1 > y2) EXCHG(y1,y2);                                           \
72     if((xx > XMAX) || (xx < XMIN)) { WHEN_OUTSIDE; }                    \
73     if((y1 > YMAX) || (y2 < YMIN)) { WHEN_OUTSIDE; }                    \
74     if(y1 < YMIN) { y1 = YMIN; WHEN_CLIPPED; }                          \
75     if(y2 > YMAX) { y2 = YMAX; WHEN_CLIPPED; }                          \
76 }
77
78 #define CLIPBOX(x1,y1,x2,y2,WHEN_OUTSIDE,WHEN_CLIPPED) {                \
79     if(x1 > x2) EXCHG(x1,x2);                                           \
80     if(y1 > y2) EXCHG(y1,y2);                                           \
81     if((x1 > XMAX) || (x2 < 0)) { WHEN_OUTSIDE; }                       \
82     if((y1 > YMAX) || (y2 < 0)) { WHEN_OUTSIDE; }                       \
83     if(x1 < 0) { x1 = 0; WHEN_CLIPPED; }                                \
84     if(y1 < 0) { y1 = 0; WHEN_CLIPPED; }                                \
85     if(x2 > XMAX) { x2 = XMAX; WHEN_CLIPPED; }                          \
86     if(y2 > YMAX) { y2 = YMAX; WHEN_CLIPPED; }                          \
87 }
88     
89 #define CLIPLINE(x1,y1,x2,y2,XMIN,YMIN,XMAX,YMAX,WHEN_OUTSIDE,WHEN_CLIPPED,MY_SCALE) do {       \
90     register int temp;                                                  \
91     if(y1 > y2)                                                         \
92         { EXCHG(y1,y2); EXCHG(x1,x2); }                                 \
93     if((y2 < YMIN) || (y1 > YMAX))                                      \
94         { WHEN_OUTSIDE; }                                               \
95     if(x1 < x2) {                                                       \
96         if((x2 < XMIN) || (x1 > XMAX)) {                                \
97             WHEN_OUTSIDE;                                               \
98         }                                                               \
99         if(x1 < XMIN) {                                                 \
100                         MY_SCALE(temp,(y2 - y1),(XMIN - x1),(x2 - x1)); \
101             if((y1 += temp) > YMAX) { WHEN_OUTSIDE; }                   \
102             x1 = XMIN;                                                  \
103             WHEN_CLIPPED;                                               \
104         }                                                               \
105         if(x2 > XMAX) {                                                 \
106                         MY_SCALE(temp,(y2 - y1),(x2 - XMAX),(x2 - x1)); \
107             if((y2 -= temp) < YMIN) { WHEN_OUTSIDE; }                   \
108             x2 = XMAX;                                                  \
109             WHEN_CLIPPED;                                               \
110         }                                                               \
111         if(y1 < YMIN) {                                                 \
112                         MY_SCALE(temp,(x2 - x1),(YMIN - y1),(y2 - y1)); \
113             x1 += temp;                                                 \
114             y1 = YMIN;                                                  \
115             WHEN_CLIPPED;                                               \
116         }                                                               \
117         if(y2 > YMAX) {                                                 \
118                         MY_SCALE(temp,(x2 - x1),(y2 - YMAX),(y2 - y1)); \
119             x2 -= temp;                                                 \
120             y2 = YMAX;                                                  \
121             WHEN_CLIPPED;                                               \
122         }                                                               \
123     }                                                                   \
124     else {                                                              \
125         if((x1 < XMIN) || (x2 > XMAX)) {                                \
126             WHEN_OUTSIDE;                                               \
127         }                                                               \
128         if(x1 > XMAX) {                                                 \
129                         MY_SCALE(temp,(y2 - y1),(x1 - XMAX),(x1 - x2)); \
130             if((y1 += temp) > YMAX) { WHEN_OUTSIDE; }                   \
131             x1 = XMAX;                                                  \
132             WHEN_CLIPPED;                                               \
133         }                                                               \
134         if(x2 < XMIN) {                                                 \
135                         MY_SCALE(temp,(y2 - y1),(XMIN - x2),(x1 - x2)); \
136             if((y2 -= temp) < YMIN) { WHEN_OUTSIDE; }                   \
137             x2 = XMIN;                                                  \
138             WHEN_CLIPPED;                                               \
139         }                                                               \
140         if(y1 < YMIN) {                                                 \
141                         MY_SCALE(temp,(x1 - x2),(YMIN - y1),(y2 - y1)); \
142             x1 -= temp;                                                 \
143             y1 = YMIN;                                                  \
144             WHEN_CLIPPED;                                               \
145         }                                                               \
146         if(y2 > YMAX) {                                                 \
147                         MY_SCALE(temp,(x1 - x2),(y2 - YMAX),(y2 - y1)); \
148             x2 += temp;                                                 \
149             y2 = YMAX;                                                  \
150             WHEN_CLIPPED;                                               \
151         }                                                               \
152     }                                                                   \
153 } while(0)