@@ -3,16 +3,18 @@ package sc.plugin2022
33import com.thoughtworks.xstream.annotations.XStreamAlias
44import com.thoughtworks.xstream.annotations.XStreamAsAttribute
55import sc.plugin2022.util.Constants
6- import kotlin.math.min
6+ import kotlin.math.abs
7+ import kotlin.math.sqrt
78
89/* * Eine 2D Koordinate der Form (x, y). */
910@XStreamAlias(value = " coordinates" )
1011data class Coordinates (
1112 @XStreamAsAttribute val x : Int ,
12- @XStreamAsAttribute val y : Int ) {
13+ @XStreamAsAttribute val y : Int ,
14+ ) {
1315
1416 override fun toString (): String = " [$x |$y ]"
15-
17+
1618 /* * Addiere den [Vector] auf die [Coordinates] auf. */
1719 operator fun plus (vector : Vector ): Coordinates {
1820 return Coordinates (x + vector.dx, y + vector.dy)
@@ -27,7 +29,7 @@ data class Coordinates(
2729 }
2830 /* * Wandelt die [Coordinates] in einen entsprechenden [Vector]. */
2931 operator fun unaryPlus (): Vector = Vector (x, y)
30-
32+
3133 /* * Gibt ein Set der vier benachbarten Felder dieser Koordinaten zurück. */
3234 val neighbors: Set <Coordinates >
3335 get() = Vector .cardinals.mapTo(HashSet ()) { this + it }
@@ -48,27 +50,31 @@ data class Coordinates(
4850 * @property dx die Differenz in x-Richtung
4951 * @property dy die Differenz in y-Richtung
5052 */
51- data class Vector (val dx : Int , val dy : Int ) {
53+ data class Vector (val dx : Int , val dy : Int ): Comparable<Vector> {
5254 /* * 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+
6068 /* *
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
6571 */
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+
6975 /* * Konvertiert den Vektor zu entsprechenden [Coordinates]. */
7076 operator fun unaryPlus (): Coordinates = Coordinates (dx, dy)
71-
77+
7278 companion object {
7379 /* * Die vier Vektoren in diagonaler Richtung. */
7480 val diagonals: Array <Vector > = arrayOf(
0 commit comments