ebiten/geometrymatrix.go

93 lines
2.3 KiB
Go
Raw Normal View History

2014-12-09 15:16:04 +01:00
/*
Copyright 2014 Hajime Hoshi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2014-12-09 14:09:22 +01:00
package ebiten
import (
"math"
)
2014-12-14 14:05:44 +01:00
// GeometryMatrixDim is a dimension of a GeometryMatrix.
const GeometryMatrixDim = 3
2014-12-09 14:09:22 +01:00
2014-12-14 14:05:44 +01:00
// A GeometryMatrix represents a matrix to transform geometry when rendering a texture or a render target.
2014-12-09 14:09:22 +01:00
type GeometryMatrix struct {
Elements [GeometryMatrixDim - 1][GeometryMatrixDim]float64
2014-12-09 14:09:22 +01:00
}
2014-12-14 14:05:44 +01:00
// GeometryMatrixI returns an identity geometry matrix.
2014-12-09 14:09:22 +01:00
func GeometryMatrixI() GeometryMatrix {
return GeometryMatrix{
[GeometryMatrixDim - 1][GeometryMatrixDim]float64{
2014-12-09 14:09:22 +01:00
{1, 0, 0},
{0, 1, 0},
},
}
}
func (g *GeometryMatrix) dim() int {
return GeometryMatrixDim
2014-12-09 14:09:22 +01:00
}
2014-12-14 14:05:44 +01:00
// Element returns a value of a matrix at (i, j).
func (g *GeometryMatrix) Element(i, j int) float64 {
return g.Elements[i][j]
}
2014-12-14 14:05:44 +01:00
// Concat multiplies a geometry matrix with the other geometry matrix.
2014-12-09 14:09:22 +01:00
func (g *GeometryMatrix) Concat(other GeometryMatrix) {
result := GeometryMatrix{}
mul(&other, g, &result)
*g = result
}
2014-12-14 14:05:44 +01:00
// IsIdentity returns a boolean indicating whether the geometry matrix is an identity.
2014-12-09 14:09:22 +01:00
func (g *GeometryMatrix) IsIdentity() bool {
return isIdentity(g)
}
func (g *GeometryMatrix) setElement(i, j int, element float64) {
g.Elements[i][j] = element
}
2014-12-14 14:05:44 +01:00
// Translate translates the geometry matrix by (tx, ty).
2014-12-09 14:09:22 +01:00
func (g *GeometryMatrix) Translate(tx, ty float64) {
g.Elements[0][2] += tx
g.Elements[1][2] += ty
}
2014-12-14 14:05:44 +01:00
// Scale scales the geometry matrix by (x, y).
2014-12-09 14:09:22 +01:00
func (g *GeometryMatrix) Scale(x, y float64) {
g.Elements[0][0] *= x
g.Elements[0][1] *= x
g.Elements[0][2] *= x
g.Elements[1][0] *= y
g.Elements[1][1] *= y
g.Elements[1][2] *= y
}
2014-12-14 14:05:44 +01:00
// Rotate rotates the geometry matrix by theta.
2014-12-09 14:09:22 +01:00
func (g *GeometryMatrix) Rotate(theta float64) {
sin, cos := math.Sincos(theta)
rotate := GeometryMatrix{
[2][3]float64{
{cos, -sin, 0},
{sin, cos, 0},
},
}
g.Concat(rotate)
}