Skip to content

Commit e8c4945

Browse files
committed
Add more water level calculators
1 parent bb54b9a commit e8c4945

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.kylecorry.sol.science.oceanography.waterlevel
2+
3+
import com.kylecorry.sol.math.Vector2
4+
import com.kylecorry.sol.math.analysis.Trigonometry
5+
import com.kylecorry.sol.science.oceanography.Tide
6+
import com.kylecorry.sol.time.Time
7+
import java.time.ZonedDateTime
8+
9+
class GapWaterLevelCalculator(
10+
private val first: Tide,
11+
private val second: Tide,
12+
private val approximateFrequency: Float
13+
) :
14+
IWaterLevelCalculator {
15+
16+
private val wave by lazy {
17+
val firstVec = Vector2(getX(first.time), first.height ?: (if (first.isHigh) 1f else -1f))
18+
val secondVec =
19+
Vector2(getX(second.time), second.height ?: (if (second.isHigh) 1f else -1f))
20+
Trigonometry.connect(firstVec, secondVec, approximateFrequency)
21+
}
22+
23+
override fun calculate(time: ZonedDateTime): Float {
24+
return wave.calculate(getX(time))
25+
}
26+
27+
private fun getX(time: ZonedDateTime): Float {
28+
return Time.hoursBetween(first.time, time)
29+
}
30+
31+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.kylecorry.sol.science.oceanography.waterlevel
2+
3+
import com.kylecorry.sol.math.Range
4+
import java.time.ZonedDateTime
5+
6+
class PiecewiseWaterLevelCalculator(private val calculators: List<Pair<Range<ZonedDateTime>, IWaterLevelCalculator>>) :
7+
IWaterLevelCalculator {
8+
override fun calculate(time: ZonedDateTime): Float {
9+
return calculators.firstOrNull { it.first.contains(time) }?.second?.calculate(time) ?: 0f
10+
}
11+
}

0 commit comments

Comments
 (0)