File: rect.c

package info (click to toggle)
xview 3.2p1.4-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 20,068 kB
  • ctags: 24,304
  • sloc: ansic: 241,105; yacc: 1,392; sh: 1,140; makefile: 273; lex: 76; perl: 54; asm: 50; cpp: 15
file content (192 lines) | stat: -rw-r--r-- 4,346 bytes parent folder | download | duplicates (9)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef lint
#ifdef sccs
static char     sccsid[] = "@(#)rect.c 20.17 93/06/28 Copyr 1984 Sun Micro";
#endif
#endif

/*
 *	(c) Copyright 1989 Sun Microsystems, Inc. Sun design patents 
 *	pending in the U.S. and foreign countries. See LEGAL NOTICE 
 *	file for terms of the license.
 */

/*
 * Overview:	Implements the interface described by rect.h which defines
 * the rectangle structure
 */

#include <stdio.h>
#include <xview_private/i18n_impl.h>
#include <xview/base.h>		/* HUH? xv_error.h also does this */
#include <xview/rect.h>
#include <xview/xv_error.h>

/*
 * Rect geometry functions
 */
rect_intersection(r1, r2, r)
    register struct rect *r1, *r2, *r;
{
    r->r_left = MAX(r1->r_left, r2->r_left);
    r->r_top = MAX(r1->r_top, r2->r_top);
    r->r_width = MIN(r1->r_left + r1->r_width, r2->r_left + r2->r_width) -
	r->r_left;
    if (r->r_width < 0)
	r->r_width = 0;
    r->r_height = MIN(r1->r_top + r1->r_height, r2->r_top + r2->r_height) -
	r->r_top;
    if (r->r_height < 0)
	r->r_height = 0;
    return;
}

unsigned
rect_clipvector(r, x1arg, y1arg, x2arg, y2arg)
    register struct rect *r;
    int            *x1arg, *y1arg, *x2arg, *y2arg;
{				/* clip vector 1=>2 to r, return TRUE if
				 * visible. Cohen-Sutherland algorithm. */
    register short  x1 = *x1arg, y1 = *y1arg, x2 = *x2arg, y2 = *y2arg;
    register char   bits1, bits2, bits;
    coord           t, n, d, p;
    unsigned        done, accept;

    done = FALSE;
    do {
	bits1 = 0;
	if (y1 < r->r_top)
	    bits1 |= 1;
	if (y1 > rect_bottom(r))
	    bits1 |= 2;
	if (x1 > rect_right(r))
	    bits1 |= 4;
	if (x1 < r->r_left)
	    bits1 |= 8;
	bits2 = 0;
	if (y2 < r->r_top)
	    bits2 |= 1;
	if (y2 > rect_bottom(r))
	    bits2 |= 2;
	if (x2 > rect_right(r))
	    bits2 |= 4;
	if (x2 < r->r_left)
	    bits2 |= 8;

	accept = ((bits1 | bits2) == 0);
	if (accept)
	    done = TRUE;	/* vector all inside clip volume */
	else {
	    accept = ((bits1 & bits2) == 0);
	    if (!accept)
		done = TRUE;	/* vec all outside clip vol  */
	    else {		/* vector needs clip	 */
		if (bits1 == 0) {	/* make sure v1 is outside	 */
		    p = x1;
		    x1 = x2;
		    x2 = p;
		    p = y1;
		    y1 = y2;
		    y2 = p;
		    bits = bits1;
		    bits1 = bits2;
		    bits2 = bits;
		}
		if (bits1 & 1) {/* clip above	 */
		    n = (r->r_top - y1);
		    d = (y2 - y1);
		    t = (x2 - x1);
		    t *= n;
		    t /= d;
		    x1 = x1 + t;
		    y1 = r->r_top;
		} else if (bits1 & 2) {	/* clip below	 */
		    n = (rect_bottom(r) - y1);
		    d = (y2 - y1);
		    t = (x2 - x1);
		    t *= n;
		    t /= d;
		    x1 = x1 + t;
		    y1 = rect_bottom(r);
		} else if (bits1 & 4) {	/* clip right	 */
		    n = (rect_right(r) - x1);
		    d = (x2 - x1);
		    t = (y2 - y1);
		    t *= n;
		    t /= d;
		    y1 = y1 + t;
		    x1 = rect_right(r);
		} else if (bits1 & 8) {	/* clip r_left	 */
		    n = (r->r_left - x1);
		    d = (x2 - x1);
		    t = (y2 - y1);
		    t *= n;
		    t /= d;
		    y1 = y1 + t;
		    x1 = r->r_left;
		}
	    }
	}
    } while (!done);
    *x1arg = x1;
    *y1arg = y1;
    *x2arg = x2;
    *y2arg = y2;
    return (accept);
}

unsigned
rect_order(r1, r2, sortorder)
    struct rect    *r1, *r2;
    int             sortorder;
{				/* Return true if r1 & r2 are in the
				 * specified relationship. */
    switch (sortorder) {
      case RECTS_TOPTOBOTTOM:
	if (r1->r_top <= r2->r_top)
	    return (TRUE);
	break;
      case RECTS_BOTTOMTOTOP:
	if (rect_bottom(r1) >= rect_bottom(r2))
	    return (TRUE);
	break;
      case RECTS_LEFTTORIGHT:
	if (r1->r_left <= r2->r_left)
	    return (TRUE);
	break;
      case RECTS_RIGHTTOLEFT:
	if (rect_right(r1) >= rect_right(r2))
	    return (TRUE);
	break;
      case RECTS_UNSORTED:
	return (TRUE);
	break;
      default:
	xv_error(NULL,
		 ERROR_STRING, 
		 XV_MSG("Bad sortorder arg in mostRect"),
		 0);
	break;
    }
    return (FALSE);
}

struct rect
rect_bounding(r1, r2)
    register struct rect *r1, *r2;
{
    struct rect     r;

    if (rect_isnull(r1))
	r = *r2;
    else if (rect_isnull(r2))
	r = *r1;
    else {
	r.r_left = MIN(r1->r_left, r2->r_left);
	r.r_top = MIN(r1->r_top, r2->r_top);
	r.r_width = MAX(r1->r_left + r1->r_width, r2->r_left + r2->r_width)
	    - r.r_left;
	r.r_height = MAX(r1->r_top + r1->r_height, r2->r_top + r2->r_height)
	    - r.r_top;
    }
    return (r);
}