Refactoring: define identity matrix variable

This commit is contained in:
Hajime Hoshi 2014-12-27 17:11:05 +09:00
parent 7849a68cd0
commit 969554b10b
2 changed files with 24 additions and 28 deletions

View File

@ -21,6 +21,16 @@ import (
// ColorMDim is a dimension of a ColorM.
const ColorMDim = 5
var colorMI = ColorM{
initialized: true,
es: [ColorMDim - 1][ColorMDim]float64{
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
},
}
// A ColorM represents a matrix to transform coloring when rendering an image.
//
// A ColorM is applied to the source alpha color
@ -34,15 +44,6 @@ type ColorM struct {
es [ColorMDim - 1][ColorMDim]float64
}
func colorMatrixEsI() [ColorMDim - 1][ColorMDim]float64 {
return [ColorMDim - 1][ColorMDim]float64{
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
}
}
func (c ColorM) dim() int {
return ColorMDim
}
@ -62,8 +63,7 @@ func (c ColorM) Element(i, j int) float64 {
// This returns c.
func (c *ColorM) Concat(other ColorM) ColorM {
if !c.initialized {
c.es = colorMatrixEsI()
c.initialized = true
*c = colorMI
}
result := ColorM{}
mul(&other, c, &result)
@ -75,8 +75,7 @@ func (c *ColorM) Concat(other ColorM) ColorM {
// This returns c.
func (c *ColorM) Add(other ColorM) ColorM {
if !c.initialized {
c.es = colorMatrixEsI()
c.initialized = true
*c = colorMI
}
result := ColorM{}
add(&other, c, &result)
@ -87,8 +86,7 @@ func (c *ColorM) Add(other ColorM) ColorM {
// SetElement sets an element at (i, j).
func (c *ColorM) SetElement(i, j int, element float64) {
if !c.initialized {
c.es = colorMatrixEsI()
c.initialized = true
*c = colorMI
}
c.es[i][j] = element
}

View File

@ -21,6 +21,14 @@ import (
// GeoMDim is a dimension of a GeoM.
const GeoMDim = 3
var geoMI = GeoM{
initialized: true,
es: [2][3]float64{
{1, 0, 0},
{0, 1, 0},
},
}
// A GeoM represents a matrix to transform geometry when rendering an image.
//
// The initial value is identity.
@ -29,13 +37,6 @@ type GeoM struct {
es [GeoMDim - 1][GeoMDim]float64
}
func geometryMatrixEsI() [GeoMDim - 1][GeoMDim]float64 {
return [GeoMDim - 1][GeoMDim]float64{
{1, 0, 0},
{0, 1, 0},
}
}
func (g GeoM) dim() int {
return GeoMDim
}
@ -55,8 +56,7 @@ func (g GeoM) Element(i, j int) float64 {
// This returns g.
func (g *GeoM) Concat(other GeoM) GeoM {
if !g.initialized {
g.es = geometryMatrixEsI()
g.initialized = true
*g = geoMI
}
result := GeoM{}
mul(&other, g, &result)
@ -68,8 +68,7 @@ func (g *GeoM) Concat(other GeoM) GeoM {
// This returns g.
func (g *GeoM) Add(other GeoM) GeoM {
if !g.initialized {
g.es = geometryMatrixEsI()
g.initialized = true
*g = geoMI
}
result := GeoM{}
add(&other, g, &result)
@ -80,8 +79,7 @@ func (g *GeoM) Add(other GeoM) GeoM {
// SetElement sets an element at (i, j).
func (g *GeoM) SetElement(i, j int, element float64) {
if !g.initialized {
g.es = geometryMatrixEsI()
g.initialized = true
*g = geoMI
}
g.es[i][j] = element
}