Added several extension functions

This commit is contained in:
Arty Bishop committed 2021-03-21 19:48:20 +00:00
1 parent e0f900366a
commit 332b6f666c
1 file changed
+29
@@ -38,6 +38,10 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import java.util.concurrent.TimeUnit
import kotlin.math.max
import kotlin.math.min
import kotlin.math.pow
import kotlin.math.sqrt
fun Long.formatForTimer(): String {
val format = "%02d:%02d:%02d"
@@ -47,12 +51,37 @@ fun Long.formatForTimer(): String {
return String.format(format, hours, minutes, seconds)
}
fun Long.getDopplerFreq(rangeRate: Double, isDownlink: Boolean): Long {
val speedOfLight = 2.99792458E8
return if (isDownlink) {
(this.toDouble() * (speedOfLight - rangeRate * 1000.0) / speedOfLight).toLong()
} else {
(this.toDouble() * (speedOfLight + rangeRate * 1000.0) / speedOfLight).toLong()
}
}
fun Double.round(decimals: Int): Double {
var multiplier = 1.0
repeat(decimals) { multiplier *= 10 }
return kotlin.math.round(this * multiplier) / multiplier
}
fun Double.toOsmLat(): Double {
return min(max(this, -85.0), 85.0)
}
fun Double.toOsmLon(): Double {
val newLon = if (this < -180.0) this + 360.0 else if (this > 180.0) this - 360.0 else this
return min(max(newLon, -180.0), 180.0)
}
fun Double.getOrbitalVelocity(): Double {
val earthG = 6.674 * 10.0.pow(-11)
val earthM = 5.98 * 10.0.pow(24)
val radius = 6.37 * 10.0.pow(6) + this * 10.0.pow(3)
return sqrt(earthG * earthM / radius) / 1000
}
fun Satellite.getPredictor(stationPosition: GroundStationPosition): PassPredictor {
return PassPredictor(this, stationPosition)
}