mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
affine: Refactoring
This commit is contained in:
parent
dfcd9fc30c
commit
48c8934838
@ -14,44 +14,30 @@
|
|||||||
|
|
||||||
package affine
|
package affine
|
||||||
|
|
||||||
type affine interface {
|
|
||||||
dim() int
|
|
||||||
element(i, j int) float64
|
|
||||||
SetElement(i, j int, element float64)
|
|
||||||
}
|
|
||||||
|
|
||||||
// add is deprecated
|
// add is deprecated
|
||||||
func add(lhs, rhs, result affine) {
|
func add(lhs, rhs []float64, dim int) []float64 {
|
||||||
dim := lhs.dim()
|
result := make([]float64, len(lhs))
|
||||||
if dim != rhs.dim() {
|
|
||||||
panic("ebiten: different-sized matrices can't be multiplied")
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < dim-1; i++ {
|
for i := 0; i < dim-1; i++ {
|
||||||
for j := 0; j < dim; j++ {
|
for j := 0; j < dim; j++ {
|
||||||
v := lhs.element(i, j) + rhs.element(i, j)
|
result[i*dim+j] = lhs[i*dim+j] + rhs[i*dim+j]
|
||||||
result.SetElement(i, j, v)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func mul(lhs, rhs, result affine) {
|
func mul(lhs, rhs []float64, dim int) []float64 {
|
||||||
dim := lhs.dim()
|
result := make([]float64, len(lhs))
|
||||||
if dim != rhs.dim() {
|
|
||||||
panic("ebiten: different-sized matrices can't be multiplied")
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < dim-1; i++ {
|
for i := 0; i < dim-1; i++ {
|
||||||
for j := 0; j < dim; j++ {
|
for j := 0; j < dim; j++ {
|
||||||
element := float64(0)
|
e := 0.0
|
||||||
for k := 0; k < dim-1; k++ {
|
for k := 0; k < dim-1; k++ {
|
||||||
element += lhs.element(i, k) *
|
e += lhs[i*dim+k] * rhs[k*dim+j]
|
||||||
rhs.element(k, j)
|
|
||||||
}
|
}
|
||||||
if j == dim-1 {
|
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
|
||||||
}
|
}
|
||||||
|
@ -44,10 +44,6 @@ type ColorM struct {
|
|||||||
elements []float64
|
elements []float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ColorM) dim() int {
|
|
||||||
return ColorMDim
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *ColorM) UnsafeElements() []float64 {
|
func (c *ColorM) UnsafeElements() []float64 {
|
||||||
if c.elements == nil {
|
if c.elements == nil {
|
||||||
c.elements = colorMIdentityElements
|
c.elements = colorMIdentityElements
|
||||||
@ -55,13 +51,6 @@ func (c *ColorM) UnsafeElements() []float64 {
|
|||||||
return c.elements
|
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).
|
// SetElement sets an element at (i, j).
|
||||||
func (c *ColorM) SetElement(i, j int, element float64) {
|
func (c *ColorM) SetElement(i, j int, element float64) {
|
||||||
if c.elements == nil {
|
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.
|
// 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.
|
// This is same as muptiplying the matrix other and the matrix c in this order.
|
||||||
func (c *ColorM) Concat(other ColorM) {
|
func (c *ColorM) Concat(other ColorM) {
|
||||||
result := ColorM{}
|
if c.elements == nil {
|
||||||
mul(&other, c, &result)
|
c.elements = colorMIdentityElements
|
||||||
*c = result
|
}
|
||||||
|
if other.elements == nil {
|
||||||
|
other.elements = colorMIdentityElements
|
||||||
|
}
|
||||||
|
c.elements = mul(other.elements, c.elements, ColorMDim)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add is deprecated.
|
// Add is deprecated.
|
||||||
func (c *ColorM) Add(other ColorM) {
|
func (c *ColorM) Add(other ColorM) {
|
||||||
result := ColorM{}
|
if c.elements == nil {
|
||||||
add(&other, c, &result)
|
c.elements = colorMIdentityElements
|
||||||
*c = result
|
}
|
||||||
|
if other.elements == nil {
|
||||||
|
other.elements = colorMIdentityElements
|
||||||
|
}
|
||||||
|
c.elements = add(other.elements, c.elements, ColorMDim)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scale scales the matrix by (r, g, b, a).
|
// Scale scales the matrix by (r, g, b, a).
|
||||||
|
@ -37,10 +37,6 @@ type GeoM struct {
|
|||||||
elements []float64
|
elements []float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GeoM) dim() int {
|
|
||||||
return GeoMDim
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GeoM) UnsafeElements() []float64 {
|
func (g *GeoM) UnsafeElements() []float64 {
|
||||||
if g.elements == nil {
|
if g.elements == nil {
|
||||||
g.elements = geoMIdentityElements
|
g.elements = geoMIdentityElements
|
||||||
@ -48,13 +44,6 @@ func (g *GeoM) UnsafeElements() []float64 {
|
|||||||
return g.elements
|
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).
|
// SetElement sets an element at (i, j).
|
||||||
func (g *GeoM) SetElement(i, j int, element float64) {
|
func (g *GeoM) SetElement(i, j int, element float64) {
|
||||||
if g.elements == nil {
|
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.
|
// 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.
|
// This is same as muptiplying the matrix other and the matrix g in this order.
|
||||||
func (g *GeoM) Concat(other GeoM) {
|
func (g *GeoM) Concat(other GeoM) {
|
||||||
result := GeoM{}
|
if g.elements == nil {
|
||||||
mul(&other, g, &result)
|
g.elements = geoMIdentityElements
|
||||||
*g = result
|
}
|
||||||
|
if other.elements == nil {
|
||||||
|
other.elements = geoMIdentityElements
|
||||||
|
}
|
||||||
|
g.elements = mul(other.elements, g.elements, GeoMDim)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add is deprecated.
|
// Add is deprecated.
|
||||||
func (g *GeoM) Add(other GeoM) {
|
func (g *GeoM) Add(other GeoM) {
|
||||||
result := GeoM{}
|
if g.elements == nil {
|
||||||
add(&other, g, &result)
|
g.elements = geoMIdentityElements
|
||||||
*g = result
|
}
|
||||||
|
if other.elements == nil {
|
||||||
|
other.elements = geoMIdentityElements
|
||||||
|
}
|
||||||
|
g.elements = add(other.elements, g.elements, GeoMDim)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scale scales the matrix by (x, y).
|
// Scale scales the matrix by (x, y).
|
||||||
|
Loading…
Reference in New Issue
Block a user