update get point at distance
This commit is contained in:
parent
6d2613effb
commit
ae6618a195
2 changed files with 16 additions and 7 deletions
|
|
@ -287,3 +287,11 @@ func (p *Point) Transpose() {
|
|||
}
|
||||
p.X, p.Y = p.Y, p.X
|
||||
}
|
||||
|
||||
// point t% of the way between a and b
|
||||
func (a *Point) Interpolate(b *Point, t float64) *Point {
|
||||
return NewPoint(
|
||||
a.X*(1.0-t)+b.X*t,
|
||||
a.Y*(1.0-t)+b.Y*t,
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,22 +20,23 @@ func (route Route) Length() float64 {
|
|||
// return the point at _distance_ along the route, and the index of the segment it's on
|
||||
func (route Route) GetPointAtDistance(distance float64) (*Point, int) {
|
||||
remaining := distance
|
||||
var curr, next *Point
|
||||
var length float64
|
||||
for i := 0; i < len(route)-1; i++ {
|
||||
curr, next := route[i], route[i+1]
|
||||
length := EuclideanDistance(curr.X, curr.Y, next.X, next.Y)
|
||||
curr, next = route[i], route[i+1]
|
||||
length = EuclideanDistance(curr.X, curr.Y, next.X, next.Y)
|
||||
|
||||
if remaining <= length {
|
||||
t := remaining / length
|
||||
// point t% of the way between curr and next
|
||||
return NewPoint(
|
||||
curr.X*(1.0-t)+next.X*t,
|
||||
curr.Y*(1.0-t)+next.Y*t,
|
||||
), i
|
||||
return curr.Interpolate(next, t), i
|
||||
}
|
||||
remaining -= length
|
||||
}
|
||||
|
||||
return nil, -1
|
||||
// distance > length, so continue with last segment
|
||||
// Note: distance < 0 handled above with first segment
|
||||
return curr.Interpolate(next, 1+remaining/length), len(route) - 2
|
||||
}
|
||||
|
||||
func (route Route) GetBoundingBox() (tl, br *Point) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue