This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRRectangle.cs
447 lines (406 loc) · 14.1 KB
/
RRectangle.cs
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Rectangle.java
// Java Spatial Index Library
// Copyright (C) 2002 Infomatiq Limited
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// Ported to C# By Dror Gluska, April 9th, 2009
using System;
using System.Text;
using BibliotecaComun;
namespace SolucionAlumno
{
/**
* Currently hardcoded to 2 dimensions, but could be extended.
*
* @author [email protected]
* @version 1.0b2p1
*/
public class RRectangle
{
/**
* Number of dimensions in a rectangle. In theory this
* could be exended to three or more dimensions.
*/
internal const int DIMENSIONS = 2;
/**
* array containing the minimum value for each dimension; ie { min(x), min(y) }
*/
internal int[] max;
/**
* array containing the maximum value for each dimension; ie { max(x), max(y) }
*/
internal int[] min;
/**
* Constructor.
*
* @param x1 coordinate of any corner of the rectangle
* @param y1 (see x1)
* @param x2 coordinate of the opposite corner
* @param y2 (see x2)
*/
public RRectangle(int x1, int y1, int x2, int y2)
{
min = new int[DIMENSIONS];
max = new int[DIMENSIONS];
set(x1, y1, x2, y2);
}
public RRectangle(ZonaProhibida z)
{
min = new int[DIMENSIONS];
max = new int[DIMENSIONS];
set(z.X, z.Y, z.X + z.Width, z.Y + z.Height);
}
/**
* Constructor.
*
* @param min array containing the minimum value for each dimension; ie { min(x), min(y) }
* @param max array containing the maximum value for each dimension; ie { max(x), max(y) }
*/
public RRectangle(int[] min, int[] max)
{
if (min.Length != DIMENSIONS || max.Length != DIMENSIONS)
{
throw new Exception("Error in Rectangle constructor: " +
"min and max arrays must be of length " + DIMENSIONS);
}
this.min = new int[DIMENSIONS];
this.max = new int[DIMENSIONS];
set(min, max);
}
/**
* Sets the size of the rectangle.
*
* @param x1 coordinate of any corner of the rectangle
* @param y1 (see x1)
* @param x2 coordinate of the opposite corner
* @param y2 (see x2)
*/
internal void set(int x1, int y1, int x2, int y2)
{
min[0] = Math.Min(x1, x2);
min[1] = Math.Min(y1, y2);
max[0] = Math.Max(x1, x2);
max[1] = Math.Max(y1, y2);
}
/**
* Sets the size of the rectangle.
*
* @param min array containing the minimum value for each dimension; ie { min(x), min(y) }
* @param max array containing the maximum value for each dimension; ie { max(x), max(y) }
*/
internal void set(int[] min, int[] max)
{
System.Array.Copy(min, 0, this.min, 0, DIMENSIONS);
System.Array.Copy(max, 0, this.max, 0, DIMENSIONS);
}
/**
* Make a copy of this rectangle
*
* @return copy of this rectangle
*/
internal RRectangle copy()
{
return new RRectangle(min, max);
}
/**
* Determine whether an edge of this rectangle overlies the equivalent
* edge of the passed rectangle
*/
internal bool edgeOverlaps(RRectangle r)
{
for (int i = 0; i < DIMENSIONS; i++)
{
if (min[i] == r.min[i] || max[i] == r.max[i])
{
return true;
}
}
return false;
}
/**
* Determine whether this rectangle intersects the passed rectangle
*
* @param r The rectangle that might intersect this rectangle
*
* @return true if the rectangles intersect, false if they do not intersect
*/
internal bool intersects(RRectangle r)
{
// Every dimension must intersect. If any dimension
// does not intersect, return false immediately.
for (int i = 0; i < DIMENSIONS; i++)
{
if (max[i] < r.min[i] || min[i] > r.max[i])
{
return false;
}
}
return true;
}
/**
* Determine whether this rectangle contains the passed rectangle
*
* @param r The rectangle that might be contained by this rectangle
*
* @return true if this rectangle contains the passed rectangle, false if
* it does not
*/
internal bool contains(RRectangle r)
{
for (int i = 0; i < DIMENSIONS; i++)
{
if (max[i] < r.max[i] || min[i] > r.min[i])
{
return false;
}
}
return true;
}
/**
* Determine whether this rectangle is contained by the passed rectangle
*
* @param r The rectangle that might contain this rectangle
*
* @return true if the passed rectangle contains this rectangle, false if
* it does not
*/
internal bool containedBy(RRectangle r)
{
for (int i = 0; i < DIMENSIONS; i++)
{
if (max[i] > r.max[i] || min[i] < r.min[i])
{
return false;
}
}
return true;
}
/**
* Return the distance between this rectangle and the passed point.
* If the rectangle contains the point, the distance is zero.
*
* @param p Point to find the distance to
*
* @return distance beween this rectangle and the passed point.
*/
internal int distance(RPoint p)
{
int distanceSquared = 0;
for (int i = 0; i < DIMENSIONS; i++)
{
int greatestMin = Math.Max(min[i], p.coordinates[i]);
int leastMax = Math.Min(max[i], p.coordinates[i]);
if (greatestMin > leastMax)
{
distanceSquared += ((greatestMin - leastMax) * (greatestMin - leastMax));
}
}
return (int)Math.Sqrt(distanceSquared);
}
/**
* Return the distance between this rectangle and the passed rectangle.
* If the rectangles overlap, the distance is zero.
*
* @param r Rectangle to find the distance to
*
* @return distance between this rectangle and the passed rectangle
*/
internal int distance(RRectangle r)
{
int distanceSquared = 0;
for (int i = 0; i < DIMENSIONS; i++)
{
int greatestMin = Math.Max(min[i], r.min[i]);
int leastMax = Math.Min(max[i], r.max[i]);
if (greatestMin > leastMax)
{
distanceSquared += ((greatestMin - leastMax) * (greatestMin - leastMax));
}
}
return (int)Math.Sqrt(distanceSquared);
}
/**
* Return the squared distance from this rectangle to the passed point
*/
internal int distanceSquared(int dimension, int point)
{
int distanceSquared = 0;
int tempDistance = point - max[dimension];
for (int i = 0; i < 2; i++)
{
if (tempDistance > 0)
{
distanceSquared = (tempDistance * tempDistance);
break;
}
tempDistance = min[dimension] - point;
}
return distanceSquared;
}
/**
* Return the furthst possible distance between this rectangle and
* the passed rectangle.
*
* Find the distance between this rectangle and each corner of the
* passed rectangle, and use the maximum.
*
*/
internal int furthestDistance(RRectangle r)
{
int distanceSquared = 0;
for (int i = 0; i < DIMENSIONS; i++)
{
distanceSquared += Math.Max(r.min[i], r.max[i]);
#warning possible didn't convert properly
//distanceSquared += Math.Max(distanceSquared(i, r.min[i]), distanceSquared(i, r.max[i]));
}
return (int)Math.Sqrt(distanceSquared);
}
/**
* Calculate the area by which this rectangle would be enlarged if
* added to the passed rectangle. Neither rectangle is altered.
*
* @param r Rectangle to union with this rectangle, in order to
* compute the difference in area of the union and the
* original rectangle
*/
internal int enlargement(RRectangle r)
{
int enlargedArea = (Math.Max(max[0], r.max[0]) - Math.Min(min[0], r.min[0])) *
(Math.Max(max[1], r.max[1]) - Math.Min(min[1], r.min[1]));
return enlargedArea - area();
}
/**
* Compute the area of this rectangle.
*
* @return The area of this rectangle
*/
internal int area()
{
return (max[0] - min[0]) * (max[1] - min[1]);
}
/**
* Computes the union of this rectangle and the passed rectangle, storing
* the result in this rectangle.
*
* @param r Rectangle to add to this rectangle
*/
internal void add(RRectangle r)
{
for (int i = 0; i < DIMENSIONS; i++)
{
if (r.min[i] < min[i])
{
min[i] = r.min[i];
}
if (r.max[i] > max[i])
{
max[i] = r.max[i];
}
}
}
/**
* Find the the union of this rectangle and the passed rectangle.
* Neither rectangle is altered
*
* @param r The rectangle to union with this rectangle
*/
internal RRectangle union(RRectangle r)
{
RRectangle union = this.copy();
union.add(r);
return union;
}
internal bool CompareArrays(int[] a1, int[] a2)
{
if ((a1 == null) || (a2 == null))
return false;
if (a1.Length != a2.Length)
return false;
for (int i = 0; i < a1.Length; i++)
if (a1[i] != a2[i])
return false;
return true;
}
/**
* Determine whether this rectangle is equal to a given object.
* Equality is determined by the bounds of the rectangle.
*
* @param o The object to compare with this rectangle
*/
public override bool Equals(object obj)
{
bool equals = false;
if (obj.GetType() == typeof(RRectangle))
{
RRectangle r = (RRectangle)obj;
#warning possible didn't convert properly
if (CompareArrays(r.min, min) && CompareArrays(r.max, max))
{
equals = true;
}
}
return equals;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
/**
* Determine whether this rectangle is the same as another object
*
* Note that two rectangles can be equal but not the same object,
* if they both have the same bounds.
*
* @param o The object to compare with this rectangle.
*/
internal bool sameObject(object o)
{
return base.Equals(o);
}
/**
* Return a string representation of this rectangle, in the form:
* (1.2, 3.4), (5.6, 7.8)
*
* @return String String representation of this rectangle.
*/
public override string ToString()
{
StringBuilder sb = new StringBuilder();
// min coordinates
sb.Append('(');
for (int i = 0; i < DIMENSIONS; i++)
{
if (i > 0)
{
sb.Append(", ");
}
sb.Append(min[i]);
}
sb.Append("), (");
// max coordinates
for (int i = 0; i < DIMENSIONS; i++)
{
if (i > 0)
{
sb.Append(", ");
}
sb.Append(max[i]);
}
sb.Append(')');
return sb.ToString();
}
}
}