Skip to content

Commit c931020

Browse files
committed
Change from float64 to float32 for performance
1 parent 8d6a0a0 commit c931020

File tree

6 files changed

+32
-31
lines changed

6 files changed

+32
-31
lines changed

examples/GeoM/geom/game.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const (
2222
type Game struct {
2323
gopher *koebiten.Image
2424
x, y int
25-
scale float64
26-
theta float64
25+
scale float32
26+
theta float32
2727
}
2828

2929
var (
@@ -89,10 +89,10 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (w, h int) {
8989

9090
func (g *Game) Draw(screen *koebiten.Image) {
9191
op := koebiten.DrawImageOptions{}
92-
op.GeoM.Translate(-float64(gopherWidth)/2, -float64(gopherHeight)/2)
92+
op.GeoM.Translate(-float32(gopherWidth)/2, -float32(gopherHeight)/2)
9393
op.GeoM.Scale(g.scale, g.scale)
9494
op.GeoM.Rotate(g.theta)
95-
op.GeoM.Translate(float64(g.x), float64(g.y))
95+
op.GeoM.Translate(float32(g.x), float32(g.y))
9696
g.gopher.DrawImage(screen, op)
9797
}
9898

games/jumpingopher/jumpingopher/game.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func drawWalls(c *cloud) {
145145
koebiten.DrawImageFS(nil, fsys, "cloud.png", c.cloudX, c.holeY-cloudHeight)
146146
}
147147

148-
func walkGopher(x, y float64, frames int) {
148+
func walkGopher(x, y float32, frames int) {
149149
img := "gopher.png"
150150
if frames%2 != 0 {
151151
img = "gopher_r.png"

games/snakegame/snakegame/snakegame.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (g *Game) Update() error {
131131
if next == g.food {
132132
g.spawnFood()
133133
g.score = len(g.snake) - 1
134-
g.speed = time.Duration(float64(g.speed) * 0.95)
134+
g.speed = time.Duration(float32(g.speed) * 0.95)
135135
} else {
136136
g.snake = g.snake[:len(g.snake)-1]
137137
}

geom.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ package koebiten
1616

1717
import (
1818
"fmt"
19-
"math"
19+
20+
"github.com/chewxy/math32"
2021
)
2122

2223
// GeoMDim is a dimension of a GeoM.
@@ -26,12 +27,12 @@ const GeoMDim = 3
2627
//
2728
// The initial value is identity.
2829
type GeoM struct {
29-
a_1 float64 // The actual 'a' value minus 1
30-
b float64
31-
c float64
32-
d_1 float64 // The actual 'd' value minus 1
33-
tx float64
34-
ty float64
30+
a_1 float32 // The actual 'a' value minus 1
31+
b float32
32+
c float32
33+
d_1 float32 // The actual 'd' value minus 1
34+
tx float32
35+
ty float32
3536
}
3637

3738
// String returns a string representation of GeoM.
@@ -52,7 +53,7 @@ func (g *GeoM) Reset() {
5253
// Apply pre-multiplies a vector (x, y, 1) by the matrix.
5354
// In other words, Apply calculates GeoM * (x, y, 1)^T.
5455
// The return value is x and y values of the result vector.
55-
func (g *GeoM) Apply(x, y float64) (float64, float64) {
56+
func (g *GeoM) Apply(x, y float32) (float32, float32) {
5657
return (g.a_1+1)*x + g.b*y + g.tx, g.c*x + (g.d_1+1)*y + g.ty
5758
}
5859

@@ -61,7 +62,7 @@ func (g *GeoM) elements32() (a, b, c, d, tx, ty float32) {
6162
}
6263

6364
// Element returns a value of a matrix at (i, j).
64-
func (g *GeoM) Element(i, j int) float64 {
65+
func (g *GeoM) Element(i, j int) float32 {
6566
switch {
6667
case i == 0 && j == 0:
6768
return g.a_1 + 1
@@ -99,7 +100,7 @@ func (g *GeoM) Concat(other GeoM) {
99100
}
100101

101102
// Scale scales the matrix by (x, y).
102-
func (g *GeoM) Scale(x, y float64) {
103+
func (g *GeoM) Scale(x, y float32) {
103104
a := (g.a_1 + 1) * x
104105
b := g.b * x
105106
tx := g.tx * x
@@ -116,19 +117,19 @@ func (g *GeoM) Scale(x, y float64) {
116117
}
117118

118119
// Translate translates the matrix by (tx, ty).
119-
func (g *GeoM) Translate(tx, ty float64) {
120+
func (g *GeoM) Translate(tx, ty float32) {
120121
g.tx += tx
121122
g.ty += ty
122123
}
123124

124125
// Rotate rotates the matrix clockwise by theta.
125126
// The unit is radian.
126-
func (g *GeoM) Rotate(theta float64) {
127+
func (g *GeoM) Rotate(theta float32) {
127128
if theta == 0 {
128129
return
129130
}
130131

131-
sin, cos := math.Sincos(theta)
132+
sin, cos := math32.Sincos(theta)
132133

133134
a := cos*(g.a_1+1) - sin*g.c
134135
b := cos*g.b - sin*(g.d_1+1)
@@ -146,9 +147,9 @@ func (g *GeoM) Rotate(theta float64) {
146147
}
147148

148149
// Skew skews the matrix by (skewX, skewY). The unit is radian.
149-
func (g *GeoM) Skew(skewX, skewY float64) {
150-
sx := math.Tan(skewX)
151-
sy := math.Tan(skewY)
150+
func (g *GeoM) Skew(skewX, skewY float32) {
151+
sx := math32.Tan(skewX)
152+
sy := math32.Tan(skewY)
152153

153154
a := (g.a_1 + 1) + g.c*sx
154155
b := g.b + (g.d_1+1)*sx
@@ -165,7 +166,7 @@ func (g *GeoM) Skew(skewX, skewY float64) {
165166
g.ty = ty
166167
}
167168

168-
func (g *GeoM) det2x2() float64 {
169+
func (g *GeoM) det2x2() float32 {
169170
return (g.a_1+1)*(g.d_1+1) - g.b*g.c
170171
}
171172

@@ -199,7 +200,7 @@ func (g *GeoM) Invert() {
199200
}
200201

201202
// SetElement sets an element at (i, j).
202-
func (g *GeoM) SetElement(i, j int, element float64) {
203+
func (g *GeoM) SetElement(i, j int, element float32) {
203204
e := element
204205
switch {
205206
case i == 0 && j == 0:

image.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package koebiten
33
import (
44
"image/color"
55
"io/fs"
6-
"math"
76

7+
"github.com/chewxy/math32"
88
"tinygo.org/x/drivers/image/png"
99
"tinygo.org/x/drivers/pixel"
1010
)
@@ -144,8 +144,8 @@ func (i *Image) DrawImage(dst Displayer, options DrawImageOptions) {
144144
for yy := 0; yy < min(h, int(dh)); yy++ {
145145
for xx := 0; xx < min(w, int(dw)); xx++ {
146146
if i.img.Get(xx, yy) == true {
147-
xxf, yyf := geoM.Apply(float64(xx), float64(yy))
148-
dst.SetPixel(int16(math.Round(float64(xxf))), int16(math.Round(float64(yyf))), white)
147+
xxf, yyf := geoM.Apply(float32(xx), float32(yy))
148+
dst.SetPixel(int16(math32.Round(xxf)), int16(math32.Round(yyf)), white)
149149
}
150150
}
151151
}

koebiten.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"fmt"
88
"image/color"
99
"io/fs"
10-
"math"
1110
"reflect"
1211
"strings"
1312
"time"
1413

14+
"github.com/chewxy/math32"
1515
"tinygo.org/x/drivers"
1616
"tinygo.org/x/drivers/image/png"
1717
"tinygo.org/x/drivers/pixel"
@@ -214,7 +214,7 @@ type DrawImageFSOptions struct {
214214
// Deprecated: Use Image and Image.DrawImage instead.
215215
func DrawImageFS(dst Displayer, fsys fs.FS, path string, x, y int) {
216216
op := DrawImageFSOptions{}
217-
op.GeoM.Translate(float64(x), float64(y))
217+
op.GeoM.Translate(float32(x), float32(y))
218218
DrawImageFSWithOptions(dst, fsys, path, op)
219219
}
220220

@@ -273,8 +273,8 @@ func DrawImageFSWithOptions(dst Displayer, fsys fs.FS, path string, options Draw
273273
for yy := 0; yy < h; yy++ {
274274
for xx := 0; xx < w; xx++ {
275275
if img.Get(xx, yy) == true {
276-
xxf, yyf := geoM.Apply(float64(xx), float64(yy))
277-
dst.SetPixel(int16(math.Round(float64(xxf))), int16(math.Round(float64(yyf))), white)
276+
xxf, yyf := geoM.Apply(float32(xx), float32(yy))
277+
dst.SetPixel(int16(math32.Round(xxf)), int16(math32.Round(yyf)), white)
278278
}
279279
}
280280
}

0 commit comments

Comments
 (0)