MCPcopy
hub / github.com/tdewolff/canvas / CoordDirections

Method CoordDirections

path.go:750–782  ·  view source on GitHub ↗

CoordDirections returns the direction of the segment start/end points. It will return the average direction at the intersection of two end points, and for an open path it will simply return the direction of the start and end points of the path.

()

Source from the content-addressed store, hash-verified

748
749// CoordDirections returns the direction of the segment start/end points. It will return the average direction at the intersection of two end points, and for an open path it will simply return the direction of the start and end points of the path.
750func (p *Path) CoordDirections() []Point {
751 if len(p.d) <= 4 {
752 return []Point{{}}
753 }
754 last := len(p.d)
755 if p.d[last-1] == CloseCmd && (Point{p.d[last-cmdLen(CloseCmd)-3], p.d[last-cmdLen(CloseCmd)-2]}).Equals(Point{p.d[last-3], p.d[last-2]}) {
756 // point-closed
757 last -= cmdLen(CloseCmd)
758 }
759
760 dirs := []Point{}
761 var closed bool
762 var dirPrev Point
763 for i := 4; i < last; {
764 cmd := p.d[i]
765 dir := p.direction(i, 0.0)
766 if i == 0 {
767 dirs = append(dirs, dir)
768 } else {
769 dirs = append(dirs, dirPrev.Add(dir).Norm(1.0))
770 }
771 dirPrev = p.direction(i, 1.0)
772 closed = cmd == CloseCmd
773 i += cmdLen(cmd)
774 }
775 if closed {
776 dirs[0] = dirs[0].Add(dirPrev).Norm(1.0)
777 dirs = append(dirs, dirs[0])
778 } else {
779 dirs = append(dirs, dirPrev)
780 }
781 return dirs
782}
783
784// curvature returns the curvature of the path at the given index into Path.d and t in [0.0,1.0]. Path must not contain subpaths, and will return the path's starting curvature when i points to a MoveToCmd, or the path's final curvature when i points to a CloseCmd of zero-length.
785func (p *Path) curvature(i int, t float64) float64 {

Callers 2

MarkersMethod · 0.95
parseDefsMethod · 0.80

Calls 6

directionMethod · 0.95
AddMethod · 0.95
cmdLenFunction · 0.85
NormMethod · 0.80
EqualsMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected