MCPcopy Create free account
hub / github.com/tdewolff/canvas / TransformFunc

Method TransformFunc

path.go:1359–1433  ·  view source on GitHub ↗

TransformFunc transforms the path by the given function: (x,y)=>(x,y). It modifies the path in-place.

(f func(float64, float64) (float64, float64))

Source from the content-addressed store, hash-verified

1357
1358// TransformFunc transforms the path by the given function: (x,y)=>(x,y). It modifies the path in-place.
1359func (p *Path) TransformFunc(f func(float64, float64) (float64, float64)) *Path {
1360 for i := 0; i < len(p.d); {
1361 cmd := p.d[i]
1362 switch cmd {
1363 case MoveToCmd, LineToCmd, CloseCmd:
1364 x, y := f(p.d[i+1], p.d[i+2])
1365 p.d[i+1] = x
1366 p.d[i+2] = y
1367 case QuadToCmd:
1368 cpx, cpy := f(p.d[i+1], p.d[i+2])
1369 x, y := f(p.d[i+3], p.d[i+4])
1370 p.d[i+1] = cpx
1371 p.d[i+2] = cpy
1372 p.d[i+3] = x
1373 p.d[i+4] = y
1374 case CubeToCmd:
1375 cp1x, cp1y := f(p.d[i+1], p.d[i+2])
1376 cp2x, cp2y := f(p.d[i+3], p.d[i+4])
1377 x, y := f(p.d[i+5], p.d[i+6])
1378 p.d[i+1] = cp1x
1379 p.d[i+2] = cp1y
1380 p.d[i+3] = cp2x
1381 p.d[i+4] = cp2y
1382 p.d[i+5] = x
1383 p.d[i+6] = y
1384 case ArcToCmd:
1385 panic("not implemented") // TODO: implement transform func for arcs
1386 //rx := p.d[i+1]
1387 //ry := p.d[i+2]
1388 //phi := p.d[i+3]
1389 //large, sweep := toArcFlags(p.d[i+4])
1390 //end := Point{p.d[i+5], p.d[i+6]}
1391
1392 //// For ellipses written as the conic section equation in matrix form, we have:
1393 //// [x, y] E [x; y] = 0, with E = [1/rx^2, 0; 0, 1/ry^2]
1394 //// For our transformed ellipse we have [x', y'] = T [x, y], with T the affine
1395 //// transformation matrix so that
1396 //// (T^-1 [x'; y'])^T E (T^-1 [x'; y'] = 0 => [x', y'] T^(-T) E T^(-1) [x'; y'] = 0
1397 //// We define Q = T^(-1,T) E T^(-1) the new ellipse equation which is typically rotated
1398 //// from the x-axis. That's why we find the eigenvalues and eigenvectors (the new
1399 //// direction and length of the major and minor axes).
1400 //T := m.Rotate(phi * 180.0 / math.Pi)
1401 //invT := T.Inv()
1402 //Q := Identity.Scale(1.0/rx/rx, 1.0/ry/ry)
1403 //Q = invT.T().Mul(Q).Mul(invT)
1404
1405 //lambda1, lambda2, v1, v2 := Q.Eigen()
1406 //rx = 1 / math.Sqrt(lambda1)
1407 //ry = 1 / math.Sqrt(lambda2)
1408 //phi = v1.Angle()
1409 //if rx < ry {
1410 // rx, ry = ry, rx
1411 // phi = v2.Angle()
1412 //}
1413 //phi = angleNorm(phi)
1414 //if math.Pi <= phi { // phi is canonical within 0 <= phi < 180
1415 // phi -= math.Pi
1416 //}

Callers 1

mainFunction · 0.80

Calls 1

cmdLenFunction · 0.85

Tested by

no test coverage detected