Skip to content

Commit

Permalink
interpolate result if next point is also known
Browse files Browse the repository at this point in the history
  • Loading branch information
angryziber committed Jan 3, 2017
1 parent 8920ed5 commit bf2d68c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/geotag/timeline/Track.kt
@@ -1,5 +1,6 @@
package geotag.timeline

import java.time.Duration.between
import java.time.Instant

data class Track(
Expand All @@ -8,6 +9,22 @@ data class Track(
val points: List<TrackPoint>) {

fun pointAt(time: Instant): TrackPoint? {
return points.findLast { it.time <= time }
val i = points.listIterator(points.size)
while (i.hasPrevious()) {
val point = i.previous()
if (time >= point.time) {
i.next()
if (i.hasNext()) return interpolate(point, i.next(), time)
else return point
}
}
return null
}

private fun interpolate(p1: TrackPoint, p2: TrackPoint, time: Instant): TrackPoint {
val c = between(p1.time, time).toMillis().toFloat() / between(p1.time, p2.time).toMillis()
return TrackPoint(interpolate(p1.lat, p2.lat, c), interpolate(p1.lon, p2.lon, c), time)
}

private fun interpolate(v1: LatLon, v2: LatLon, c: Float) = v1.value + (v2.value - v1.value) * c
}

0 comments on commit bf2d68c

Please sign in to comment.