github.com/pavlo67/common@v0.5.3/common/mathlib/plane/direction.go (about) 1 package plane 2 3 func Middle(p0, p1 Point2) Point2 { 4 return Point2{(p0.X + p1.X) / 2, (p0.Y + p1.Y) / 2} 5 } 6 7 func PointOnRay(p0 Point2, r float64) Point2 { 8 r0 := p0.Radius() 9 if r0 == 0 { 10 return Point2{} 11 } 12 13 return Point2{p0.X * r / r0, p0.Y * r / r0} 14 } 15 16 func Direction(pCh PolyChain, deviationMaxIn float64) (*Segment, int) { 17 if len(pCh) < 2 { 18 return nil, 0 19 } 20 21 pEnd := pCh[len(pCh)-1] 22 directionLine := Segment{pCh[len(pCh)-2], pEnd} 23 n := 1 24 25 for i := len(pCh) - 3; i >= 0; i-- { 26 directionLineNext := Segment{Segment{pCh[i], pCh[i+1]}.Middle(), pEnd} 27 for j := i + 1; j < len(pCh)-1; j++ { 28 if pCh[j].DistanceToLine(directionLineNext) > deviationMaxIn { 29 return &directionLine, n 30 } 31 } 32 directionLine = directionLineNext 33 34 directionLineNext = Segment{pCh[i], pEnd} 35 for j := i + 1; j < len(pCh)-1; j++ { 36 if pCh[j].DistanceToLine(directionLineNext) > deviationMaxIn { 37 return &directionLine, n 38 } 39 } 40 directionLine = directionLineNext 41 n++ 42 43 } 44 45 if directionLine[0] == directionLine[1] { 46 return nil, 0 47 } 48 49 return &directionLine, n 50 }