mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
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:
parent
6bc62b8f17
commit
4b63be0156
73
geom.go
73
geom.go
@ -26,12 +26,12 @@ const GeoMDim = 3
|
||||
//
|
||||
// The initial value is identity.
|
||||
type GeoM struct {
|
||||
a_1 float32 // The actual 'a' value minus 1
|
||||
b float32
|
||||
c float32
|
||||
d_1 float32 // The actual 'd' value minus 1
|
||||
tx float32
|
||||
ty float32
|
||||
a_1 float64 // The actual 'a' value minus 1
|
||||
b float64
|
||||
c float64
|
||||
d_1 float64 // The actual 'd' value minus 1
|
||||
tx float64
|
||||
ty float64
|
||||
}
|
||||
|
||||
// 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.
|
||||
// The return value is x and y values of the result vector.
|
||||
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
|
||||
}
|
||||
|
||||
func (g *GeoM) elements() (a, b, c, d, tx, ty float32) {
|
||||
return g.a_1 + 1, g.b, g.c, g.d_1 + 1, g.tx, g.ty
|
||||
func (g *GeoM) elements32() (a, b, c, d, tx, ty float32) {
|
||||
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).
|
||||
func (g *GeoM) Element(i, j int) float64 {
|
||||
switch {
|
||||
case i == 0 && j == 0:
|
||||
return float64(g.a_1) + 1
|
||||
return g.a_1 + 1
|
||||
case i == 0 && j == 1:
|
||||
return float64(g.b)
|
||||
return g.b
|
||||
case i == 0 && j == 2:
|
||||
return float64(g.tx)
|
||||
return g.tx
|
||||
case i == 1 && j == 0:
|
||||
return float64(g.c)
|
||||
return g.c
|
||||
case i == 1 && j == 1:
|
||||
return float64(g.d_1) + 1
|
||||
return g.d_1 + 1
|
||||
case i == 1 && j == 2:
|
||||
return float64(g.ty)
|
||||
return g.ty
|
||||
default:
|
||||
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).
|
||||
func (g *GeoM) Scale(x, y float64) {
|
||||
a := (float64(g.a_1) + 1) * x
|
||||
b := float64(g.b) * x
|
||||
tx := float64(g.tx) * x
|
||||
c := float64(g.c) * y
|
||||
d := (float64(g.d_1) + 1) * y
|
||||
ty := float64(g.ty) * y
|
||||
a := (g.a_1 + 1) * x
|
||||
b := g.b * x
|
||||
tx := g.tx * x
|
||||
c := g.c * y
|
||||
d := (g.d_1 + 1) * y
|
||||
ty := g.ty * y
|
||||
|
||||
g.a_1 = float32(a) - 1
|
||||
g.b = float32(b)
|
||||
g.c = float32(c)
|
||||
g.d_1 = float32(d) - 1
|
||||
g.tx = float32(tx)
|
||||
g.ty = float32(ty)
|
||||
g.a_1 = a - 1
|
||||
g.b = b
|
||||
g.c = c
|
||||
g.d_1 = d - 1
|
||||
g.tx = tx
|
||||
g.ty = ty
|
||||
}
|
||||
|
||||
// Translate translates the matrix by (tx, ty).
|
||||
func (g *GeoM) Translate(tx, ty float64) {
|
||||
g.tx += float32(tx)
|
||||
g.ty += float32(ty)
|
||||
g.tx += tx
|
||||
g.ty += ty
|
||||
}
|
||||
|
||||
// Rotate rotates the matrix by theta.
|
||||
@ -144,8 +139,7 @@ func (g *GeoM) Rotate(theta float64) {
|
||||
return
|
||||
}
|
||||
|
||||
sin64, cos64 := math.Sincos(theta)
|
||||
sin, cos := float32(sin64), float32(cos64)
|
||||
sin, cos := math.Sincos(theta)
|
||||
|
||||
a := cos*(g.a_1+1) - sin*g.c
|
||||
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.
|
||||
func (g *GeoM) Skew(skewX, skewY float64) {
|
||||
sx64 := math.Tan(skewX)
|
||||
sy64 := math.Tan(skewY)
|
||||
sx, sy := float32(sx64), float32(sy64)
|
||||
sx := math.Tan(skewX)
|
||||
sy := math.Tan(skewY)
|
||||
|
||||
a := (g.a_1 + 1) + g.c*sx
|
||||
b := g.b + (g.d_1+1)*sx
|
||||
@ -183,7 +176,7 @@ func (g *GeoM) Skew(skewX, skewY float64) {
|
||||
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
|
||||
}
|
||||
|
||||
@ -218,7 +211,7 @@ func (g *GeoM) Invert() {
|
||||
|
||||
// SetElement sets an element at (i, j).
|
||||
func (g *GeoM) SetElement(i, j int, element float64) {
|
||||
e := float32(element)
|
||||
e := element
|
||||
switch {
|
||||
case i == 0 && j == 0:
|
||||
g.a_1 = e - 1
|
||||
|
2
image.go
2
image.go
@ -197,7 +197,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
|
||||
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)
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user