-
Notifications
You must be signed in to change notification settings - Fork 0
/
maths.go
64 lines (50 loc) · 1.33 KB
/
maths.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package tempura
import (
"math"
"github.com/hajimehoshi/ebiten"
)
// DegToRad converts degrees to radians
func DegToRad(deg float64) (rad float64) {
return deg * math.Pi / 180
}
// RadToDeg converts radians to degrees
func RadToDeg(rad float64) (deg float64) {
return rad * 180 / math.Pi
}
// Fit returns the Matrix that will transform a source Rect
// into the dest Rect
func Fit(source, dest Rect) ebiten.GeoM {
scaleX := dest.W() / source.W()
scaleY := dest.H() / source.H()
mat := ebiten.GeoM{}
mat.Translate(-source.Min.X, -source.Min.Y)
mat.Scale(scaleX, scaleY)
mat.Translate(dest.Min.X, dest.Min.Y)
return mat
}
// FitGeoM returns the Matrix that will transform a source Rect
// into the dest Rect
func FitRotated(rot float64, source, dest Rect) ebiten.GeoM {
scaleX := dest.W() / source.W()
scaleY := dest.H() / source.H()
mat := ebiten.GeoM{}
// rotate about center of source
mat.Translate(-source.W()/2, -source.W()/2)
mat.Rotate(rot)
mat.Translate(source.W()/2, source.W()/2)
// scale
mat.Scale(scaleX, scaleY)
// move to destination
mat.Translate(dest.Min.X, dest.Min.Y)
return mat
}
// Collision returns if two rectangles intersect
func Collision(r1, r2 Rect) bool {
if r1.Min.X > r2.Max.X || r2.Min.X > r1.Max.X {
return false
}
if r1.Min.Y > r2.Max.Y || r2.Min.Y > r1.Max.Y {
return false
}
return true
}