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 7255abf commit fd285e8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }

Expand Down
9 changes: 5 additions & 4 deletions gart/src/main/kotlin/dev/oblac/gart/force/Flow.kt
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions gart/src/main/kotlin/dev/oblac/gart/force/FlowGenerators.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,7 +26,7 @@ class CircularFlow(
RotationDirection.CCW -> -atan2(-dy, dx)
}

return Flow(normalizeRad(theta), magnitude)
return Flow(Radians(theta).normalize(), magnitude)
}
}

Expand All @@ -46,7 +46,7 @@ class SpiralFlow(
RotationDirection.CCW -> -atan2(-dy, dx)
} + spiralSpeed

return Flow(normalizeRad(theta), magnitude)
return Flow(Radians(theta).normalize(), magnitude)
}
}

Expand All @@ -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)
}

}
15 changes: 0 additions & 15 deletions gart/src/main/kotlin/dev/oblac/gart/math/math.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
5 changes: 3 additions & 2 deletions gart/src/test/kotlin/dev/oblac/gart/force/FlowTest.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package dev.oblac.gart.force

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

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

Expand Down

0 comments on commit fd285e8

Please sign in to comment.