Skip to content

Commit

Permalink
Differentiate 'to' and 'from' wind direction; arrow rotated by 'to'
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtakac committed May 26, 2024
1 parent adec818 commit bd72da7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fun WindSummary(state: WindSummary, modifier: Modifier = Modifier) {
contentDescription = null,
modifier = Modifier
.fillMaxSize()
.rotate(state.windNow.direction.degrees.toFloat())
.rotate(state.windNow.to.degrees.toFloat())
)
}
)
Expand Down Expand Up @@ -107,7 +107,7 @@ private fun WindSummaryPreview() {
state = WindSummary(
windNow = Wind(
speed = WindSpeed.fromMetersPerSecond(9.0),
direction = WindDirection(76.0)
from = WindDirection(76.0)
),
gustNow = WindSpeed.fromMetersPerSecond(20.0)
),
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/davidtakac/bura/wind/Wind.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import java.util.Objects

class Wind(
val speed: WindSpeed,
val direction: WindDirection
val from: WindDirection
) {
val to: WindDirection = WindDirection(degrees = from.degrees + 180)

override fun equals(other: Any?): Boolean =
other is Wind && other.speed == speed && other.direction == direction
other is Wind && other.speed == speed && other.from == from

override fun hashCode(): Int = Objects.hash(speed, direction)
override fun hashCode(): Int = Objects.hash(speed, from)

override fun toString(): String = "$speed, $direction"
override fun toString(): String = "$speed from $from"
}
11 changes: 4 additions & 7 deletions app/src/main/java/com/davidtakac/bura/wind/WindDirection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
package com.davidtakac.bura.wind

import java.util.Objects
import kotlin.math.ceil

class WindDirection(val degrees: Double) {
val compass: Compass

init {
val index = (degrees / 22.5 + 0.5).toInt() % 16
compass = Compass.values()[index]
}
class WindDirection(degrees: Double) {
val degrees: Double = degrees + ceil(-degrees / 360) * 360
val compass: Compass = Compass.entries[(degrees / 22.5 + 0.5).toInt() % 16]

enum class Compass {
N, NNE, NE, ENE,
Expand Down
8 changes: 8 additions & 0 deletions app/src/test/java/com/davidtakac/bura/WindDirectionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ class WindDirectionTest {
assertEquals(WindDirection.Compass.NNW, WindDirection(338.0).compass)
}

@Test
fun `normalizes degrees to 0-359`() {
assertEquals(WindDirection(30.0), WindDirection(350.0 + 40.0))
assertEquals(WindDirection(0.0), WindDirection(360.0))
assertEquals(WindDirection(1.0), WindDirection(361.0))
assertEquals(WindDirection(359.0), WindDirection(359.0))
}

@Test
fun equals() {
assertEquals(WindDirection(0.0), WindDirection(0.0))
Expand Down

0 comments on commit bd72da7

Please sign in to comment.