@@ -8,6 +8,10 @@ import kotlin.math.absoluteValue
88 * This class represents an angle in degrees in the range of (-180,180]
99 *
1010 * The constructor also accepts a special value for undefined angle which is defined by [UNDEFINED_ANGLE]
11+ *
12+ * The [plus] and [minus] operators are overloaded to support two operations:
13+ * 1. [plus] allows adding a positive or negative number (or an `Angle` object) to the current angle, resulting in a new angle
14+ * 2. [minus] allows calculating the absolute difference between two angles
1115 */
1216class Angle (
1317 val floatValue : Float
@@ -22,6 +26,9 @@ class Angle(
2226 val doubleValue: Double
2327 get() = floatValue.toDouble()
2428
29+ /* *
30+ * @return absolute difference between this angle and the other angle
31+ */
2532 operator fun minus (other : Angle ): Angle {
2633 // handle the gap between 2nd and 3rd quadrants
2734 val diff = if (this .isInQuadrant(2 ) && other.isInQuadrant(3 )){
@@ -37,21 +44,20 @@ class Angle(
3744 return Angle (diff.absoluteValue)
3845 }
3946
40- operator fun minus (other : Number ): Angle {
41- return minus(Angle (other.toFloat()))
42- }
43-
44- operator fun plus (other : Angle ): Angle {
45- var added = this .floatValue + other.floatValue
47+ /* *
48+ * @return a new angle that is the sum of this angle and the other angle
49+ */
50+ operator fun plus (other : Float ): Angle {
51+ var added = this .floatValue + other
4652
4753 // handle the gap between 2nd and 3rd quadrants
48- added = if (this .isInQuadrant(2 ) && other.floatValue > 0 ){
54+ added = if (this .isInQuadrant(2 ) && other > 0 ){
4955 if (added > 180 ){
5056 added - 360
5157 } else {
5258 added
5359 }
54- } else if (this .isInQuadrant(3 ) && other.floatValue < 0 ){
60+ } else if (this .isInQuadrant(3 ) && other < 0 ){
5561 if (added <= - 180 ){
5662 added + 360
5763 } else {
@@ -65,16 +71,18 @@ class Angle(
6571 return Angle (added)
6672 }
6773
68- operator fun plus (other : Number ): Angle {
69- return plus(Angle (other.toFloat()))
70- }
71-
72- operator fun compareTo (other : Angle ): Int {
73- return floatValue.compareTo(other.floatValue)
74+ /* *
75+ * @return a new angle that is the sum of this angle and the other angle
76+ */
77+ operator fun plus (other : Angle ): Angle {
78+ return plus(other.floatValue)
7479 }
7580
76- operator fun compareTo (other : Number ): Int {
77- return compareTo(Angle (other.toFloat()))
81+ /* *
82+ * @return a new angle that is the sum of this angle and the other angle
83+ */
84+ operator fun plus (other : Number ): Angle {
85+ return plus(other.toFloat())
7886 }
7987
8088 fun isBetweenInclusive (min : Float , max : Float ) = min <= floatValue && floatValue <= max
@@ -96,6 +104,11 @@ class Angle(
96104 return floatValue.hashCode()
97105 }
98106
107+
108+ private fun isInQuadrant (@IntRange(1 ,4 ) quadrant : Int ) : Boolean {
109+ return floatValue.isInQuadrant(quadrant)
110+ }
111+
99112 /* *
100113 * this function assumes that the angle is in the range of (-180,180]
101114 * and the quadrant is in the range of [1,4]
@@ -108,19 +121,20 @@ class Angle(
108121 *
109122 * meaning, clockwise side of the quadrant is inclusive and the counter-clockwise side is exclusive
110123 */
111- private fun isInQuadrant (@IntRange(1 ,4 ) quadrant : Int ) : Boolean {
124+ private fun Float. isInQuadrant (@IntRange(1 ,4 ) quadrant : Int ) : Boolean {
112125 return when (quadrant){
113- 1 -> 0f < floatValue && floatValue <= 90f
114- 2 -> 90f < floatValue && floatValue <= 180f
115- 3 -> - 180f < floatValue && floatValue <= - 90f
116- 4 -> - 90f < floatValue && floatValue <= 0f
126+ 1 -> 0f < this && this <= 90f
127+ 2 -> 90f < this && this <= 180f
128+ 3 -> - 180f < this && this <= - 90f
129+ 4 -> - 90f < this && this <= 0f
117130 else -> throw IllegalArgumentException (" Invalid quadrant: $quadrant " )
118131 }
119132 }
120133
121134 companion object {
122135 /* *
123136 * This function assumes that the angles are in the range of (-180,180]
137+ * and that the difference between the angles is less than 180 degrees
124138 */
125139 fun isClockwise (
126140 from : Angle ,
@@ -134,7 +148,7 @@ class Angle(
134148 }
135149 // simple case
136150 else {
137- from < to
151+ from.floatValue < to.floatValue
138152 }
139153 }
140154 }
0 commit comments