Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vector convenience functions. #759

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ final case class Vector2(x: Double, y: Double) derives CanEqual:
def invert: Vector2 =
Vector2(-x, -y)

def `unary_-` : Vector2 = invert

def translate(vec: Vector2): Vector2 =
Vector2.add(this, vec)

Expand Down
16 changes: 16 additions & 0 deletions indigo/indigo/src/main/scala/indigo/shared/datatypes/Vector3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ final case class Vector3(x: Double, y: Double, z: Double) derives CanEqual:
def invert: Vector3 =
Vector3(-x, -y, -z)

def `unary_-` : Vector3 = invert

def translate(vec: Vector3): Vector3 =
Vector3.add(this, vec)

Expand Down Expand Up @@ -85,6 +87,9 @@ final case class Vector3(x: Double, y: Double, z: Double) derives CanEqual:
def dot(other: Vector3): Double =
Vector3.dotProduct(this, other)

def cross(other: Vector3): Vector3 =
Vector3.crossProduct(this, other)

def normalise: Vector3 =
val magnitude = length
if magnitude == 0 then Vector3.zero
Expand Down Expand Up @@ -126,6 +131,10 @@ object Vector3:
val zero: Vector3 = Vector3(0d, 0d, 0d)
val one: Vector3 = Vector3(1d, 1d, 1d)

val unitX: Vector3 = Vector3(1d, 0d, 0d)
val unitY: Vector3 = Vector3(0d, 1d, 0d)
val unitZ: Vector3 = Vector3(0d, 0d, 1d)

def fromPoint(point: Point): Vector3 =
Vector3(point.x.toDouble, point.y.toDouble, 0)

Expand All @@ -150,6 +159,13 @@ object Vector3:
def dotProduct(vec1: Vector3, vec2: Vector3): Double =
(vec1.x * vec2.x) + (vec1.y * vec2.y) + (vec1.z * vec2.z)

def crossProduct(vec1: Vector3, vec2: Vector3): Vector3 =
Vector3(
vec1.y * vec2.z - vec1.z * vec2.y,
vec1.z * vec2.x - vec1.x * vec2.z,
vec1.x * vec2.y - vec1.y * vec2.x
)

def distance(v1: Vector3, v2: Vector3): Double =
Math.sqrt(Math.abs(Math.pow(v2.x - v1.x, 2) + Math.pow(v2.y - v1.y, 2) + Math.pow(v2.z - v1.z, 2)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ final case class Vector4(x: Double, y: Double, z: Double, w: Double) derives Can
def invert: Vector4 =
Vector4(-x, -y, -z, -w)

def `unary_-` : Vector4 = invert

def translate(vec: Vector4): Vector4 =
Vector4.add(this, vec)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ class Vector3Tests extends munit.FunSuite {
assert(clue(Vector3.mod(Vector3(-11), Vector3(-10))) ~== clue(Vector3(-1)))
}

test("cross product") {
assertEquals(Vector3.unitX.cross(Vector3.unitY), Vector3.unitZ)
assertEquals(Vector3.unitY.cross(Vector3.unitZ), Vector3.unitX)
assertEquals(Vector3.unitX.cross(Vector3.unitZ), -Vector3.unitY)
}

def to2dp(d: Double): Double =
Math.round(d * 100).toDouble / 100

Expand Down
Loading