affine: Make Scale/Translate faster

This commit is contained in:
Hajime Hoshi 2017-01-20 00:59:33 +09:00
parent 48c8934838
commit 6e84919ed5
2 changed files with 24 additions and 4 deletions

View File

@ -106,7 +106,13 @@ func (c *ColorM) Add(other ColorM) {
// Scale scales the matrix by (r, g, b, a). // Scale scales the matrix by (r, g, b, a).
func (c *ColorM) Scale(r, g, b, a float64) { func (c *ColorM) Scale(r, g, b, a float64) {
if c.elements == nil { if c.elements == nil {
c.elements = colorMIdentityElements c.elements = []float64{
r, 0, 0, 0, 0,
0, g, 0, 0, 0,
0, 0, b, 0, 0,
0, 0, 0, a, 0,
}
return
} }
es := make([]float64, len(c.elements)) es := make([]float64, len(c.elements))
copy(es, c.elements) copy(es, c.elements)
@ -122,7 +128,13 @@ func (c *ColorM) Scale(r, g, b, a float64) {
// Translate translates the matrix by (r, g, b, a). // Translate translates the matrix by (r, g, b, a).
func (c *ColorM) Translate(r, g, b, a float64) { func (c *ColorM) Translate(r, g, b, a float64) {
if c.elements == nil { if c.elements == nil {
c.elements = colorMIdentityElements c.elements = []float64{
1, 0, 0, 0, r,
0, 1, 0, 0, g,
0, 0, 1, 0, b,
0, 0, 0, 1, a,
}
return
} }
es := make([]float64, len(c.elements)) es := make([]float64, len(c.elements))
copy(es, c.elements) copy(es, c.elements)

View File

@ -81,7 +81,11 @@ func (g *GeoM) Add(other GeoM) {
// Scale scales the matrix by (x, y). // Scale scales the matrix by (x, y).
func (g *GeoM) Scale(x, y float64) { func (g *GeoM) Scale(x, y float64) {
if g.elements == nil { if g.elements == nil {
g.elements = geoMIdentityElements g.elements = []float64{
x, 0, 0,
0, y, 0,
}
return
} }
es := make([]float64, len(g.elements)) es := make([]float64, len(g.elements))
copy(es, g.elements) copy(es, g.elements)
@ -95,7 +99,11 @@ func (g *GeoM) Scale(x, y float64) {
// Translate translates the matrix by (x, y). // Translate translates the matrix by (x, y).
func (g *GeoM) Translate(tx, ty float64) { func (g *GeoM) Translate(tx, ty float64) {
if g.elements == nil { if g.elements == nil {
g.elements = geoMIdentityElements g.elements = []float64{
1, 0, tx,
0, 1, ty,
}
return
} }
es := make([]float64, len(g.elements)) es := make([]float64, len(g.elements))
copy(es, g.elements) copy(es, g.elements)