affine: Refactoring

This commit is contained in:
Hajime Hoshi 2017-01-20 00:46:25 +09:00
parent dfcd9fc30c
commit 48c8934838
3 changed files with 39 additions and 59 deletions

View File

@ -14,44 +14,30 @@
package affine
type affine interface {
dim() int
element(i, j int) float64
SetElement(i, j int, element float64)
}
// add is deprecated
func add(lhs, rhs, result affine) {
dim := lhs.dim()
if dim != rhs.dim() {
panic("ebiten: different-sized matrices can't be multiplied")
}
func add(lhs, rhs []float64, dim int) []float64 {
result := make([]float64, len(lhs))
for i := 0; i < dim-1; i++ {
for j := 0; j < dim; j++ {
v := lhs.element(i, j) + rhs.element(i, j)
result.SetElement(i, j, v)
result[i*dim+j] = lhs[i*dim+j] + rhs[i*dim+j]
}
}
return result
}
func mul(lhs, rhs, result affine) {
dim := lhs.dim()
if dim != rhs.dim() {
panic("ebiten: different-sized matrices can't be multiplied")
}
func mul(lhs, rhs []float64, dim int) []float64 {
result := make([]float64, len(lhs))
for i := 0; i < dim-1; i++ {
for j := 0; j < dim; j++ {
element := float64(0)
e := 0.0
for k := 0; k < dim-1; k++ {
element += lhs.element(i, k) *
rhs.element(k, j)
e += lhs[i*dim+k] * rhs[k*dim+j]
}
if j == dim-1 {
element += lhs.element(i, j)
e += lhs[i*dim+j]
}
result.SetElement(i, j, element)
result[i*dim+j] = e
}
}
return result
}

View File

@ -44,10 +44,6 @@ type ColorM struct {
elements []float64
}
func (c *ColorM) dim() int {
return ColorMDim
}
func (c *ColorM) UnsafeElements() []float64 {
if c.elements == nil {
c.elements = colorMIdentityElements
@ -55,13 +51,6 @@ func (c *ColorM) UnsafeElements() []float64 {
return c.elements
}
func (c *ColorM) element(i, j int) float64 {
if c.elements == nil {
c.elements = colorMIdentityElements
}
return c.elements[i*ColorMDim+j]
}
// SetElement sets an element at (i, j).
func (c *ColorM) SetElement(i, j int, element float64) {
if c.elements == nil {
@ -94,16 +83,24 @@ func (c *ColorM) Equals(other *ColorM) bool {
// Concat multiplies a color matrix with the other color matrix.
// This is same as muptiplying the matrix other and the matrix c in this order.
func (c *ColorM) Concat(other ColorM) {
result := ColorM{}
mul(&other, c, &result)
*c = result
if c.elements == nil {
c.elements = colorMIdentityElements
}
if other.elements == nil {
other.elements = colorMIdentityElements
}
c.elements = mul(other.elements, c.elements, ColorMDim)
}
// Add is deprecated.
func (c *ColorM) Add(other ColorM) {
result := ColorM{}
add(&other, c, &result)
*c = result
if c.elements == nil {
c.elements = colorMIdentityElements
}
if other.elements == nil {
other.elements = colorMIdentityElements
}
c.elements = add(other.elements, c.elements, ColorMDim)
}
// Scale scales the matrix by (r, g, b, a).

View File

@ -37,10 +37,6 @@ type GeoM struct {
elements []float64
}
func (g *GeoM) dim() int {
return GeoMDim
}
func (g *GeoM) UnsafeElements() []float64 {
if g.elements == nil {
g.elements = geoMIdentityElements
@ -48,13 +44,6 @@ func (g *GeoM) UnsafeElements() []float64 {
return g.elements
}
func (g *GeoM) element(i, j int) float64 {
if g.elements == nil {
g.elements = geoMIdentityElements
}
return g.elements[i*GeoMDim+j]
}
// SetElement sets an element at (i, j).
func (g *GeoM) SetElement(i, j int, element float64) {
if g.elements == nil {
@ -69,16 +58,24 @@ func (g *GeoM) SetElement(i, j int, element float64) {
// Concat multiplies a geometry matrix with the other geometry matrix.
// This is same as muptiplying the matrix other and the matrix g in this order.
func (g *GeoM) Concat(other GeoM) {
result := GeoM{}
mul(&other, g, &result)
*g = result
if g.elements == nil {
g.elements = geoMIdentityElements
}
if other.elements == nil {
other.elements = geoMIdentityElements
}
g.elements = mul(other.elements, g.elements, GeoMDim)
}
// Add is deprecated.
func (g *GeoM) Add(other GeoM) {
result := GeoM{}
add(&other, g, &result)
*g = result
if g.elements == nil {
g.elements = geoMIdentityElements
}
if other.elements == nil {
other.elements = geoMIdentityElements
}
g.elements = add(other.elements, g.elements, GeoMDim)
}
// Scale scales the matrix by (x, y).