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

Method Length

path.go:1233–1264  ·  view source on GitHub ↗

Length returns the length of the path in millimeters. The length is approximated for cubic Béziers.

()

Source from the content-addressed store, hash-verified

1231
1232// Length returns the length of the path in millimeters. The length is approximated for cubic Béziers.
1233func (p *Path) Length() float64 {
1234 d := 0.0
1235 var start, end Point
1236 for i := 0; i < len(p.d); {
1237 cmd := p.d[i]
1238 switch cmd {
1239 case MoveToCmd:
1240 end = Point{p.d[i+1], p.d[i+2]}
1241 case LineToCmd, CloseCmd:
1242 end = Point{p.d[i+1], p.d[i+2]}
1243 d += end.Sub(start).Length()
1244 case QuadToCmd:
1245 cp := Point{p.d[i+1], p.d[i+2]}
1246 end = Point{p.d[i+3], p.d[i+4]}
1247 d += quadraticBezierLength(start, cp, end)
1248 case CubeToCmd:
1249 cp1 := Point{p.d[i+1], p.d[i+2]}
1250 cp2 := Point{p.d[i+3], p.d[i+4]}
1251 end = Point{p.d[i+5], p.d[i+6]}
1252 d += cubicBezierLength(start, cp1, cp2, end)
1253 case ArcToCmd:
1254 rx, ry, phi := p.d[i+1], p.d[i+2], p.d[i+3]
1255 large, sweep := toArcFlags(p.d[i+4])
1256 end = Point{p.d[i+5], p.d[i+6]}
1257 _, _, theta1, theta2 := ellipseToCenter(start.X, start.Y, rx, ry, phi, large, sweep, end.X, end.Y)
1258 d += ellipseLength(rx, ry, theta1, theta2)
1259 }
1260 i += cmdLen(cmd)
1261 start = end
1262 }
1263 return d
1264}
1265
1266// Transform transforms the path by the given transformation matrix. It modifies the path in-place.
1267func (p *Path) Transform(m Matrix) *Path {

Callers 15

checkDashMethod · 0.95
TestPathStrokeEllipseFunction · 0.45
LineToMethod · 0.45
SplitAtMethod · 0.45
DashMethod · 0.45
ellipseLengthFunction · 0.45
quadraticBezierLengthFunction · 0.45
quadraticBezierDistanceFunction · 0.45
cubicBezierLengthFunction · 0.45
flattenSmoothCubicBezierFunction · 0.45
TestCubicBezierStrokeFunction · 0.45
TestPathLengthFunction · 0.45

Calls 7

SubMethod · 0.95
quadraticBezierLengthFunction · 0.85
cubicBezierLengthFunction · 0.85
toArcFlagsFunction · 0.85
ellipseToCenterFunction · 0.85
ellipseLengthFunction · 0.85
cmdLenFunction · 0.85

Tested by 4

TestPathStrokeEllipseFunction · 0.36
TestCubicBezierStrokeFunction · 0.36
TestPathLengthFunction · 0.36