-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblRectangleOperations.hpp
More file actions
691 lines (539 loc) · 23.2 KB
/
blRectangleOperations.hpp
File metadata and controls
691 lines (539 loc) · 23.2 KB
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#ifndef BL_RECTANGLEOPERATIONS_HPP
#define BL_RECTANGLEOPERATIONS_HPP
///-------------------------------------------------------------------
///
///
///
/// PURPOSE: Some useful functions to handle rectangles
///
/// AUTHOR: Vincenzo Barbato
/// navyenzo@gmail.com
///
/// NOTE: All things in this library are defined within the
/// blMathAPI namespace
///
/// LISENSE: MIT-LICENCE
/// http://www.opensource.org/licenses/mit-license.php
///
///
///
///-------------------------------------------------------------------
//-------------------------------------------------------------------
// Includes needed for this file
//-------------------------------------------------------------------
#include "blRectangle.hpp"
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// NOTE: This class is defined within the blMathAPI namespace
//-------------------------------------------------------------------
namespace blMathAPI
{
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to calculate the width, height
// area and center of a rectangle
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType width(const blRectangle<blNumberType>& rectangle)
{
return blMathAPI::abs(rectangle.p2().x() - rectangle.p1().x());
}
template<typename blNumberType>
inline blNumberType height(const blRectangle<blNumberType>& rectangle)
{
return blMathAPI::abs(rectangle.p2().y() - rectangle.p1().y());
}
template<typename blNumberType>
inline blNumberType area(const blRectangle<blNumberType>& rectangle)
{
return width(rectangle)*height(rectangle);
}
template<typename blNumberType>
inline blPoint2d<blNumberType> center(const blRectangle<blNumberType>& rectangle)
{
return (rectangle.p1() + rectangle.p2())/blNumberType(2);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to sord a rectangle's
// coordinates so that p1.x and p1.y are
// smaller than p2.x and p2.y
//-------------------------------------------------------------------
template<typename blNumberType>
inline blRectangle<blNumberType>& sortCoordinates(blRectangle<blNumberType>& rectangle)
{
// Sort x coordinates
if(rectangle.p1().x() > rectangle.p2().x())
std::swap(rectangle.p1().x(),rectangle.p2().x());
// Sort y coordinates
if(rectangle.p1().y() > rectangle.p2().y())
std::swap(rectangle.p1().y(),rectangle.p2().y());
return rectangle;
}
template<typename blNumberType>
inline blRectangle<blNumberType>& getSortedCoordinates(const blRectangle<blNumberType>& rectangle)
{
blRectangle<blNumberType> sortedRectangle = rectangle;
// Sort x coordinates
if(sortedRectangle.p1().x() > sortedRectangle.p2().x())
std::swap(sortedRectangle.p1().x(),sortedRectangle.p2().x());
// Sort y coordinates
if(sortedRectangle.p1().y() > sortedRectangle.p2().y())
std::swap(sortedRectangle.p1().y(),sortedRectangle.p2().y());
return sortedRectangle;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to clip coordinates if they
// go past a specified limit
//-------------------------------------------------------------------
template<typename blNumberType>
inline blRectangle<blNumberType>& clipXCoords(const blNumberType& xMin,
const blNumberType& xMax,
blRectangle<blNumberType>& rectangle)
{
if(xMin <= xMax)
{
if(rectangle.p1().x() <= rectangle.p2().x())
{
if(rectangle.p1().x() < xMin)
rectangle.p1().x() = xMin;
if(rectangle.p2().x() > xMax)
rectangle.p2().x() = xMax;
}
else
{
if(rectangle.p2().x() < xMin)
rectangle.p2().x() = xMin;
if(rectangle.p1().x() > xMax)
rectangle.p1().x() = xMax;
}
}
else
{
if(rectangle.p1().x() <= rectangle.p2().x())
{
if(rectangle.p1().x() < xMax)
rectangle.p1().x() = xMax;
if(rectangle.p2().x() > xMin)
rectangle.p2().x() = xMin;
}
else
{
if(rectangle.p2().x() < xMax)
rectangle.p2().x() = xMax;
if(rectangle.p1().x() > xMin)
rectangle.p1().x() = xMin;
}
}
return rectangle;
}
template<typename blNumberType>
inline blRectangle<blNumberType>& clipYCoords(const blNumberType& yMin,
const blNumberType& yMax,
blRectangle<blNumberType>& rectangle)
{
if(yMin <= yMax)
{
if(rectangle.p1().y() <= rectangle.p2().y())
{
if(rectangle.p1().y() < yMin)
rectangle.p1().y() = yMin;
if(rectangle.p2().y() > yMax)
rectangle.p2().y() = yMax;
}
else
{
if(rectangle.p2().y() < yMin)
rectangle.p2().y() = yMin;
if(rectangle.p1().y() > yMax)
rectangle.p1().y() = yMax;
}
}
else
{
if(rectangle.p1().y() <= rectangle.p2().y())
{
if(rectangle.p1().y() < yMax)
rectangle.p1().y() = yMax;
if(rectangle.p2().y() > yMin)
rectangle.p2().y() = yMin;
}
else
{
if(rectangle.p2().y() < yMax)
rectangle.p2().y() = yMax;
if(rectangle.p1().y() > yMin)
rectangle.p1().y() = yMin;
}
}
return rectangle;
}
template<typename blNumberType>
inline blRectangle<blNumberType>& clipCoords(const blNumberType& xMin,
const blNumberType& yMin,
const blNumberType& xMax,
const blNumberType& yMax,
blRectangle<blNumberType>& rectangle)
{
clipXCoords(xMin,xMax,rectangle);
clipYCoords(yMin,yMax,rectangle);
return rectangle;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to translated a rectangle
//-------------------------------------------------------------------
template<typename blNumberType>
inline blRectangle<blNumberType>& translate(const blNumberType& xTranslation,
const blNumberType& yTranslation,
blRectangle<blNumberType>& rectangle)
{
// Add xTranslation and
// yTranslation to all coordinates
rectangle.p1().x() += xTranslation;
rectangle.p1().y() += yTranslation;
rectangle.p2().x() += xTranslation;
rectangle.p2().y() += yTranslation;
return rectangle;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to move rectangles
//-------------------------------------------------------------------
template<typename blNumberType>
inline blRectangle<blNumberType>& moveByPlacingP1At(const blNumberType& x,
const blNumberType& y,
blRectangle<blNumberType>& rectangle)
{
return translate(x - rectangle.p1().x(),y - rectangle.p1().y(),rectangle);
}
template<typename blNumberType>
inline blRectangle<blNumberType>& moveRectangleByPlacingP2At(const blNumberType& x,
const blNumberType& y,
blRectangle<blNumberType>& rectangle)
{
return translateRectangle(x - rectangle.p2().x(),y - rectangle.p2().y());
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to scale rectangles
//-------------------------------------------------------------------
template<typename blNumberType>
inline blRectangle<blNumberType>& scaleRectangle(const blNumberType& widthScale,
const blNumberType& heightScale,
blRectangle<blNumberType>& rectangle)
{
// Get the center point
// of this rectangle
blPoint2d<blNumberType> centerPoint = center(rectangle);
// Calculate the new
// scaled width and height
blNumberType halfOfNewWidth = width(rectangle)/blNumberType(2) * widthScale;
blNumberType halfOfNewHeight = height(rectangle)/blNumberType(2) * heightScale;
// Place the x coords
if(rectangle.p1().x() <= rectangle.p2().x())
{
rectangle.p1().x() = centerPoint.x() - halfOfNewWidth;
rectangle.p2().x() = centerPoint.x() + halfOfNewWidth;
}
else
{
rectangle.p1().x() = centerPoint.x() + halfOfNewWidth;
rectangle.p2().x() = centerPoint.x() - halfOfNewWidth;
}
// Place the y coords
if(rectangle.p1().y() <= rectangle.p2().y())
{
rectangle.p1().y() = centerPoint.y() - halfOfNewHeight;
rectangle.p2().y() = centerPoint.y() + halfOfNewHeight;
}
else
{
rectangle.p1().y() = centerPoint.y() + halfOfNewHeight;
rectangle.p2().y() = centerPoint.y() - halfOfNewHeight;
}
return rectangle;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to scale a rectangle
// while keeping its point P1 fixed
//-------------------------------------------------------------------
template<typename blNumberType>
inline blRectangle<blNumberType>& scaleRectangleWithP1Fixed(const blNumberType& widthScale,
const blNumberType& heightScale,
blRectangle<blNumberType>& rectangle)
{
// Get the center point
// of this rectangle
blPoint2d<blNumberType> centerPoint = center(rectangle);
// Calculate the new
// scaled width and height
blNumberType newWidth = width(rectangle) * widthScale;
blNumberType newHeight = height(rectangle) * heightScale;
// Add the new width and height
// to the point P1
if(rectangle.p1().x() < rectangle.p2().x())
rectangle.p2().x() = rectangle.p1().x() + newWidth;
else
rectangle.p2().x() = rectangle.p1().x() - newWidth;
if(rectangle.p1().y() < rectangle.p2().y())
rectangle.p2().y() = rectangle.p1().y() + newHeight;
else
rectangle.p2().y() = rectangle.p1().y() - newHeight;
return rectangle;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to scale a rectangle
// while keeping its point P2 fixed
//-------------------------------------------------------------------
template<typename blNumberType>
inline blRectangle<blNumberType>& scaleRectangleWithP2Fixed(const blNumberType& widthScale,
const blNumberType& heightScale,
blRectangle<blNumberType>& rectangle)
{
// Get the center point
// of this rectangle
blPoint2d<blNumberType> centerPoint = center(rectangle);
// Calculate the new
// scaled width and height
blNumberType newWidth = width(rectangle) * widthScale;
blNumberType newHeight = height(rectangle) * heightScale;
// Add the new width and height
// to the point P2
if(rectangle.p2().x() > rectangle.p1().x())
rectangle.p1().x() = rectangle.p2().x() - newWidth;
else
rectangle.p1().x() = rectangle.p2().x() + newWidth;
if(rectangle.p2().y() > rectangle.p1().y())
rectangle.p1().y() = rectangle.p2().y() - newHeight;
else
rectangle.p1().y() = rectangle.p2().y() + newHeight;
return rectangle;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to scale a rectangle's
// individual coordinates
//-------------------------------------------------------------------
template<typename blNumberType>
inline blRectangle<blNumberType>& scaleCoordinates(const blNumberType& xScale,
const blNumberType& yScale,
blRectangle<blNumberType>& rectangle)
{
// Simply scale the x
// and y coordinates
// by the passed arguments
rectangle.p1() *= xScale;
rectangle.p1() *= yScale;
rectangle.p2() *= xScale;
rectangle.p2() *= yScale;
return rectangle;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to center a rectangle
// about a specified point
//-------------------------------------------------------------------
template<typename blNumberType>
inline blRectangle<blNumberType>& centerRectangle(const blPoint2d<blNumberType>& newCenter,
blRectangle<blNumberType>& rectangle)
{
// Calculate the difference
// between the new and old centers
blPoint2d<blNumberType> translation = newCenter - center(rectangle);
// Translate the rectangle
// accordingly
return translate(translation.x,translation.y,rectangle);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to flip a rectangle's coordinates
//-------------------------------------------------------------------
template<typename blNumberType>
inline blRectangle<blNumberType>& flipXCoords(blRectangle<blNumberType>& rectangle)
{
std::swap(rectangle.p1().x(),rectangle.p2().x());
return rectangle;
}
template<typename blNumberType>
inline blRectangle<blNumberType>& flipYCoords(blRectangle<blNumberType>& rectangle)
{
std::swap(rectangle.p1().y(),rectangle.p2().y());
return rectangle;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to map a point onto a rectangle
// Function assumes that the point coordinates vary
// from 0 to 1
// The point is then mapped onto an actual coordinate
// while lies somewhere in the specified rectangle
// Ex: point P(0,0) would map to rectangle coordinate P1
// Ex: point P(1,1) would map to rectangle coordinate P2
//-------------------------------------------------------------------
template<typename blNumberType,typename blNumberType2>
inline blPoint2d<blNumberType>& mapPointToRectangle(const blRectangle<blNumberType>& rectangleMap,
blPoint2d<blNumberType2>& pointToMap)
{
pointToMap.x = static_cast<blNumberType2>(rectangleMap.p1().x() + static_cast<blNumberType>(pointToMap.x) * (rectangleMap.p2().x() - rectangleMap.p1().x()));
pointToMap.y = static_cast<blNumberType2>(rectangleMap.p1().y() + static_cast<blNumberType>(pointToMap.y) * (rectangleMap.p2().y() - rectangleMap.p1().y()));
return pointToMap;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Same function as above but this function
// maps a rectangle whose coordinates vary
// from 0 to 1 onto a specified rectangle
//-------------------------------------------------------------------
template<typename blNumberType>
inline blRectangle<blNumberType>& mapRectangleToRectangle(const blRectangle<blNumberType>& rectangleMap,
blRectangle<blNumberType>& rectangleToMap)
{
mapPointToRectangle(rectangleMap,rectangleToMap.p1());
mapPointToRectangle(rectangleMap,rectangleToMap.p2());
return rectangleToMap;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to "zoom" where a "zoom rectangle" whose
// coordinates vary from 0 to 1 is mapped onto a rectangle
// and that rectangle's coordinates are changed so that now
// it is "zoomed"
//-------------------------------------------------------------------
template<typename blNumberType>
inline blRectangle<blNumberType>& zoomRectangle(const blRectangle<blNumberType>& zoomRectangle,
blRectangle<blNumberType>& rectangleToZoom)
{
// First we map the zoom
// rectangle's coordinates
// onto the rectangle to zoom
blRectangle<blNumberType> rectangleToMap = zoomRectangle;
mapRectangleToRectangle(rectangleToZoom,rectangleToMap);
// Then we set the new rectangle's
// zoomed coordinates
rectangleToZoom = rectangleToMap;
return rectangleToZoom;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Change the size of rectangle while
// keeping it centered where it currently is
//-------------------------------------------------------------------
template<typename blNumberType,typename blNumberType2>
inline blRectangle<blNumberType>& sizeRectangle(const blNumberType2& newWidth,
const blNumberType2& newHeight,
blRectangle<blNumberType>& rectangle)
{
// Get the center point
// of this rectangle
blPoint2d<blNumberType> centerPoint = center(rectangle);
// Place the x coords
if(rectangle.p1().x() <= rectangle.p2().x())
{
rectangle.p1().x() = centerPoint.x() - blMathAPI::abs(static_cast<blNumberType>(newWidth)/2.0);
rectangle.p2().x() = centerPoint.x() + blMathAPI::abs(static_cast<blNumberType>(newWidth)/2.0);
}
else
{
rectangle.p1().x() = centerPoint.x() + blMathAPI::abs(static_cast<blNumberType>(newWidth)/2.0);
rectangle.p2().x() = centerPoint.x() - blMathAPI::abs(static_cast<blNumberType>(newWidth)/2.0);
}
// Place the y coords
if(rectangle.p1().y() <= rectangle.p2().y())
{
rectangle.p1().y() = centerPoint.y() - blMathAPI::abs(static_cast<blNumberType>(newHeight)/2.0);
rectangle.p2().y() = centerPoint.y() + blMathAPI::abs(static_cast<blNumberType>(newHeight)/2.0);
}
else
{
rectangle.p1().y() = centerPoint.y() + blMathAPI::abs(static_cast<blNumberType>(newHeight)/2.0);
rectangle.p2().y() = centerPoint.y() - blMathAPI::abs(static_cast<blNumberType>(newHeight)/2.0);
}
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to
// output a rectangle
// to an output stream
//-------------------------------------------------------------------
template<typename blNumberType>
inline std::ostream& operator<<(std::ostream& os,
const blMathAPI::blRectangle<blNumberType>& rectangle)
{
// We simply output the
// components with a
// space separating them
os << rectangle.p1() << " " << rectangle.p2();
return os;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to
// input a rectangle
// from an input stream
//-------------------------------------------------------------------
template<typename blNumberType>
inline std::istream& operator>>(std::istream& is,
blRectangle<blNumberType>& rectangle)
{
if(!(is >> rectangle.p1()))
{
// Error -- Cound not read
// the p1 value
return is;
}
if(!(is >> rectangle.p2()))
{
// Error -- Cound not read
// the p2 value
return is;
}
return is;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to
// convert a generic
// string to a rectangle
//-------------------------------------------------------------------
template<typename blStringIteratorType,
typename blCharacterType,
typename blNumberType>
inline blStringIteratorType blConvertStringToNumber(const blStringIteratorType& beginIter,
const blStringIteratorType& endIter,
const blCharacterType& DecimalPointDelimiter,
blMathAPI::blRectangle<blNumberType>& ConvertedNumber,
const int& NumberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter)
{
// We expect the
// following format:
// R = p1 p2
blStringIteratorType newStringPosition;
// First we try to
// get first point
newStringPosition = blConvertStringToNumber(beginIter,
endIter,
DecimalPointDelimiter,
ConvertedNumber.p1(),
NumberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
if(newStringPosition != endIter)
++newStringPosition;
// Then we try to
// get the second
// point
newStringPosition = blConvertStringToNumber(newStringPosition,
endIter,
DecimalPointDelimiter,
ConvertedNumber.p2(),
NumberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
return newStringPosition;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// End of the blMathAPI namespace
}
//-------------------------------------------------------------------
#endif // BL_RECTANGLEOPERATIONS_HPP