The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4394 ripley 1
/*
2
 * GraphApp - Cross-Platform Graphics Programming Library.
3
 *
4
 * File: arith.c -- point and rectangle arithmetic.
5
 * Platform: Neutral  Version: 2.40  Date: 1998/05/05
6
 *
7
 * Version: 1.00  Changes: Original version by Lachlan Patrick.
8
 * Version: 1.04  Changes: Put the rcanon function back in.
9
 * Version: 1.50  Changes: Change to rect structure & functions.
10
 * Version: 2.40  Changes: Fix for rinr and clipr by Angus North.
11
 */
12
 
13
/* Copyright (C) 1993-1998 Lachlan Patrick
14
 
15
   This file is part of GraphApp, a cross-platform C graphics library.
16
 
17
   GraphApp is free software; you can redistribute it and/or modify it
18
   under the terms of the GNU Library General Public License.
19
   GraphApp is distributed in the hope that it will be useful, but
20
   WITHOUT ANY WARRANTY.
21
 
22
   See the file COPYLIB.TXT for details.
23
*/
24
 
25
#include "graphapp.h"
26
 
27
point newpoint(int x, int y)
28
{
29
	point p;
30
 
31
	p.x = x;
32
	p.y = y;
33
	return p;
34
}
35
 
36
rect newrect(int x, int y, int width, int height)
37
{
38
	rect r;
39
 
40
	r.x = x;
41
	r.y = y;
42
	r.width = width;
43
	r.height = height;
44
	return r;
45
}
46
 
47
rect rpt(point min, point max)
48
{
49
	rect r;
50
 
51
	r.x = min.x;
52
	r.y  = min.y;
53
	r.width = max.x - min.x;
54
	r.height = max.y - min.y;
55
	return r;
56
}
57
 
58
point topleft(rect r)
59
{
60
	point p;
61
 
62
	p.x = r.x;
63
	p.y = r.y;
64
 
65
	return p;
66
}
67
 
68
point topright(rect r)
69
{
70
	point p;
71
 
72
	p.x = r.x + r.width - 1;
73
	p.y = r.y;
74
 
75
	return p;
76
}
77
 
78
point bottomright(rect r)
79
{
80
	point p;
81
 
82
	p.x = r.x + r.width - 1;
83
	p.y = r.y + r.height - 1;
84
 
85
	return p;
86
}
87
 
88
point bottomleft(rect r)
89
{
90
	point p;
91
 
92
	p.x = r.x;
93
	p.y = r.y + r.height - 1;
94
 
95
	return p;
96
}
97
 
98
point addpt(point p1, point p2)
99
{
100
	p1.x += p2.x;
101
	p1.y += p2.y;
102
	return p1;
103
}
104
 
105
point subpt(point p1, point p2)
106
{
107
	p1.x -= p2.x;
108
	p1.y -= p2.y;
109
	return p1;
110
}
111
 
112
point midpt(point p1, point p2)
113
{
114
	point p;
115
 
116
	p.x = (p1.x + p2.x)/2;
117
	p.y = (p1.y + p2.y)/2;
118
	return p;
119
}
120
 
121
point mulpt(point p, int i)
122
{
123
	p.x *= i;
124
	p.y *= i;
125
	return p;
126
}
127
 
128
point divpt(point p, int i)
129
{
130
	p.x /= i;
131
	p.y /= i;
132
	return p;
133
}
134
 
135
rect rmove(rect r, point p)
136
{
137
	r.x = p.x;
138
	r.y  = p.y;
139
	return r;
140
}
141
 
142
rect raddpt(rect r, point p)
143
{
144
	r.x += p.x;
145
	r.y += p.y;
146
	return r;
147
}
148
 
149
rect rsubpt(rect r, point p)
150
{
151
	r.x -= p.x;
152
	r.y  -= p.y;
153
	return r;
154
}
155
 
156
rect rmul(rect r, int i)
157
{
158
	if (i != 1) {
159
		r.x *= i;
160
		r.y  *= i;
161
		r.width *= i;
162
		r.height *= i;
163
	}
164
	return r;
165
}
166
 
167
rect rdiv(rect r, int i)
168
{
169
	if (i != 1) {
170
		r.x /= i;
171
		r.y  /= i;
172
		r.width /= i;
173
		r.height /= i;
174
	}
175
	return r;
176
}
177
 
178
rect growr(rect r, int w, int h)
179
{
180
	r.x -= w;
181
	r.y  -= h;
182
	r.width += 2*w;
183
	r.height += 2*h;
184
	return r;
185
}
186
 
187
rect insetr(rect r, int i)
188
{
189
	r.x += i;
190
	r.y  += i;
191
	r.width -= 2*i;
192
	r.height -= 2*i;
193
	return r;
194
}
195
 
196
rect rcenter(rect r1, rect r2) /* center r1 on r2 */
197
{
198
	rect r;
199
 
200
	r.x = r2.x + (r2.width-r1.width)/2;
201
	r.y  = r2.y + (r2.height-r1.height)/2;
202
	r.width = r1.width;
203
	r.height = r1.height;
204
 
205
	return r;
206
}
207
 
208
int ptinr(point p, rect r)
209
{
210
	if ((p.x>=r.x) && (p.x<r.x+r.width) &&
211
			(p.y>=r.y) && (p.y<r.y+r.height))
212
		return 1;
213
	else
214
		return 0;
215
}
216
 
217
int rinr(rect r1, rect r2)
218
{
219
	if ((r1.x>=r2.x) && (r1.y>=r2.y) &&
220
		(r1.x+r1.width<=r2.x+r2.width) &&
221
		(r1.y+r1.height<=r2.y+r2.height))
222
		return 1;
223
	else
224
		return 0;
225
}
226
 
227
int rxr(rect r1, rect r2)
228
{
229
	if ((r1.x<r2.x+r2.width) &&
230
		(r2.x<r1.x+r1.width) &&
231
		(r1.y<r2.y+r2.height) &&
232
		(r2.y<r1.y+r1.height))
233
		return 1;
234
	else
235
		return 0;
236
}
237
 
238
int equalpt(point p1, point p2)
239
{
240
	if ((p1.x==p2.x) && (p1.y==p2.y))
241
		return 1;
242
	else
243
		return 0;
244
}
245
 
246
int equalr(rect r1, rect r2)
247
{
248
	if ((r1.x==r2.x) && (r1.width==r2.width) &&
249
		(r1.y==r2.y) && (r1.height==r2.height))
250
		return 1;
251
	else
252
		return 0;
253
}
254
 
255
rect clipr(rect r1, rect r2)
256
{
257
	if (rxr(r1,r2) == 0)
258
		return rect(0,0,0,0); /* they don't overlap */
259
 
260
	if (r1.x < r2.x) {
261
		r1.width -= (r2.x - r1.x);
262
		r1.x = r2.x;
263
	}
264
	if (r1.y < r2.y) {
265
		r1.height -= (r2.y - r1.y);
266
		r1.y = r2.y;
267
	}
268
	if (r1.x + r1.width > r2.x + r2.width)
269
		r1.width = r2.x + r2.width - r1.x;
270
	if (r1.y + r1.height > r2.y + r2.height)
271
		r1.height = r2.y + r2.height - r1.y;
272
	return r1; /* they do overlap */
273
}
274
 
275
rect rcanon(rect r)
276
{
277
	if (r.width < 0) {
278
		r.x += r.width;
279
		r.width = -r.width;
280
	}
281
	if (r.height < 0) {
282
		r.y += r.height;
283
		r.height = -r.height;
284
	}
285
	return r;
286
}