Jspice3
mfblclip.c
Go to the documentation of this file.
1 /*************************************************************************
2  MFB graphics and miscellaneous library
3  Copyright (c) Stephen R. Whiteley 1992
4  Author: Stephen R. Whiteley
5  *************************************************************************/
6 
7 #include "mfb.h"
8 #if defined(MSDOS) || __MSDOS__
9 #include "mfbP.h"
10 #endif
11 
12 
13 /* Line clipping routines.
14  * Note that these routines operate with long integers */
15 
16 
17 #define CODELEFT 1
18 #define CODEBOTTOM 2
19 #define CODERIGHT 4
20 #define CODETOP 8
21 #define CODE(x,y,c) c = 0;\
22  if(x < l)\
23  c = CODELEFT;\
24  else if(x > r)\
25  c = CODERIGHT;\
26  if(y < b)\
27  c |= CODEBOTTOM;\
28  else if(y > t)\
29  c |= CODETOP;
30 
31 void
32 MFB_Y_Intercept(x1,y1,x2,y2,e,yi)
33 
34 long x1,y1,x2,y2; /* two points on line */
35 long e; /* vertical line of intercept */
36 long *yi; /* y coordinate of intercept */
37 {
38  /*
39  * MFB_Y_Intercept will return the value 'yi' where the the coordinate
40  * (e,yi) is the intersection of the vertical line x = e and the line
41  * determined by the coordinates (x1,y1) and (x2,y2).
42  */
43  *yi = y1;
44  if (x1 == x2) return; /* vertical line */
45  *yi = y1 + ((e - x1) * (y2 - y1))/(x2 - x1);
46 }
47 
48 
49 void
50 MFB_X_Intercept(x1,y1,x2,y2,e,xi)
51 
52 long x1,y1,x2,y2; /* two polongs on line */
53 long e; /* horizontal line of longercept */
54 long *xi; /* x coordinate of longercept */
55 {
56  /*
57  * MFB_X_Intercept will return the value 'xi' where the the coordinate
58  * (xi,e) is the intersection of the horizontal line y = e and the line
59  * determined by the coordinates (x1,y1) and (x2,y2).
60  */
61  *xi = x1;
62  if (y1 == y2) return; /* horizontal line */
63  *xi = x1 + ((e - y1) * (x2 - x1))/(y2 - y1);
64 }
65 
66 
67 Bool
68 MFBLineClip(pX1,pY1,pX2,pY2,l,b,r,t)
69 
70 long *pX1,*pY1,*pX2,*pY2,l,b,r,t;
71 {
72  /*
73  * MFBLineClip will clip a line to a rectangular area. The returned
74  * value is 'true' if the line is out of the AOI (therefore does not
75  * need to be displayed) and 'false' if the line is in the AOI.
76  */
77  long x1 = *pX1;
78  long y1 = *pY1;
79  long x2 = *pX2;
80  long y2 = *pY2;
81  long x,y;
82  int c,c1,c2;
83 
84  CODE(x1,y1,c1)
85  CODE(x2,y2,c2)
86  while (c1 != 0 || c2 != 0) {
87  if ((c1 & c2) != 0) return (true); /* Line is invisible. */
88  if ((c = c1) == 0) c = c2;
89  if (c & CODELEFT) {
90  y = y1+(y2-y1)*(l-x1)/(x2-x1);
91  x = l;
92  }
93  else if (c & CODERIGHT) {
94  y = y1+(y2-y1)*(r-x1)/(x2-x1);
95  x = r;
96  }
97  else if (c & CODEBOTTOM) {
98  x = x1+(x2-x1)*(b-y1)/(y2-y1);
99  y = b;
100  }
101  else if (c & CODETOP) {
102  x = x1+(x2-x1)*(t-y1)/(y2-y1);
103  y = t;
104  }
105  if (c == c1) {
106  x1 = x; y1 = y; CODE(x,y,c1)
107  }
108  else {
109  x2 = x; y2 = y; CODE(x,y,c2)
110  }
111  }
112  *pX1 = x1; *pY1 = y1;
113  *pX2 = x2; *pY2 = y2;
114  return (false); /* Line is at least partially visible. */
115 }
#define CODERIGHT
Definition: mfblclip.c:19
static double e
Definition: vectors.c:17
Bool MFBLineClip(long *pX1, long *pY1, long *pX2, long *pY2, long l, long b, long r, long t)
Definition: mfblclip.c:68
Definition: cddefs.h:312
#define CODETOP
Definition: mfblclip.c:20
void MFB_X_Intercept(long x1, long y1, long x2, long y2, long e, long *xi)
Definition: mfblclip.c:50
Bool
Definition: mfb.h:18
static double c
Definition: vectors.c:16
void MFB_Y_Intercept(long x1, long y1, long x2, long y2, long e, long *yi)
Definition: mfblclip.c:32
#define CODEBOTTOM
Definition: mfblclip.c:18
#define CODELEFT
Definition: mfblclip.c:17
#define CODE(x, y, c)
Definition: mfblclip.c:21
Definition: cddefs.h:162
Definition: cddefs.h:192