From 16666055baa885b47ec293606341229817025ac3 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 29 Feb 2024 10:48:39 +0700 Subject: [PATCH] 2D cross product --- gradle.properties | 2 +- .../ru/dbotthepony/kommons/vector/Vector2d.kt | 21 +++++++++++++++++++ .../ru/dbotthepony/kommons/vector/Vector2f.kt | 21 +++++++++++++++++++ .../ru/dbotthepony/kommons/vector/Vector2i.kt | 21 +++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 2db2647..c980a9f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ kotlin.code.style=official specifyKotlinAsDependency=false projectGroup=ru.dbotthepony.kommons -projectVersion=2.9.15 +projectVersion=2.9.16 guavaDepVersion=33.0.0 gsonDepVersion=2.8.9 diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2d.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2d.kt index 0583273..832e2cf 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2d.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2d.kt @@ -111,6 +111,27 @@ data class Vector2d( operator fun unaryMinus(): Vector2d = Vector2d(-x, -y) + /** + * 2D vector cross product (Z coordinate) + */ + fun cross(other: IStruct2d): Double { + return x * other.component2() - other.component1() * y + } + + /** + * 2D vector cross product (Z coordinate) + */ + fun cross(other: IStruct2f): Double { + return x * other.component2() - other.component1() * y + } + + /** + * 2D vector cross product (Z coordinate) + */ + fun cross(other: IStruct2i): Double { + return x * other.component2() - other.component1() * y + } + fun dot(x: Double, y: Double) = this.x * x + this.y * y fun dot(value: IStruct2d): Double { val (x, y) = value; return dot(x, y) } fun dot(value: Vector2d): Double { val (x, y) = value; return dot(x, y) } diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2f.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2f.kt index 09e4fb8..58a3c53 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2f.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2f.kt @@ -111,6 +111,27 @@ data class Vector2f( operator fun unaryMinus(): Vector2f = Vector2f(-x, -y) + /** + * 2D vector cross product (Z coordinate) + */ + fun cross(other: IStruct2f): Float { + return x * other.component2() - other.component1() * y + } + + /** + * 2D vector cross product (Z coordinate) + */ + fun cross(other: IStruct2i): Float { + return x * other.component2() - other.component1() * y + } + + /** + * 2D vector cross product (Z coordinate) + */ + fun cross(other: IStruct2d): Double { + return x * other.component2() - other.component1() * y + } + fun dot(x: Float, y: Float) = this.x * x + this.y * y fun dot(value: IStruct2f): Float { val (x, y) = value; return dot(x, y) } fun dot(value: Vector2f): Float { val (x, y) = value; return dot(x, y) } diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2i.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2i.kt index 719191a..3cf5be6 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2i.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2i.kt @@ -88,6 +88,27 @@ data class Vector2i( operator fun unaryMinus(): Vector2i = Vector2i(-x, -y) + /** + * 2D vector cross product (Z coordinate) + */ + fun cross(other: IStruct2f): Float { + return x * other.component2() - other.component1() * y + } + + /** + * 2D vector cross product (Z coordinate) + */ + fun cross(other: IStruct2i): Int { + return x * other.component2() - other.component1() * y + } + + /** + * 2D vector cross product (Z coordinate) + */ + fun cross(other: IStruct2d): Double { + return x * other.component2() - other.component1() * y + } + fun dot(x: Int, y: Int) = this.x * x + this.y * y fun dot(value: IStruct2i): Int { val (x, y) = value; return dot(x, y) } fun dot(value: Vector2i): Int { val (x, y) = value; return dot(x, y) }