This commit is contained in:
2025-11-15 10:14:16 +01:00
parent c189e3f59a
commit 1bebded484
3 changed files with 75 additions and 22 deletions

View File

@@ -2,6 +2,7 @@ package types
import (
"iter"
"math"
"time"
)
@@ -51,6 +52,19 @@ func (s *Stop) GetTripsAfter(when time.Time) iter.Seq[*TripWithDepartureTime] {
}
}
func (s *Stop) HaversineDistance(other *Stop) float64 {
return haversine(s.StopLat, s.StopLon, other.StopLat, other.StopLon)
}
func haversine(lat1, lon1, lat2, lon2 float64) float64 {
const R = 6371 // Earth radius in km
dLat := (lat2 - lat1) * math.Pi / 180
dLon := (lon2 - lon1) * math.Pi / 180
a := math.Sin(dLat/2)*math.Sin(dLat/2) + math.Cos(lat1*math.Pi/180)*math.Cos(lat2*math.Pi/180)*math.Sin(dLon/2)*math.Sin(dLon/2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
return R * c
}
func (s *Stop) GetUpcomingStops(start *StopTime) iter.Seq[*StopTime] {
return func(yield func(*StopTime) bool) {
found := false