Skip to content

Commit

Permalink
Some more refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
igr committed Oct 25, 2024
1 parent 3cf68b6 commit 7255abf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
13 changes: 12 additions & 1 deletion gart/src/main/kotlin/dev/oblac/gart/angles/Angles.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.oblac.gart.angles

import dev.oblac.gart.math.DOUBLE_PIf
import dev.oblac.gart.math.HALF_PIf
import dev.oblac.gart.math.PIf

@JvmInline
Expand Down Expand Up @@ -39,7 +40,10 @@ value class Radians(internal val value: Float) {
}

companion object {
val ZERO: Radians = Radians(0f)
val ZERO = Radians(0f)
val PI_HALF = Radians(HALF_PIf)
val PI = Radians(PIf)
val TWO_PI = Radians(DOUBLE_PIf)
}
}

Expand Down Expand Up @@ -91,6 +95,13 @@ value class Degrees(internal val value: Float) {
}
return Degrees(result)
}

companion object {
val ZERO = Degrees(0f)
val D90 = Degrees(90f)
val D180 = Degrees(180f)
val D360 = Degrees(360f)
}
}

fun cos(d: Degrees): Double =
Expand Down
6 changes: 1 addition & 5 deletions gart/src/main/kotlin/dev/oblac/gart/math/math.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ package dev.oblac.gart.math
import kotlin.math.cos
import kotlin.math.sin


fun Int.isEven() = this % 2 == 0
fun Int.isOdd() = this % 2 == 1

fun Float.toRadian(): Float = (this / 180 * Math.PI).toFloat()
fun Double.toRadian(): Double = this / 180 * Math.PI

fun Float.toDegree(): Float = (this * 180 / Math.PI).toFloat()
fun Double.toDegree(): Double = this * 180 / Math.PI

/**
* Safe degrees' subtraction.
Expand Down Expand Up @@ -39,7 +35,7 @@ fun normalizeRad(rad: Float): Float {
return result
}

fun normalizeDeg(deg: Float): Float {
private fun normalizeDeg(deg: Float): Float {
var result = deg
while (result < 0) {
result += 360
Expand Down
3 changes: 2 additions & 1 deletion gart/src/main/kotlin/dev/oblac/gart/math/vector.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.oblac.gart.math

import dev.oblac.gart.angles.Radians
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.sin
Expand All @@ -24,7 +25,7 @@ data class Vector2(val x: Float, val y: Float) {
/**
* Returns the angle of the vector in radians.
*/
val angle by lazy { atan2(y, x) }
val angle by lazy { Radians(atan2(y, x)) }

companion object {
val ZERO = Vector2(0f, 0f)
Expand Down
9 changes: 5 additions & 4 deletions gart/src/test/kotlin/dev/oblac/gart/math/Vector2Test.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package dev.oblac.gart.math

import dev.oblac.gart.angles.Radians
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

class Vector2Test {

@Test
fun testAngle() {
assertEquals(0f, Vector2(1f, 0f).angle, 0.0001f)
assertEquals(PIf / 2, Vector2(0f, 1f).angle, 0.0001f)
assertEquals(PIf, Vector2(-1f, 0f).angle, 0.0001f)
assertEquals(-PIf / 2, Vector2(0f, -1f).angle, 0.0001f)
assertEquals(Radians.ZERO.toFloat(), Vector2(1f, 0f).angle.toFloat(), 0.0001f)
assertEquals(Radians.PI_HALF.toFloat(), Vector2(0f, 1f).angle.toFloat(), 0.0001f)
assertEquals(Radians.PI.toFloat(), Vector2(-1f, 0f).angle.toFloat(), 0.0001f)
assertEquals(Radians(-PIf / 2).toFloat(), Vector2(0f, -1f).angle.toFloat(), 0.0001f)
}
}

0 comments on commit 7255abf

Please sign in to comment.