美文网首页
Interpolate

Interpolate

作者: hehehehe | 来源:发表于2022-03-08 14:47 被阅读0次
    package gojts
    
    
    const DISTMIN = 0.000001
    
    type Segment struct {
        Start, End geom.XY
    }
    
    func (s Segment) Length() float64 {
        return s.End.Sub(s.Start).Length()
    }
    
    /**
    x := segment.Start.X + (segment.End.X-segment.Start.X)*0.4
    y := segment.Start.Y + (segment.End.Y-segment.Start.Y)*0.4
    fmt.Println(x, y)
    
    fmt.Println(segment.Start.Add(segment.End.Sub(segment.Start).Scale(rate)))
    **/
    func distValid(dist float64) {
        if dist < DISTMIN {
            panic("dist can not < 0")
        }
    }
    func (s Segment) Interpolate(dist float64) geom.XY {
        distValid(dist)
        length := s.Length()
        if dist < length {
            rate := dist / length
            return s.Start.Add(s.End.Sub(s.Start).Scale(rate))
        } else {
            return s.End
        }
    }
    
    func Interpolate(linestring geom.LineString, dist float64) geom.XY {
        distValid(dist)
        seq := linestring.Coordinates()
        for i := 0; i < seq.Length(); i++ {
            segment := Segment{Start: seq.GetXY(i), End: seq.GetXY(i + 1)}
            length := segment.Length()
            if dist <= length {
                return segment.Interpolate(dist)
            } else {
                dist -= length
            }
        }
        return geom.XY{X: -9999, Y: -9999}
    }
    
    
    package gojts
    
    type Segment2 struct {
        Start, End r2.Vec
    }
    
    func (s Segment2) Length() float64 {
        return r2.Norm(r2.Sub(s.End, s.Start))
    }
    
    func (s Segment2) Interpolate(dist float64) r2.Vec {
        distValid(dist)
        length := s.Length()
        if dist < length {
            rate := dist / length
            return r2.Add(s.Start, r2.Scale(rate, r2.Sub(s.End, s.Start)))
        } else {
            return s.End
        }
    }
    
    func Interpolate2(linestring geom.LineString, dist float64) r2.Vec {
        distValid(dist)
        coords := linestring.Coords()
        for i := 0; i < len(coords); i++ {
            start := coords[i]
            end := coords[i+1]
            segment := Segment2{Start: r2.Vec{X: start[0], Y: start[1]}, End: r2.Vec{X: end[0], Y: end[1]}}
            length := segment.Length()
            if dist <= length {
                return segment.Interpolate(dist)
            } else {
                dist -= length
            }
        }
        return r2.Vec{X: -9999, Y: -9999}
    }
    
    

    相关文章

      网友评论

          本文标题:Interpolate

          本文链接:https://www.haomeiwen.com/subject/zlyarrtx.html