diff --git a/arts/flowforce/src/main/kotlin/dev/oblac/gart/flowforce/eclipse/Eclipse.kt b/arts/flowforce/src/main/kotlin/dev/oblac/gart/flowforce/eclipse/Eclipse.kt index 34da753..edfb2fc 100644 --- a/arts/flowforce/src/main/kotlin/dev/oblac/gart/flowforce/eclipse/Eclipse.kt +++ b/arts/flowforce/src/main/kotlin/dev/oblac/gart/flowforce/eclipse/Eclipse.kt @@ -2,6 +2,7 @@ package dev.oblac.gart.flowforce.eclipse import dev.oblac.gart.Dimension import dev.oblac.gart.Gart +import dev.oblac.gart.angles.Radians import dev.oblac.gart.color.BgColors import dev.oblac.gart.color.Colors import dev.oblac.gart.force.Flow @@ -76,7 +77,7 @@ fun drawRays(c: Canvas, d: Dimension, moonR: Float) { val dx = x - d.cx val dy = y - d.cy val theta = atan2(dy, dx) + PIf / 2f + rndf(-0.3f, 0.3f) - Flow(theta, 2f) + Flow(Radians(theta), 2f) } val ff = ForceField.of(gart.d) { x, y -> flow(x, y) } diff --git a/gart/src/main/kotlin/dev/oblac/gart/force/Flow.kt b/gart/src/main/kotlin/dev/oblac/gart/force/Flow.kt index 6c72540..3944c36 100644 --- a/gart/src/main/kotlin/dev/oblac/gart/force/Flow.kt +++ b/gart/src/main/kotlin/dev/oblac/gart/force/Flow.kt @@ -1,10 +1,11 @@ package dev.oblac.gart.force +import dev.oblac.gart.angles.Radians +import dev.oblac.gart.angles.cos +import dev.oblac.gart.angles.middleAngle +import dev.oblac.gart.angles.sin import dev.oblac.gart.math.Vector2 -import dev.oblac.gart.math.middleAngle import org.jetbrains.skia.Point -import kotlin.math.cos -import kotlin.math.sin /** * Flow is a vector that represents the direction and magnitude of a flow. @@ -13,7 +14,7 @@ import kotlin.math.sin * @param direction in radians, indicates the direction of the flow. The angle is measured from the negative x-axis. * 0 is up, PI/2 is right, PI is down, 3PI/2 is left. */ -data class Flow(val direction: Float, val magnitude: Float = 1f) : Force { +data class Flow(val direction: Radians, val magnitude: Float = 1f) : Force { // todo remove? ne treba jer ce sabirati vektori operator fun plus(other: Flow): Flow { diff --git a/gart/src/main/kotlin/dev/oblac/gart/force/FlowGenerators.kt b/gart/src/main/kotlin/dev/oblac/gart/force/FlowGenerators.kt index 8fef040..8525c7c 100644 --- a/gart/src/main/kotlin/dev/oblac/gart/force/FlowGenerators.kt +++ b/gart/src/main/kotlin/dev/oblac/gart/force/FlowGenerators.kt @@ -1,9 +1,9 @@ package dev.oblac.gart.force +import dev.oblac.gart.angles.Radians import dev.oblac.gart.math.PIf import dev.oblac.gart.math.RotationDirection import dev.oblac.gart.math.RotationDirection.CW -import dev.oblac.gart.math.normalizeRad import kotlin.math.atan2 import kotlin.math.cos import kotlin.math.sin @@ -26,7 +26,7 @@ class CircularFlow( RotationDirection.CCW -> -atan2(-dy, dx) } - return Flow(normalizeRad(theta), magnitude) + return Flow(Radians(theta).normalize(), magnitude) } } @@ -46,7 +46,7 @@ class SpiralFlow( RotationDirection.CCW -> -atan2(-dy, dx) } + spiralSpeed - return Flow(normalizeRad(theta), magnitude) + return Flow(Radians(theta).normalize(), magnitude) } } @@ -60,7 +60,7 @@ class WaveFlow( override fun invoke(x: Float, y: Float): Flow { val a = sin(x * xFreq) * xAmp val b = cos(y * yFreq) * yAmp - return Flow(a + b, magnitude) + return Flow(Radians(a + b), magnitude) } } diff --git a/gart/src/main/kotlin/dev/oblac/gart/math/math.kt b/gart/src/main/kotlin/dev/oblac/gart/math/math.kt index 189599f..ee6fddc 100644 --- a/gart/src/main/kotlin/dev/oblac/gart/math/math.kt +++ b/gart/src/main/kotlin/dev/oblac/gart/math/math.kt @@ -45,18 +45,3 @@ private fun normalizeDeg(deg: Float): Float { } return result } - -/** - * Calculates the middle angle between two angles. The result is always in the range of -PI..PI. - * This means that the result is always the shortest angle between the two angles. - */ -fun middleAngle(a: Float, b: Float): Float { - return when (val diff = b - a) { - in -PIf..PIf -> normalizeRad(a + diff / 2) - in PIf..DOUBLE_PIf -> normalizeRad(a + diff / 2 - PIf) - in -DOUBLE_PIf..-PIf -> normalizeRad(a + diff / 2 + PIf) - in DOUBLE_PIf..2 * DOUBLE_PIf -> normalizeRad(a + (diff - DOUBLE_PIf) / 2) - in -2 * DOUBLE_PIf..-DOUBLE_PIf -> normalizeRad(a + (diff + DOUBLE_PIf) / 2) - else -> throw IllegalStateException("Unexpected angle difference: $diff") - } -} diff --git a/gart/src/test/kotlin/dev/oblac/gart/force/FlowTest.kt b/gart/src/test/kotlin/dev/oblac/gart/force/FlowTest.kt index 1b2f1f2..635b7bc 100644 --- a/gart/src/test/kotlin/dev/oblac/gart/force/FlowTest.kt +++ b/gart/src/test/kotlin/dev/oblac/gart/force/FlowTest.kt @@ -1,5 +1,6 @@ package dev.oblac.gart.force +import dev.oblac.gart.angles.Radians import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test @@ -7,8 +8,8 @@ class FlowTest { @Test fun addOppositeFlows() { - val right = Flow(0f, 1f) - val left = Flow(Math.PI.toFloat(), 1f) + val right = Flow(Radians.ZERO, 1f) + val left = Flow(Radians.PI, 1f) val result = right + left