I'm trying to create a function that will let me give 2 arguments, a new location and a speed to travel at (in meters / second)
It looks like this:
func (l *Location) Move(newLoc *Location, speed float64) {
R := 6371.0 // Kilometers
lat1 := l.Latitude * math.Pi / 180
lat2 := l.Longitude * math.Pi / 180
diffLat := (newLoc.Latitude - l.Latitude) * math.Pi / 180
diffLon := (newLoc.Longitude - l.Longitude) * math.Pi / 180
a := math.Sin(diffLat/2)*math.Sin(diffLat/2) +
math.Cos(lat1)*math.Cos(lat2)*math.Sin(diffLon/2)*math.Sin(diffLon/2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
distanceToMove := R * c // Distance to travel in a straight line, in Kilometers
}
The only thing I'm having trouble with is thinking of the formula to make the latitude, start at its current position, and end up at its new position over a set amount of time.
So say the person changed the latitude from 56.65
to 58.12
and I told it to travel at 1.3m/s
how can I accomplish this. Thanks.
If I understand your question, your goal is to compute all the intermediate points between two location, starting from one location and going to the second one using a specified speed.
If I'm correct, the following is how I would get a first solution. If anyone can improve this, I'd appreciate.
On the proj4 documentation, you can find a lot of information on how to compute distance between two points.
Starting from points A to reach B with a given speed (m/s), just means to compute each seconds a point A' that is at a distance m from A on the line AB.
In a more algorithmic way (based on Vincenty's formula):
func (l *Location) Move(newLoc *Location, speed float64) Location {
azimuthA, azimuthB, d := inverseVincenty(l, newLoc)
// Use the direct vincenty's formula.
// Here transform speed to get the correct value
// without transformation, since speed is in m/s,
// the resulting point will be at speed(m) distance from l.
res := directVincenty(l, speed, azimuthA)
// Now res shall contain your new point.
return res
}
func main() {
// init A and B and speed values.
C := A // to conserve A position.
t := time.Tick(1* time.Second)
for stop := false; !stop; {
select {
case <- t:
C = C.Move(B, speed)
case // here a break condition:
stop = true
}
}
}
I think thats a start, any comment is appreciate on this answer.