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