graphics: Use float64 in GeoM

The reason why float32 was adopted was forgotten. Using float64
should not be problematic.

Fixes #883
This commit is contained in:
Hajime Hoshi 2020-03-16 00:38:31 +09:00
parent 6bc62b8f17
commit 4b63be0156
2 changed files with 34 additions and 41 deletions

73
geom.go
View File

@ -26,12 +26,12 @@ const GeoMDim = 3
// //
// The initial value is identity. // The initial value is identity.
type GeoM struct { type GeoM struct {
a_1 float32 // The actual 'a' value minus 1 a_1 float64 // The actual 'a' value minus 1
b float32 b float64
c float32 c float64
d_1 float32 // The actual 'd' value minus 1 d_1 float64 // The actual 'd' value minus 1
tx float32 tx float64
ty float32 ty float64
} }
// String returns a string representation of GeoM. // String returns a string representation of GeoM.
@ -53,33 +53,28 @@ func (g *GeoM) Reset() {
// In other words, Apply calculates GeoM * (x, y, 1)^T. // In other words, Apply calculates GeoM * (x, y, 1)^T.
// The return value is x and y values of the result vector. // The return value is x and y values of the result vector.
func (g *GeoM) Apply(x, y float64) (float64, float64) { func (g *GeoM) Apply(x, y float64) (float64, float64) {
x2, y2 := g.apply32(float32(x), float32(y))
return float64(x2), float64(y2)
}
func (g *GeoM) apply32(x, y float32) (x2, y2 float32) {
return (g.a_1+1)*x + g.b*y + g.tx, g.c*x + (g.d_1+1)*y + g.ty return (g.a_1+1)*x + g.b*y + g.tx, g.c*x + (g.d_1+1)*y + g.ty
} }
func (g *GeoM) elements() (a, b, c, d, tx, ty float32) { func (g *GeoM) elements32() (a, b, c, d, tx, ty float32) {
return g.a_1 + 1, g.b, g.c, g.d_1 + 1, g.tx, g.ty return float32(g.a_1) + 1, float32(g.b), float32(g.c), float32(g.d_1) + 1, float32(g.tx), float32(g.ty)
} }
// Element returns a value of a matrix at (i, j). // Element returns a value of a matrix at (i, j).
func (g *GeoM) Element(i, j int) float64 { func (g *GeoM) Element(i, j int) float64 {
switch { switch {
case i == 0 && j == 0: case i == 0 && j == 0:
return float64(g.a_1) + 1 return g.a_1 + 1
case i == 0 && j == 1: case i == 0 && j == 1:
return float64(g.b) return g.b
case i == 0 && j == 2: case i == 0 && j == 2:
return float64(g.tx) return g.tx
case i == 1 && j == 0: case i == 1 && j == 0:
return float64(g.c) return g.c
case i == 1 && j == 1: case i == 1 && j == 1:
return float64(g.d_1) + 1 return g.d_1 + 1
case i == 1 && j == 2: case i == 1 && j == 2:
return float64(g.ty) return g.ty
default: default:
panic("ebiten: i or j is out of index") panic("ebiten: i or j is out of index")
} }
@ -116,25 +111,25 @@ 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) {
a := (float64(g.a_1) + 1) * x a := (g.a_1 + 1) * x
b := float64(g.b) * x b := g.b * x
tx := float64(g.tx) * x tx := g.tx * x
c := float64(g.c) * y c := g.c * y
d := (float64(g.d_1) + 1) * y d := (g.d_1 + 1) * y
ty := float64(g.ty) * y ty := g.ty * y
g.a_1 = float32(a) - 1 g.a_1 = a - 1
g.b = float32(b) g.b = b
g.c = float32(c) g.c = c
g.d_1 = float32(d) - 1 g.d_1 = d - 1
g.tx = float32(tx) g.tx = tx
g.ty = float32(ty) g.ty = ty
} }
// Translate translates the matrix by (tx, ty). // Translate translates the matrix by (tx, ty).
func (g *GeoM) Translate(tx, ty float64) { func (g *GeoM) Translate(tx, ty float64) {
g.tx += float32(tx) g.tx += tx
g.ty += float32(ty) g.ty += ty
} }
// Rotate rotates the matrix by theta. // Rotate rotates the matrix by theta.
@ -144,8 +139,7 @@ func (g *GeoM) Rotate(theta float64) {
return return
} }
sin64, cos64 := math.Sincos(theta) sin, cos := math.Sincos(theta)
sin, cos := float32(sin64), float32(cos64)
a := cos*(g.a_1+1) - sin*g.c a := cos*(g.a_1+1) - sin*g.c
b := cos*g.b - sin*(g.d_1+1) b := cos*g.b - sin*(g.d_1+1)
@ -164,9 +158,8 @@ func (g *GeoM) Rotate(theta float64) {
// Skew skews the matrix by (skewX, skewY). The unit is radian. // Skew skews the matrix by (skewX, skewY). The unit is radian.
func (g *GeoM) Skew(skewX, skewY float64) { func (g *GeoM) Skew(skewX, skewY float64) {
sx64 := math.Tan(skewX) sx := math.Tan(skewX)
sy64 := math.Tan(skewY) sy := math.Tan(skewY)
sx, sy := float32(sx64), float32(sy64)
a := (g.a_1 + 1) + g.c*sx a := (g.a_1 + 1) + g.c*sx
b := g.b + (g.d_1+1)*sx b := g.b + (g.d_1+1)*sx
@ -183,7 +176,7 @@ func (g *GeoM) Skew(skewX, skewY float64) {
g.ty = ty g.ty = ty
} }
func (g *GeoM) det() float32 { func (g *GeoM) det() float64 {
return (g.a_1+1)*(g.d_1+1) - g.b*g.c return (g.a_1+1)*(g.d_1+1) - g.b*g.c
} }
@ -218,7 +211,7 @@ func (g *GeoM) Invert() {
// 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) {
e := float32(element) e := element
switch { switch {
case i == 0 && j == 0: case i == 0 && j == 0:
g.a_1 = e - 1 g.a_1 = e - 1

View File

@ -197,7 +197,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
filter = driver.Filter(img.filter) filter = driver.Filter(img.filter)
} }
a, b, c, d, tx, ty := geom.elements() a, b, c, d, tx, ty := geom.elements32()
i.buffered.DrawImage(img.buffered, img.Bounds(), a, b, c, d, tx, ty, options.ColorM.impl, mode, filter) i.buffered.DrawImage(img.buffered, img.Bounds(), a, b, c, d, tx, ty, options.ColorM.impl, mode, filter)
return nil return nil
} }