Implement cross product

This commit is contained in:
Sven Weidauer 2024-12-01 12:38:28 +01:00
parent 130c8a5bd1
commit c4dfbfd3ed

View file

@ -4,7 +4,7 @@ import kotlin.math.sqrt
data class Vector(val x: Float, val y: Float, val z: Float) {
val length: Float get() = sqrt(squaredLength)
val squaredLength: Float get() = x * x + y * y + z * z
val squaredLength: Float get() = this dot this
fun normalized(): Vector = this / length
@ -14,6 +14,12 @@ data class Vector(val x: Float, val y: Float, val z: Float) {
operator fun minus(rhs: Vector): Vector = Vector(x - rhs.x, y - rhs.y, z - rhs.z)
operator fun div(rhs: Float): Vector = Vector(x / rhs, y / rhs, z / rhs)
operator fun times(rhs: Vector): Vector = Vector(
y * rhs.z - z * rhs.y,
z * rhs.x - x * rhs.z,
x * rhs.y - y * rhs.x
)
infix fun dot(rhs: Vector): Float = x * rhs.x + y * rhs.y + z * rhs.z
}