Implement cross product
This commit is contained in:
parent
130c8a5bd1
commit
c4dfbfd3ed
1 changed files with 7 additions and 1 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue