-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeometryR.cpp
1533 lines (1352 loc) · 59.2 KB
/
GeometryR.cpp
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2000-2002, Jelle Kok, University of Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the University of Amsterdam nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNE
R OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*! \file Geometry.C
<pre>
<b>File:</b> Geometry.C
<b>Project:</b> Robocup Soccer Simulation Team: UvA Trilearn
<b>Authors:</b> Jelle Kok
<b>Created:</b> 13/02/2001
<b>Last Revision:</b> $ID$
<b>Contents:</b> class declarations of different geometry classes:<BR>
- VecPosition: representation of a point
- Line: representation of a line
- Rectangle: representation of a rectangle
- Circle: representation of a circle
- Geometry: different geometry methods
Furthermore it contains some goniometric functions to work with sine, cosine
and tangent functions using degrees and some utility functions to return
the maximum and the minimum of two values.
<hr size=2>
<h2><b>Changes</b></h2>
<b>Date</b> <b>Author</b> <b>Comment</b>
12/02/2001 Jelle Kok Initial version created
</pre>
*/
#include "stdafx.h"
#include "GeometryR.h"
#include <stdio.h> // needed for sprintf
/*******************************************************************************************/
/** Following is Math functions ************************************************************/
/*******************************************************************************************/
/*! This function returns the Maths::Sign of a give double.
1 is positive, -1 is negative
\param d1 first parameter
\return the Maths::Sign of this double */
int Maths::Sign(double d1) {
return (d1 > 0) ? 1 : -1;
}
double Maths::Limit(double d, double dMin, double dMax) {
if (dMin > dMax) {
double dTemp = dMin;
dMin = dMax;
dMax = dTemp;
}
if (d < dMin) d = dMin;
if (d > dMax) d = dMax;
return d;
}
/*! This function returns the maximum of two given doubles.
\param d1 first parameter
\param d2 second parameter
\return the maximum of these two parameters */
double Maths::Max(double d1, double d2) {
return (d1 > d2) ? d1 : d2;
}
/*! This function returns the minimum of two given doubles.
\param d1 first parameter
\param d2 second parameter
\return the minimum of these two parameters */
double Maths::Min(double d1, double d2) {
return (d1 < d2) ? d1 : d2;
}
/*! This function converts an angle in radians to the corresponding angle in
degrees.
\param x an angle in radians
\return the corresponding angle in degrees */
AngDeg Maths::Rad2Deg(AngRad x) {
return (x * 180 / M_PI);
}
/*! This function converts an angle in degrees to the corresponding angle in
radians.
\param x an angle in degrees
\return the corresponding angle in radians */
AngRad Maths::Deg2Rad(AngDeg x) {
return (x * M_PI / 180);
}
/*! This function returns the cosine of a given angle in degrees using the
built-in cosine function that works with angles in radians.
\param x an angle in degrees
\return the cosine of the given angle */
double Maths::cosDeg(AngDeg x) {
return (cos(Deg2Rad(x)));
}
/*! This function returns the sine of a given angle in degrees using the
built-in sine function that works with angles in radians.
\param x an angle in degrees
\return the sine of the given angle */
double Maths::sinDeg(AngDeg x) {
return (sin(Deg2Rad(x)));
}
/*! This function returns the tangent of a given angle in degrees using the
built-in tangent function that works with angles in radians.
\param x an angle in degrees
\return the tangent of the given angle */
double Maths::tanDeg(AngDeg x) {
return (tan(Deg2Rad(x)));
}
/*! This function returns the principal value of the arc tangent of x in degrees
using the built-in arc tangent function which returns this value in radians.
\param x a double value
\return the arc tangent of the given value in degrees */
AngDeg Maths::atanDeg(double x) {
return (Rad2Deg(atan(x)));
}
/*! This function returns the principal value of the arc tangent of y/x in
degrees using the signs of both arguments to determine the quadrant of the
return value. For this the built-in 'atan2' function is used which returns
this value in radians.
\param x a double value
\param y a double value
\return the arc tangent of y/x in degrees taking the signs of x and y into
account */
double Maths::atan2Deg(double x, double y) {
if (fabs(x) < EPS && fabs(y) < EPS)
return (0.0);
return (Rad2Deg(atan2(x, y)));
}
/*! This function returns the principal value of the arc cosine of x in degrees
using the built-in arc cosine function which returns this value in radians.
\param x a double value
\return the arc cosine of the given value in degrees */
AngDeg Maths::acosDeg(double x) {
if (x >= 1)
return (0.0);
else if (x <= -1)
return (180.0);
return (Rad2Deg(acos(x)));
}
/*! This function returns the principal value of the arc sine of x in degrees
using the built-in arc sine function which returns this value in radians.
\param x a double value
\return the arc sine of the given value in degrees */
AngDeg Maths::asinDeg(double x) {
if (x >= 1)
return (90.0);
else if (x <= -1)
return (-90.0);
return (Rad2Deg(asin(x)));
}
/*********************************************************************************/
/*! This function returns a boolean value which indicates whether the value
'ang' (from interval [-180..180] lies in the interval [angMin..angMax].
Examples: IsAngInInterval( -100, 4, -150) returns false
IsAngInInterval( 45, 4, -150) returns true
\param ang angle that should be checked
\param angMin minimum angle in interval
\param angMax maximum angle in interval
\return boolean indicating whether ang lies in [angMin..angMax] */
bool IsAngInInterval(AngRad ang, AngRad angMin, AngRad angMax) {
// convert all angles to interval 0..360
if ((ang + 2 * M_PI) < 2 * M_PI) ang += 2 * M_PI;
if ((angMin + 2 * M_PI) < 2 * M_PI) angMin += 2 * M_PI;
if ((angMax + 2 * M_PI) < 2 * M_PI) angMax += 2 * M_PI;
if (angMin < angMax) // 0 ---false-- angMin ---true-----angMax---false--360
return angMin < ang && ang < angMax;
else // 0 ---true--- angMax ---false----angMin---true---360
return !(angMax < ang && ang < angMin);
}
/*! This method returns the bisector (average) of two angles. It deals
with the boundary problem, thus when 'angMin' equals 170 and 'angMax'
equals -100, -145 is returned.
\param angMin minimum angle [-PI,PI]
\param angMax maximum angle [-PI,PI]
\return average of angMin and angMax. */
AngRad GetBisectorTwoAngles(AngRad angMin, AngRad angMax) {
// separate sine and cosine part to circumvent boundary problem
return VecPosition::NormalizeAngle(
atan2((sin(angMin) + sin(angMax)) / 2.0,
(cos(angMin) + cos(angMax)) / 2.0));
}
/******************************************************************************/
/******************** CLASS VECPOSITION ***********************************/
/******************************************************************************/
/*! Constructor for the VecPosition claSS-> When the supplied Coordinate System
type equals CARTESIAN, the arguments x and y denote the x- and y-coordinates
of the new position. When it equals POLAR however, the arguments x and y
denote the polar coordinates of the new position; in this case x is thus
equal to the distance r from the origin and y is equal to the angle phi that
the polar vector Makes with the x-axis.
\param x the x-coordinate of the new position when cs == CARTESIAN; the
distance of the new position from the origin when cs = POLAR
\param y the y-coordinate of the new position when cs = CARTESIAN; the
angle that the polar vector Makes with the x-axis when cs = POLAR
\param cs a CoordSystemT indicating whether x and y denote cartesian
coordinates or polar coordinates
\return the VecPosition corresponding to the given arguments */
VecPosition::VecPosition(double x, double y, CoordSystemT cs) {
SetVecPosition(x, y, cs);
}
dbPOINT VecPosition::GetdbPOINT() {
dbPOINT p;
p.x = m_x;
p.y = m_y;
return p;
}
/*! Overloaded version of unary minus operator for VecPositions. It returns the
negative VecPosition, i.e. both the x- and y-coordinates are multiplied by
-1. The current VecPosition itself is left unchanged.
\return a negated version of the current VecPosition */
VecPosition VecPosition::operator-() {
return (VecPosition(-m_x, -m_y));
}
/*! Overloaded version of the binary plus operator for adding a given double
value to a VecPosition. The double value is added to both the x- and
y-coordinates of the current VecPosition. The current VecPosition itself is
left unchanged.
\param d a double value which has to be added to both the x- and
y-coordinates of the current VecPosition
\return the result of adding the given double value to the current
VecPosition */
VecPosition VecPosition::operator+(const double &d) {
return (VecPosition(m_x + d, m_y + d));
}
/*! Overloaded version of the binary plus operator for VecPositions. It returns
the sum of the current VecPosition and the given VecPosition by adding their
x- and y-coordinates. The VecPositions themselves are left unchanged.
\param p a VecPosition
\return the sum of the current VecPosition and the given VecPosition */
VecPosition VecPosition::operator+(const VecPosition &p) {
return (VecPosition(m_x + p.m_x, m_y + p.m_y));
}
/*! Overloaded version of the binary minus operator for subtracting a given
double value from a VecPosition. The double value is subtracted from both
the x- and y-coordinates of the current VecPosition. The current VecPosition
itself is left unchanged.
\param d a double value which has to be subtracted from both the x- and
y-coordinates of the current VecPosition
\return the result of subtracting the given double value from the current
VecPosition */
VecPosition VecPosition::operator-(const double &d) {
return (VecPosition(m_x - d, m_y - d));
}
/*! Overloaded version of the binary minus operator for VecPositions. It returns
the difference between the current VecPosition and the given VecPosition by
subtracting their x- and y-coordinates. The VecPositions themselves are left
unchanged.
\param p a VecPosition
\return the difference between the current VecPosition and the given
VecPosition */
VecPosition VecPosition::operator-(const VecPosition &p) {
return (VecPosition(m_x - p.m_x, m_y - p.m_y));
}
/*! Overloaded version of the multiplication operator for multiplying a
VecPosition by a given double value. Both the x- and y-coordinates of the
current VecPosition are multiplied by this value. The current VecPosition
itself is left unchanged.
\param d the multiplication factor
\return the result of multiplying the current VecPosition by the given
double value */
VecPosition VecPosition::operator*(const double &d) {
return (VecPosition(m_x * d, m_y * d));
}
/*! Overloaded version of the multiplication operator for VecPositions. It
returns the product of the current VecPosition and the given VecPosition by
multiplying their x- and y-coordinates. The VecPositions themselves are left
unchanged.
\param p a VecPosition
\return the product of the current VecPosition and the given VecPosition */
VecPosition VecPosition::operator*(const VecPosition &p) {
return (VecPosition(m_x * p.m_x, m_y * p.m_y));
}
/*! Overloaded version of the division operator for dividing a VecPosition by a
given double value. Both the x- and y-coordinates of the current VecPosition
are divided by this value. The current VecPosition itself is left unchanged.
\param d the division factor
\return the result of dividing the current VecPosition by the given double
value */
VecPosition VecPosition::operator/(const double &d) {
return (VecPosition(m_x / d, m_y / d));
}
/*! Overloaded version of the division operator for VecPositions. It returns the
quotient of the current VecPosition and the given VecPosition by dividing
their x- and y-coordinates. The VecPositions themselves are left unchanged.
\param p a VecPosition
\return the quotient of the current VecPosition and the given VecPosition */
VecPosition VecPosition::operator/(const VecPosition &p) {
return (VecPosition(m_x / p.m_x, m_y / p.m_y));
}
/*! Overloaded version of the assignment operator for assigning a given double
value to both the x- and y-coordinates of the current VecPosition. This
changes the current VecPosition itself.
\param d a double value which has to be assigned to both the x- and
y-coordinates of the current VecPosition */
void VecPosition::operator=(const double &d) {
m_x = d;
m_y = d;
}
/*! Overloaded version of the sum-assignment operator for VecPositions. It
returns the sum of the current VecPosition and the given VecPosition by
adding their x- and y-coordinates. This changes the current VecPosition
itself.
\param p a VecPosition which has to be added to the current VecPosition */
void VecPosition::operator+=(const VecPosition &p) {
m_x += p.m_x;
m_y += p.m_y;
}
/*! Overloaded version of the sum-assignment operator for adding a given double
value to a VecPosition. The double value is added to both the x- and
y-coordinates of the current VecPosition. This changes the current
VecPosition itself.
\param d a double value which has to be added to both the x- and
y-coordinates of the current VecPosition */
void VecPosition::operator+=(const double &d) {
m_x += d;
m_y += d;
}
/*! Overloaded version of the difference-assignment operator for VecPositions.
It returns the difference between the current VecPosition and the given
VecPosition by subtracting their x- and y-coordinates. This changes the
current VecPosition itself.
\param p a VecPosition which has to be subtracted from the current
VecPosition */
void VecPosition::operator-=(const VecPosition &p) {
m_x -= p.m_x;
m_y -= p.m_y;
}
/*! Overloaded version of the difference-assignment operator for subtracting a
given double value from a VecPosition. The double value is subtracted from
both the x- and y-coordinates of the current VecPosition. This changes the
current VecPosition itself.
\param d a double value which has to be subtracted from both the x- and
y-coordinates of the current VecPosition */
void VecPosition::operator-=(const double &d) {
m_x -= d;
m_y -= d;
}
/*! Overloaded version of the multiplication-assignment operator for
VecPositions. It returns the product of the current VecPosition and the
given VecPosition by multiplying their x- and y-coordinates. This changes
the current VecPosition itself.
\param p a VecPosition by which the current VecPosition has to be
multiplied */
void VecPosition::operator*=(const VecPosition &p) {
m_x *= p.m_x;
m_y *= p.m_y;
}
/*! Overloaded version of the multiplication-assignment operator for multiplying
a VecPosition by a given double value. Both the x- and y-coordinates of the
current VecPosition are multiplied by this value. This changes the current
VecPosition itself.
\param d a double value by which both the x- and y-coordinates of the
current VecPosition have to be multiplied */
void VecPosition::operator*=(const double &d) {
m_x *= d;
m_y *= d;
}
/*! Overloaded version of the division-assignment operator for VecPositions. It
returns the quotient of the current VecPosition and the given VecPosition by
dividing their x- and y-coordinates. This changes the current VecPosition
itself.
\param p a VecPosition by which the current VecPosition has to be divided */
void VecPosition::operator/=(const VecPosition &p) {
m_x /= p.m_x;
m_y /= p.m_y;
}
/*! Overloaded version of the division-assignment operator for dividing a
VecPosition by a given double value. Both the x- and y-coordinates of the
current VecPosition are divided by this value. This changes the current
VecPosition itself.
\param d a double value by which both the x- and y-coordinates of the
current VecPosition have to be divided */
void VecPosition::operator/=(const double &d) {
m_x /= d;
m_y /= d;
}
/*! Overloaded version of the inequality operator for VecPositions. It
determines whether the current VecPosition is unequal to the given
VecPosition by comparing their x- and y-coordinates.
\param p a VecPosition
\return true when either the x- or y-coordinates of the given VecPosition
and the current VecPosition are different; false otherwise */
bool VecPosition::operator!=(const VecPosition &p) {
return ((fabs(m_x - p.m_x) > EPS) || (fabs(m_y - p.m_y) > EPS));
}
/*! Overloaded version of the inequality operator for comparing a VecPosition to
a double value. It determines whether either the x- or y-coordinate of the
current VecPosition is unequal to the given double value.
\param d a double value with which both the x- and y-coordinates of the
current VecPosition have to be compared.
\return true when either the x- or y-coordinate of the current VecPosition
is unequal to the given double value; false otherwise */
bool VecPosition::operator!=(const double &d) {
return ((fabs(m_x - d) > EPS) || (fabs(m_y - d) > EPS));
}
/*! Overloaded version of the equality operator for VecPositions. It determines
whether the current VecPosition is equal to the given VecPosition by
comparing their x- and y-coordinates.
\param p a VecPosition
\return true when both the x- and y-coordinates of the given VecPosition and
the current VecPosition are equal; false otherwise */
bool VecPosition::operator==(const VecPosition &p) {
return ((fabs(m_x - p.m_x) < EPS) && (fabs(m_y - p.m_y) < EPS));
}
/*! Overloaded version of the equality operator for comparing a VecPosition to a
double value. It determines whether both the x- and y-coordinates of the
current VecPosition are equal to the given double value.
\param d a double value with which both the x- and y-coordinates of the
current VecPosition have to be compared.
\return true when both the x- and y-coordinates of the current VecPosition
are equal to the given double value; false otherwise */
bool VecPosition::operator==(const double &d) {
return ((fabs(m_x - d) < EPS) && (fabs(m_y - d) < EPS));
}
/*! Overloaded version of the C++ output operator for VecPositions. This
operator Makes it possible to use VecPositions in output statements (e.g.
cout << v). The x- and y-coordinates of the VecPosition are printed in the
format (x,y).
\param os output stream to which information should be written
\param v a VecPosition which must be printed
\return output stream containing (x,y) */
/*
::ostream& operator <<( ::ostream &os, VecPosition v )
{
return ( os << "( " << v.m_x << ", " << v.m_y << " )" );
}
*/
/*! This method writes the current VecPosition to standard output. It can also
print a polar representation of the current VecPosition.
\param cs a CoordSystemtT indicating whether a POLAR or CARTESIAN
representation of the current VecPosition should be printed */
void VecPosition::Show(CoordSystemT cs) {
if (cs == CARTESIAN)
printf("x = %5.2f, y = %5.2f", m_x, m_y);
else
printf("r = %5.2f, phi = %5.2f", GetMagnitude(), GetDirection());
}
void VecPosition::Print(FILE *file) {
fprintf(file, "%2.2f\t%2.2f", m_x, m_y);
}
/*! This method writes the current VecPosition to a string. It can also write a
polar representation of the current VecPosition.
\param cs a CoordSystemtT indicating whether a POLAR or CARTESIAN
representation of the current VecPosition should be written
\return a string containing a polar or Cartesian representation of the
current VecPosition depending on the value of the boolean argument */
void VecPosition::ToString(char buf[], CoordSystemT cs) {
if (cs == CARTESIAN)
sprintf(buf, "( %5.2f, %5.2f )", GetX(), GetY());
else
sprintf(buf, "( r: %5.2f, phi: %5.2f )", GetMagnitude(), GetDirection());
}
/*! Set method for the x-coordinate of the current VecPosition.
\param dX a double value representing a new x-coordinate
\return a boolean indicating whether the Update was successful */
bool VecPosition::SetX(double dX) {
m_x = dX;
return (true);
}
/*! Get method for the x-coordinate of the current VecPosition.
\return the x-coordinate of the current VecPosition */
double VecPosition::GetX() const {
return (m_x);
}
/*! Set method for the y-coordinate of the current VecPosition.
\param dY a double value representing a new y-coordinate
\return a boolean indicating whether the Update was successful */
bool VecPosition::SetY(double dY) {
m_y = dY;
return (true);
}
/*! Get method for the y-coordinate of the current VecPosition.
\return the y-coordinate of the current VecPosition */
double VecPosition::GetY() const {
return (m_y);
}
/*! This method (re)Sets the coordinates of the current VecPosition. The given
coordinates can either be polar or Cartesian coordinates. This is indicated
by the value of the third argument.
\param dX a double value indicating either a new Cartesian x-coordinate when
cs=CARTESIAN or a new polar r-coordinate (distance) when cs=POLAR
\param dY a double value indicating either a new Cartesian y-coordinate when
cs=CARTESIAN or a new polar phi-coordinate (angle) when cs=POLAR
\param cs a CoordSystemT indicating whether x and y denote cartesian
coordinates or polar coordinates */
void VecPosition::SetVecPosition(double dX, double dY, CoordSystemT cs) {
if (cs == CARTESIAN) {
m_x = dX;
m_y = dY;
} else
*this = GetVecPositionFromPolar(dX, dY);
}
/*! This method determines the distance between the current VecPosition and a
given VecPosition. This is equal to the magnitude (length) of the vector
connecting the two positions which is the difference vector between them.
\param p a Vecposition
\return the distance between the current VecPosition and the given
VecPosition */
double VecPosition::GetDistanceTo(const VecPosition p) {
return ((*this - p).GetMagnitude());
}
/*! This method adjusts the coordinates of the current VecPosition in such a way
that the magnitude of the corresponding vector equals the double value which
is supplied as an argument. It thus scales the vector to a given length by
multiplying both the x- and y-coordinates by the quotient of the argument
and the current magnitude. This changes the VecPosition itself.
\param d a double value representing a new magnitude
\return the result of scaling the vector corresponding with the current
VecPosition to the given magnitude thus yielding a different VecPosition */
VecPosition VecPosition::SetMagnitude(double d) {
if (GetMagnitude() > EPS)
(*this) *= (d / GetMagnitude());
return (*this);
}
/*! This method determines the magnitude (length) of the vector corresponding
with the current VecPosition using the formula of Pythagoras.
\return the length of the vector corresponding with the current
VecPosition */
double VecPosition::GetMagnitude() const {
return (sqrt(m_x * m_x + m_y * m_y));
}
/*! This method determines the direction of the vector corresponding with the
current VecPosition (the phi-coordinate in polar representation) using the
arc tangent function. Note that the signs of x and y have to be taken into
account in order to determine the correct quadrant.
\return the direction in degrees of the vector corresponding with the
current VecPosition */
AngRad VecPosition::GetDirection() const {
return (atan2(m_y, m_x));
}
/*! This method determines whether the current VecPosition is in front of a
given VecPosition, i.e. whether the x-coordinate of the current VecPosition
is larger than the x-coordinate of the given VecPosition.
\param p a VecPosition to which the current VecPosition must be compared
\return true when the current VecPosition is in front of the given
VecPosition; false otherwise */
bool VecPosition::IsRightOf(const VecPosition &p) {
return m_x >= p.GetX();
}
/*! This method determines whether the x-coordinate of the current VecPosition
is in front of (i.e. larger than) a given double value.
\param d a double value to which the current x-coordinate must be compared
\return true when the current x-coordinate is in front of the given value;
false otherwise */
bool VecPosition::IsRightOf(const double &d) {
return m_x >= d;
}
/*! This method determines whether the current VecPosition is behind a given
VecPosition, i.e. whether the x-coordinate of the current VecPosition is
smaller than the x-coordinate of the given VecPosition.
\param p a VecPosition to which the current VecPosition must be compared
\return true when the current VecPosition is behind the given VecPosition;
false otherwise */
bool VecPosition::IsLeftOf(const VecPosition &p) {
return m_x <= p.GetX();
}
/*! This method determines whether the x-coordinate of the current VecPosition
is behind (i.e. smaller than) a given double value.
\param d a double value to which the current x-coordinate must be compared
\return true when the current x-coordinate is behind the given value; false
otherwise */
bool VecPosition::IsLeftOf(const double &d) {
return m_x <= d;
}
/*! This method determines whether the current VecPosition is to the left of a
given VecPosition, i.e. whether the y-coordinate of the current VecPosition
is smaller than the y-coordinate of the given VecPosition.
\param p a VecPosition to which the current VecPosition must be compared
\return true when the current VecPosition is to the left of the given
VecPosition; false otherwise */
bool VecPosition::IsButtomOf(const VecPosition &p) {
return m_y <= p.GetY();
}
/*! This method determines whether the y-coordinate of the current VecPosition
is to the left of (i.e. smaller than) a given double value.
\param d a double value to which the current y-coordinate must be compared
\return true when the current y-coordinate is to the left of the given
value; false otherwise */
bool VecPosition::IsButtomOf(const double &d) {
return m_y <= d;
}
/*! This method determines whether the current VecPosition is to the right of a
given VecPosition, i.e. whether the y-coordinate of the current VecPosition
is larger than the y-coordinate of the given VecPosition.
\param p a VecPosition to which the current VecPosition must be compared
\return true when the current VecPosition is to the right of the given
VecPosition; false otherwise */
bool VecPosition::IsTopOf(const VecPosition &p) {
return m_y >= p.GetY();
}
/*! This method determines whether the y-coordinate of the current VecPosition
is to the right of (i.e. larger than) a given double value.
\param d a double value to which the current y-coordinate must be compared
\return true when the current y-coordinate is to the right of the given
value; false otherwise */
bool VecPosition::IsTopOf(const double &d) {
return m_y >= d;
}
/*! This method determines whether the current VecPosition is in between two
given VecPositions when looking in the x-direction, i.e. whether the current
VecPosition is in front of the first argument and behind the second.
\param p1 a VecPosition to which the current VecPosition must be compared
\param p2 a VecPosition to which the current VecPosition must be compared
\return true when the current VecPosition is in between the two given
VecPositions when looking in the x-direction; false otherwise */
bool VecPosition::IsBetweenX(const VecPosition &p1, const VecPosition &p2) {
return IsRightOf(p1) && IsLeftOf(p2);
}
/*! This method determines whether the x-coordinate of the current VecPosition
is in between two given double values, i.e. whether the x-coordinate of the
current VecPosition is in front of the first argument and behind the second.
\param d1 a double value to which the current x-coordinate must be compared
\param d2 a double value to which the current x-coordinate must be compared
\return true when the current x-coordinate is in between the two given
values; false otherwise */
bool VecPosition::IsBetweenX(const double &d1, const double &d2) {
return IsRightOf(d1) && IsLeftOf(d2);
}
/*! This method determines whether the current VecPosition is in between two
given VecPositions when looking in the y-direction, i.e. whether the current
VecPosition is to the right of the first argument and to the left of the
second.
\param p1 a VecPosition to which the current VecPosition must be compared
\param p2 a VecPosition to which the current VecPosition must be compared
\return true when the current VecPosition is in between the two given
VecPositions when looking in the y-direction; false otherwise */
bool VecPosition::IsBetweenY(const VecPosition &p1, const VecPosition &p2) {
return IsTopOf(p1) && IsButtomOf(p2);
}
/*! This method determines whether the y-coordinate of the current VecPosition
is in between two given double values, i.e. whether the y-coordinate of the
current VecPosition is to the right of the first argument and to the left
of the second.
\param d1 a double value to which the current y-coordinate must be compared
\param d2 a double value to which the current y-coordinate must be compared
\return true when the current y-coordinate is in between the two given
values; false otherwise */
bool VecPosition::IsBetweenY(const double &d1, const double &d2) {
return IsTopOf(d1) && IsButtomOf(d2);
}
/*! This method Normalizes a VecPosition by Setting the magnitude of the
corresponding vector to 1. This thus changes the VecPosition itself.
\return the result of normalizing the current VecPosition thus yielding a
different VecPosition */
VecPosition VecPosition::Normalize() {
return (SetMagnitude(1.0));
}
/*! This method Rotates the vector corresponding to the current VecPosition over
a given angle thereby changing the current VecPosition itself. This is done
by calculating the polar coordinates of the current VecPosition and adding
the given angle to the phi-coordinate in the polar representation. The polar
coordinates are then converted back to Cartesian coordinates to obtain the
desired result.
\param angle an angle in degrees over which the vector corresponding to the
current VecPosition must be Rotated
\return the result of rotating the vector corresponding to the current
VecPosition over the given angle thus yielding a different VecPosition */
VecPosition VecPosition::Rotate(AngRad angle) {
// determine the polar representation of the current VecPosition
double dMag = this->GetMagnitude();
double dNewDir = this->GetDirection() + angle; // add rotation angle to phi
SetVecPosition(dMag, dNewDir, POLAR); // convert back to Cartesian
return (*this);
}
/*! This method converts the coordinates of the current VecPosition (which are
represented in an global coordinate system with the origin at (0,0)) into
relative coordinates in a different coordinate system (e.g. relative to a
player). The new coordinate system is defined by the arguments to the
method. The relative coordinates are now obtained by aligning the relative
coordinate system with the global coordinate system using a translation to
Make both origins coincide followed by a rotation to align the axes.
\param origin the origin of the relative coordinate frame
\param ang the angle between the world frame and the relative frame
(reasoning from the world frame)
\return the result of converting the current global VecPosition into a
relative VecPosition */
VecPosition VecPosition::GlobalToRelative(VecPosition origin, AngRad ang) {
// convert global coordinates into relative coordinates by aligning relative
// frame and world frame. First perform translation to Make origins of both
// frames coincide. Then perform rotation to Make axes of both frames coincide
// (use negative angle since you Rotate relative frame to world frame).
*this -= origin;
return (Rotate(-ang));
}
/*! This method converts the coordinates of the current VecPosition (which are
represented in a relative coordinate system) into global coordinates in
the world frame (with origin at (0,0)). The relative coordinate system is
defined by the arguments to the method. The global coordinates are now
obtained by aligning the world frame with the relative frame using a
rotation to align the axes followed by a translation to Make both origins
coincide.
\param origin the origin of the relative coordinate frame
\param ang the angle between the world frame and the relative frame
(reasoning from the world frame)
\return the result of converting the current relative VecPosition into an
global VecPosition */
VecPosition VecPosition::RelativeToGlobal(VecPosition origin, AngRad ang) {
// convert relative coordinates into global coordinates by aligning world
// frame and relative frame. First perform rotation to Make axes of both
// frames coincide (use positive angle since you Rotate world frame to
// relative frame). Then perform translation to Make origins of both frames
// coincide.
Rotate(ang);
*this += origin;
return (*this);
}
/*! This method returns a VecPosition that lies somewhere on the vector between
the current VecPosition and a given VecPosition. The desired position is
specified by a given fraction of this vector (e.g. 0.5 means exactly in
the middle of the vector). The current VecPosition itself is left unchanged.
\param p a VecPosition which defines the vector to the current VecPosition
\param dFrac double representing the fraction of the connecting vector at
which the desired VecPosition lies.
\return the VecPosition which lies at fraction dFrac on the vector
connecting p and the current VecPosition */
VecPosition VecPosition::GetVecPositionOnLineFraction(VecPosition &p,
double dFrac) {
// determine point on line that lies at fraction dFrac of whole line
// example: this --- 0.25 --------- p
// formula: this + dFrac * ( p - this ) = this - dFrac * this + dFrac * p =
// ( 1 - dFrac ) * this + dFrac * p
return ((*this) * (1.0 - dFrac) + (p * dFrac));
}
/*! This method converts a polar representation of a VecPosition into a
Cartesian representation.
\param dMag a double representing the polar r-coordinate, i.e. the distance
from the point to the origin
\param ang the angle that the polar vector Makes with the x-axis, i.e. the
polar phi-coordinate
\return the result of converting the given polar representation into a
Cartesian representation thus yielding a Cartesian VecPosition */
VecPosition VecPosition::GetVecPositionFromPolar(double dMag, AngRad ang) {
// cos(phi) = x/r <=> x = r*cos(phi); sin(phi) = y/r <=> y = r*sin(phi)
return (VecPosition(dMag * cos(ang), dMag * sin(ang)));
}
/*! This method Normalizes an angle. This means that the resulting angle lies
between -PI and PI degrees.
\param angle the angle which must be Normalized
\return the result of normalizing the given angle */
AngRad VecPosition::NormalizeAngle(AngRad angle) {
while (angle > M_PI) angle -= 2 * M_PI;
while (angle < -M_PI) angle += 2 * M_PI;
return (angle);
}
AngRad VecPosition::NormalizeAngle2PI(AngRad angle) {
while (angle > 2 * M_PI) angle -= 2 * M_PI;
while (angle < 0) angle += 2 * M_PI;
return (angle);
}
/******************************************************************************/
/*********************** CLASS GEOMETRY ***************************************/
/******************************************************************************/
/*! A geometric series is one in which there is a constant ratio between each
element and the one preceding it. This method determines the
length of a geometric series given its first element, the sum of the
elements in the series and the constant ratio between the elements.
Normally: s = a + ar + ar^2 + ... + ar^n
Now: dSum = dFirst + dFirst*dRatio + dFirst*dRatio^2 + .. + dFist*dRatio^n
\param dFirst first term of the series
\param dRatio ratio with which the the first term is multiplied
\param dSum the total sum of all the serie
\return the length(n in above example) of the series */
double Geometry::GetLengthGeomSeries(double dFirst, double dRatio, double dSum) {
// s = a + ar + ar^2 + .. + ar^n-1 and thus sr = ar + ar^2 + .. + ar^n
// subtract: sr - s = - a + ar^n) => s(1-r)/a + 1 = r^n = temp
// log r^n / n = n log r / log r = n = length
double temp = (dSum * (dRatio - 1) / dFirst) + 1;
if (temp <= 0)
return -1.0;
return log(temp) / log(dRatio);
}
/*! A geometric series is one in which there is a constant ratio between each
element and the one preceding it. This method determines the sum of a
geometric series given its first element, the ratio and the number of steps
in the series
Normally: s = a + ar + ar^2 + ... + ar^n
Now: dSum = dFirst + dFirst*dRatio + ... + dFirst*dRatio^dSteps
\param dFirst first term of the series
\param dRatio ratio with which the the first term is multiplied
\param dSum the number of steps to be taken into account
\return the sum of the series */
double Geometry::GetSumGeomSeries(double dFirst, double dRatio, double dLength) {
// s = a + ar + ar^2 + .. + ar^n-1 and thus sr = ar + ar^2 + .. + ar^n
// subtract: s - sr = a - ar^n) => s = a(1-r^n)/(1-r)
return dFirst * (1 - pow(dRatio, dLength)) / (1 - dRatio);
}
/*! A geometric series is one in which there is a constant ratio between each
element and the one preceding it. This method determines the sum of an
infinite geometric series given its first element and the constant ratio
between the elements. Note that such an infinite series will only converge
when 0<r<1.
Normally: s = a + ar + ar^2 + ar^3 + ....
Now: dSum = dFirst + dFirst*dRatio + dFirst*dRatio^2...
\param dFirst first term of the series
\param dRatio ratio with which the the first term is multiplied
\return the sum of the series */
double Geometry::GetSumInfGeomSeries(double dFirst, double dRatio) {
// s = a(1-r^n)/(1-r) with n->inf and 0<r<1 => r^n = 0
return dFirst / (1 - dRatio);
}
/*! A geometric series is one in which there is a constant ratio between each
element and the one preceding it. This method determines the first element
of a geometric series given its element, the ratio and the number of steps
in the series
Normally: s = a + ar + ar^2 + ... + ar^n
Now: dSum = dFirst + dFirst*dRatio + ... + dFirst*dRatio^dSteps
\param dSum sum of the series
\param dRatio ratio with which the the first term is multiplied
\param dSum the number of steps to be taken into account
\return the first element (a) of a serie */
double Geometry::GetFirstGeomSeries(double dSum, double dRatio, double dLength) {
// s = a + ar + ar^2 + .. + ar^n-1 and thus sr = ar + ar^2 + .. + ar^n
// subtract: s - sr = a - ar^n) => s = a(1-r^n)/(1-r) => a = s*(1-r)/(1-r^n)
return dSum * (1 - dRatio) / (1 - pow(dRatio, dLength));
}
/*! A geometric series is one in which there is a constant ratio between each
element and the one preceding it. This method determines the first element
of an infinite geometric series given its first element and the constant
ratio between the elements. Note that such an infinite series will only
converge when 0<r<1.
Normally: s = a + ar + ar^2 + ar^3 + ....
Now: dSum = dFirst + dFirst*dRatio + dFirst*dRatio^2...
\param dSum sum of the series
\param dRatio ratio with which the the first term is multiplied
\return the first term of the series */
double Geometry::GetFirstInfGeomSeries(double dSum, double dRatio) {
// s = a(1-r^n)/(1-r) with r->inf and 0<r<1 => r^n = 0 => a = s ( 1 - r)
return dSum * (1 - dRatio);
}
/*! This method performs the abc formula (Pythagoras' Theorem) on the given
parameters and puts the result in *s1 en *s2. It returns the number of
found coordinates.
\param a a parameter in abc formula
\param b b parameter in abc formula
\param c c parameter in abc formula
\param *s1 first result of abc formula
\param *s2 second result of abc formula
\return number of found x-coordinates */
int Geometry::AbcFormula(double a, double b, double c, double *s1, double *s2) {
double dDiscr = b * b - 4 * a * c; // discriminant is b^2 - 4*a*c
if (fabs(dDiscr) < EPS) // if discriminant = 0
{
*s1 = -b / (2 * a); // only one solution
return 1;
} else if (dDiscr < 0) // if discriminant < 0
return 0; // no solutions
else // if discriminant > 0
{
dDiscr = sqrt(dDiscr); // two solutions
*s1 = (-b + dDiscr) / (2 * a);
*s2 = (-b - dDiscr) / (2 * a);
return 2;
}
}
/******************************************************************************/
/********************** CLASS CIRCLE ******************************************/
/******************************************************************************/
/*! This is the constructor of a circle.
\param pos first point that defines the center of circle
\param dR the radius of the circle
\return circle with pos as center and radius as radius*/
Circle::Circle(VecPosition pos, double dR) {
SetCircle(pos, dR);
}
/*! This is the constructor of a circle which initializes a circle with a
radius of zero. */
Circle::Circle() {
SetCircle(VecPosition(-1000.0, -1000.0), 0);
}
/*! This method prints the circle information to the specified output stream
in the following format: "c: (c_x,c_y), r: rad" where (c_x,c_y) denotes
the center of the circle and rad the radius.
\param os output stream to which output is written. */
void Circle::Show() {
printf("c:");
m_posCenter.Show();
printf(", r: %5.2f \n", m_dRadius);
}