@@ -3,16 +3,18 @@ package sc.plugin2022
3
3
import com.thoughtworks.xstream.annotations.XStreamAlias
4
4
import com.thoughtworks.xstream.annotations.XStreamAsAttribute
5
5
import sc.plugin2022.util.Constants
6
- import kotlin.math.min
6
+ import kotlin.math.abs
7
+ import kotlin.math.sqrt
7
8
8
9
/* * Eine 2D Koordinate der Form (x, y). */
9
10
@XStreamAlias(value = " coordinates" )
10
11
data class Coordinates (
11
12
@XStreamAsAttribute val x : Int ,
12
- @XStreamAsAttribute val y : Int ) {
13
+ @XStreamAsAttribute val y : Int ,
14
+ ) {
13
15
14
16
override fun toString (): String = " [$x |$y ]"
15
-
17
+
16
18
/* * Addiere den [Vector] auf die [Coordinates] auf. */
17
19
operator fun plus (vector : Vector ): Coordinates {
18
20
return Coordinates (x + vector.dx, y + vector.dy)
@@ -27,7 +29,7 @@ data class Coordinates(
27
29
}
28
30
/* * Wandelt die [Coordinates] in einen entsprechenden [Vector]. */
29
31
operator fun unaryPlus (): Vector = Vector (x, y)
30
-
32
+
31
33
/* * Gibt ein Set der vier benachbarten Felder dieser Koordinaten zurück. */
32
34
val neighbors: Set <Coordinates >
33
35
get() = Vector .cardinals.mapTo(HashSet ()) { this + it }
@@ -48,27 +50,31 @@ data class Coordinates(
48
50
* @property dx die Differenz in x-Richtung
49
51
* @property dy die Differenz in y-Richtung
50
52
*/
51
- data class Vector (val dx : Int , val dy : Int ) {
53
+ data class Vector (val dx : Int , val dy : Int ): Comparable<Vector> {
52
54
/* * Die Fläche des Rechtecks, dessen Diagonale der Vector ist. */
53
- val area: Int = dx * dy
54
-
55
- /* * Verändert die Länge des Vectors um den gegebenen Faktor, ohne seine Richtung zu ändern. */
56
- operator fun times (scalar : Int ): Vector {
57
- return Vector (scalar * dx, scalar * dy)
58
- }
59
-
55
+ val area: Int
56
+ get() = abs(dx * dy)
57
+
58
+ private val comparableLength: Int
59
+ get() = dx * dx + dy * dy
60
+
61
+ val length: Double
62
+ get() = sqrt(comparableLength.toDouble())
63
+
64
+ /* * Verändert die Länge des Vektors um den gegebenen Faktor, ohne seine Richtung zu ändern. */
65
+ operator fun times (scalar : Int ): Vector =
66
+ Vector (scalar * dx, scalar * dy)
67
+
60
68
/* *
61
- * Vergleicht die beiden Vektoren. Der Rückgabewert ist
62
- * - positiv, wenn beide Größen dieses Vektors kleiner sind als die des anderen.
63
- * - null, wenn beide Vektoren gleich groß sind.
64
- * - negativ, wenn mindestens eine Größe dieses Vektors größer als die des anderen ist.
69
+ * Vergleicht die Länge dieses Vektors mit einem anderen.
70
+ * @return groesser Null, wenn dieser laenger ist
65
71
*/
66
- operator fun compareTo (other : Vector ): Int =
67
- min(other.dx - dx, other.dy - dy)
68
-
72
+ override operator fun compareTo (other : Vector ): Int =
73
+ comparableLength - other.comparableLength
74
+
69
75
/* * Konvertiert den Vektor zu entsprechenden [Coordinates]. */
70
76
operator fun unaryPlus (): Coordinates = Coordinates (dx, dy)
71
-
77
+
72
78
companion object {
73
79
/* * Die vier Vektoren in diagonaler Richtung. */
74
80
val diagonals: Array <Vector > = arrayOf(
0 commit comments