Skip to content

Instantly share code, notes, and snippets.

@feliperyan
Created July 17, 2019 14:01
Show Gist options
  • Save feliperyan/c8abce569097a7e843348fd8c9db40fb to your computer and use it in GitHub Desktop.
Save feliperyan/c8abce569097a7e843348fd8c9db40fb to your computer and use it in GitHub Desktop.
Will convert getPositionTowardsDestination() to interpolation next!
package main
import (
"fmt"
"math"
)
type GPSCoord struct {
Lat float64
Lon float64
}
// Represents a Drone
type Drone struct {
// Looks like 0.0003 is about 10 meters
CurrentPosition GPSCoord
Destinations []GPSCoord
NextDestination int
Speed float64
Name string
}
type DroneController struct {
Drones []*Drone
NorthWestBoundary GPSCoord
SouthEastBoundary GPSCoord
}
func distanceBetweenCoords(g1, g2 GPSCoord) float64 {
sideA := g1.Lat - g2.Lat
sideB := g1.Lon - g2.Lon
hyp := math.Hypot(sideA, sideB)
return hyp
}
func (d *Drone) getPositionTowardsDestination() GPSCoord {
sideA := d.CurrentPosition.Lat - d.Destinations[d.NextDestination].Lat
sideB := d.CurrentPosition.Lon - d.Destinations[d.NextDestination].Lon
hyp := math.Hypot(sideA, sideB)
if d.Speed > hyp {
return d.Destinations[d.NextDestination]
}
newPosLat := -1 * (d.Speed / (hyp / sideA))
newPosLon := -1 * (d.Speed / (hyp / sideB))
return GPSCoord{newPosLat, newPosLon}
}
func linearInterpolation(pointA, pointB GPSCoord, speed float64) GPSCoord {
}
func (d *Drone) UpdatePositionTowardsDestination() {
newPos := d.getPositionTowardsDestination()
// If the next position is our destination just use it.
if newPos.Lat == d.Destinations[d.NextDestination].Lat &&
newPos.Lon == d.Destinations[d.NextDestination].Lon {
d.NextDestination++
newLat := newPos.Lat
newLon := newPos.Lon
d.CurrentPosition = GPSCoord{newLat, newLon}
return
}
newLat := d.CurrentPosition.Lat + newPos.Lat
newLon := d.CurrentPosition.Lon + newPos.Lon
d.CurrentPosition = GPSCoord{newLat, newLon}
}
func main() {
fmt.Println("ok")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment