2014-12-24 03:04:10 +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 15:16:04 +01:00
|
|
|
|
2014-12-09 14:09:22 +01:00
|
|
|
package ebiten
|
|
|
|
|
|
|
|
import (
|
2018-05-07 18:53:59 +02:00
|
|
|
"fmt"
|
2018-06-16 17:52:25 +02:00
|
|
|
"math"
|
2014-12-09 14:09:22 +01:00
|
|
|
)
|
|
|
|
|
2014-12-26 14:50:02 +01:00
|
|
|
// GeoMDim is a dimension of a GeoM.
|
2018-06-16 17:52:25 +02:00
|
|
|
const GeoMDim = 3
|
2014-12-09 14:09:22 +01:00
|
|
|
|
2014-12-26 14:50:02 +01:00
|
|
|
// A GeoM represents a matrix to transform geometry when rendering an image.
|
2014-12-26 03:31:20 +01:00
|
|
|
//
|
|
|
|
// The initial value is identity.
|
2014-12-26 14:50:02 +01:00
|
|
|
type GeoM struct {
|
2018-06-16 19:15:40 +02:00
|
|
|
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
|
2015-01-03 14:54:01 +01:00
|
|
|
}
|
|
|
|
|
2018-05-07 18:53:59 +02:00
|
|
|
// String returns a string representation of GeoM.
|
|
|
|
func (g *GeoM) String() string {
|
2018-06-16 17:52:25 +02:00
|
|
|
return fmt.Sprintf("[[%f, %f, %f], [%f, %f, %f]]", g.a_1+1, g.b, g.tx, g.c, g.d_1+1, g.ty)
|
2018-05-07 18:53:59 +02:00
|
|
|
}
|
|
|
|
|
2017-05-25 15:50:47 +02:00
|
|
|
// Reset resets the GeoM as identity.
|
|
|
|
func (g *GeoM) Reset() {
|
2018-06-16 17:52:25 +02:00
|
|
|
g.a_1 = 0
|
|
|
|
g.b = 0
|
|
|
|
g.c = 0
|
|
|
|
g.d_1 = 0
|
|
|
|
g.tx = 0
|
|
|
|
g.ty = 0
|
2017-05-25 15:50:47 +02:00
|
|
|
}
|
|
|
|
|
2017-09-16 11:00:11 +02:00
|
|
|
// Apply pre-multiplies a vector (x, y, 1) by the matrix.
|
2017-09-30 18:59:34 +02:00
|
|
|
// In other words, Apply calculates GeoM * (x, y, 1)^T.
|
2017-09-16 11:00:11 +02:00
|
|
|
// The return value is x and y values of the result vector.
|
2018-06-16 19:15:40 +02:00
|
|
|
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) {
|
2018-06-16 17:52:25 +02:00
|
|
|
return (g.a_1+1)*x + g.b*y + g.tx, g.c*x + (g.d_1+1)*y + g.ty
|
2017-09-16 10:19:45 +02:00
|
|
|
}
|
|
|
|
|
2018-06-17 12:45:09 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-12-14 14:05:44 +01:00
|
|
|
// Element returns a value of a matrix at (i, j).
|
2015-01-02 17:54:12 +01:00
|
|
|
func (g *GeoM) Element(i, j int) float64 {
|
2017-05-23 04:30:54 +02:00
|
|
|
switch {
|
|
|
|
case i == 0 && j == 0:
|
2018-06-16 19:15:40 +02:00
|
|
|
return float64(g.a_1) + 1
|
2017-05-23 04:30:54 +02:00
|
|
|
case i == 0 && j == 1:
|
2018-06-16 19:15:40 +02:00
|
|
|
return float64(g.b)
|
2017-05-23 04:30:54 +02:00
|
|
|
case i == 0 && j == 2:
|
2018-06-16 19:15:40 +02:00
|
|
|
return float64(g.tx)
|
2017-05-23 04:30:54 +02:00
|
|
|
case i == 1 && j == 0:
|
2018-06-16 19:15:40 +02:00
|
|
|
return float64(g.c)
|
2017-05-23 04:30:54 +02:00
|
|
|
case i == 1 && j == 1:
|
2018-06-16 19:15:40 +02:00
|
|
|
return float64(g.d_1) + 1
|
2017-05-23 04:30:54 +02:00
|
|
|
case i == 1 && j == 2:
|
2018-06-16 19:15:40 +02:00
|
|
|
return float64(g.ty)
|
2017-05-23 04:30:54 +02:00
|
|
|
default:
|
|
|
|
panic("ebiten: i or j is out of index")
|
|
|
|
}
|
2014-12-13 06:41:38 +01:00
|
|
|
}
|
|
|
|
|
2014-12-14 14:05:44 +01:00
|
|
|
// Concat multiplies a geometry matrix with the other geometry matrix.
|
2016-02-15 15:08:10 +01:00
|
|
|
// This is same as muptiplying the matrix other and the matrix g in this order.
|
2014-12-29 11:04:18 +01:00
|
|
|
func (g *GeoM) Concat(other GeoM) {
|
2018-06-16 17:52:25 +02:00
|
|
|
a := (other.a_1+1)*(g.a_1+1) + other.b*g.c
|
|
|
|
b := (other.a_1+1)*g.b + other.b*(g.d_1+1)
|
|
|
|
tx := (other.a_1+1)*g.tx + other.b*g.ty + other.tx
|
|
|
|
c := other.c*(g.a_1+1) + (other.d_1+1)*g.c
|
|
|
|
d := other.c*g.b + (other.d_1+1)*(g.d_1+1)
|
|
|
|
ty := other.c*g.tx + (other.d_1+1)*g.ty + other.ty
|
|
|
|
|
|
|
|
g.a_1 = a - 1
|
|
|
|
g.b = b
|
|
|
|
g.c = c
|
|
|
|
g.d_1 = d - 1
|
|
|
|
g.tx = tx
|
|
|
|
g.ty = ty
|
2014-12-09 14:09:22 +01:00
|
|
|
}
|
|
|
|
|
2016-12-19 18:41:19 +01:00
|
|
|
// Add is deprecated as of 1.5.0-alpha.
|
2016-12-19 19:05:30 +01:00
|
|
|
// Note that this doesn't make sense as an operation for affine matrices.
|
2014-12-29 11:04:18 +01:00
|
|
|
func (g *GeoM) Add(other GeoM) {
|
2018-06-16 17:52:25 +02:00
|
|
|
g.a_1 += other.a_1
|
|
|
|
g.b += other.b
|
|
|
|
g.c += other.c
|
|
|
|
g.d_1 += other.d_1
|
|
|
|
g.tx += other.tx
|
|
|
|
g.ty += other.ty
|
2014-12-25 20:22:06 +01:00
|
|
|
}
|
|
|
|
|
2015-02-18 03:04:23 +01:00
|
|
|
// Scale scales the matrix by (x, y).
|
2015-01-03 10:35:44 +01:00
|
|
|
func (g *GeoM) Scale(x, y float64) {
|
2018-06-16 19:15:40 +02:00
|
|
|
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
|
2018-06-16 17:52:25 +02:00
|
|
|
|
2018-06-16 19:15:40 +02:00
|
|
|
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)
|
2015-01-03 10:35:44 +01:00
|
|
|
}
|
|
|
|
|
2017-09-09 20:44:03 +02:00
|
|
|
// Translate translates the matrix by (tx, ty).
|
2015-01-03 10:35:44 +01:00
|
|
|
func (g *GeoM) Translate(tx, ty float64) {
|
2018-06-16 19:15:40 +02:00
|
|
|
g.tx += float32(tx)
|
|
|
|
g.ty += float32(ty)
|
2018-06-16 17:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rotate rotates the matrix by theta.
|
|
|
|
// The unit is radian.
|
|
|
|
func (g *GeoM) Rotate(theta float64) {
|
2018-06-17 12:45:09 +02:00
|
|
|
if theta == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-16 19:15:40 +02:00
|
|
|
sin64, cos64 := math.Sincos(theta)
|
|
|
|
sin, cos := float32(sin64), float32(cos64)
|
2018-06-16 17:52:25 +02:00
|
|
|
|
|
|
|
a := cos*(g.a_1+1) - sin*g.c
|
|
|
|
b := cos*g.b - sin*(g.d_1+1)
|
|
|
|
tx := cos*g.tx - sin*g.ty
|
|
|
|
c := sin*(g.a_1+1) + cos*g.c
|
|
|
|
d := sin*g.b + cos*(g.d_1+1)
|
|
|
|
ty := sin*g.tx + cos*g.ty
|
|
|
|
|
|
|
|
g.a_1 = a - 1
|
|
|
|
g.b = b
|
|
|
|
g.c = c
|
|
|
|
g.d_1 = d - 1
|
|
|
|
g.tx = tx
|
|
|
|
g.ty = ty
|
|
|
|
}
|
|
|
|
|
2018-08-01 07:15:30 +02:00
|
|
|
// 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)
|
|
|
|
|
|
|
|
a := (g.a_1 + 1) + g.c*sx
|
|
|
|
b := g.b + (g.d_1+1)*sx
|
|
|
|
c := (g.a_1+1)*sy + g.c
|
|
|
|
d := g.b*sy + (g.d_1 + 1)
|
|
|
|
tx := g.tx + g.ty*sx
|
|
|
|
ty := g.ty + g.tx*sy
|
|
|
|
|
|
|
|
g.a_1 = a - 1
|
|
|
|
g.b = b
|
|
|
|
g.c = c
|
|
|
|
g.d_1 = d - 1
|
|
|
|
g.tx = tx
|
|
|
|
g.ty = ty
|
|
|
|
}
|
|
|
|
|
2018-06-16 19:15:40 +02:00
|
|
|
func (g *GeoM) det() float32 {
|
2018-06-16 17:52:25 +02:00
|
|
|
return (g.a_1+1)*(g.d_1+1) - g.b*g.c
|
2015-01-03 10:35:44 +01:00
|
|
|
}
|
|
|
|
|
2018-03-11 11:13:39 +01:00
|
|
|
// IsInvertible returns a boolean value indicating
|
|
|
|
// whether the matrix g is invertible or not.
|
|
|
|
func (g *GeoM) IsInvertible() bool {
|
2018-06-16 17:52:25 +02:00
|
|
|
return g.det() != 0
|
2018-03-11 11:13:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invert inverts the matrix.
|
|
|
|
// If g is not invertible, Invert panics.
|
|
|
|
func (g *GeoM) Invert() {
|
2018-06-16 17:52:25 +02:00
|
|
|
det := g.det()
|
|
|
|
if det == 0 {
|
|
|
|
panic("ebiten: g is not invertible")
|
|
|
|
}
|
2018-03-11 11:13:39 +01:00
|
|
|
|
2018-06-16 17:52:25 +02:00
|
|
|
a := (g.d_1 + 1) / det
|
|
|
|
b := -g.b / det
|
|
|
|
c := -g.c / det
|
|
|
|
d := (g.a_1 + 1) / det
|
|
|
|
tx := (-(g.d_1+1)*g.tx + g.b*g.ty) / det
|
|
|
|
ty := (g.c*g.tx + -(g.a_1+1)*g.ty) / det
|
|
|
|
|
|
|
|
g.a_1 = a - 1
|
|
|
|
g.b = b
|
|
|
|
g.c = c
|
|
|
|
g.d_1 = d - 1
|
|
|
|
g.tx = tx
|
|
|
|
g.ty = ty
|
2015-01-05 02:30:33 +01:00
|
|
|
}
|
|
|
|
|
2014-12-26 02:33:50 +01:00
|
|
|
// SetElement sets an element at (i, j).
|
2014-12-26 14:50:02 +01:00
|
|
|
func (g *GeoM) SetElement(i, j int, element float64) {
|
2018-06-16 19:15:40 +02:00
|
|
|
e := float32(element)
|
2018-06-16 17:52:25 +02:00
|
|
|
switch {
|
|
|
|
case i == 0 && j == 0:
|
2018-06-16 19:15:40 +02:00
|
|
|
g.a_1 = e - 1
|
2018-06-16 17:52:25 +02:00
|
|
|
case i == 0 && j == 1:
|
2018-06-16 19:15:40 +02:00
|
|
|
g.b = e
|
2018-06-16 17:52:25 +02:00
|
|
|
case i == 0 && j == 2:
|
2018-06-16 19:15:40 +02:00
|
|
|
g.tx = e
|
2018-06-16 17:52:25 +02:00
|
|
|
case i == 1 && j == 0:
|
2018-06-16 19:15:40 +02:00
|
|
|
g.c = e
|
2018-06-16 17:52:25 +02:00
|
|
|
case i == 1 && j == 1:
|
2018-06-16 19:15:40 +02:00
|
|
|
g.d_1 = e - 1
|
2018-06-16 17:52:25 +02:00
|
|
|
case i == 1 && j == 2:
|
2018-06-16 19:15:40 +02:00
|
|
|
g.ty = e
|
2018-06-16 17:52:25 +02:00
|
|
|
default:
|
|
|
|
panic("ebiten: i or j is out of index")
|
|
|
|
}
|
2014-12-09 14:09:22 +01:00
|
|
|
}
|
|
|
|
|
2016-05-14 13:43:36 +02:00
|
|
|
// ScaleGeo is deprecated as of 1.2.0-alpha. Use Scale instead.
|
2014-12-26 14:50:02 +01:00
|
|
|
func ScaleGeo(x, y float64) GeoM {
|
2017-09-16 08:49:12 +02:00
|
|
|
g := GeoM{}
|
|
|
|
g.Scale(x, y)
|
|
|
|
return g
|
2014-12-09 14:09:22 +01:00
|
|
|
}
|
|
|
|
|
2016-05-14 13:43:36 +02:00
|
|
|
// TranslateGeo is deprecated as of 1.2.0-alpha. Use Translate instead.
|
2014-12-26 14:50:02 +01:00
|
|
|
func TranslateGeo(tx, ty float64) GeoM {
|
2017-09-16 08:49:12 +02:00
|
|
|
g := GeoM{}
|
|
|
|
g.Translate(tx, ty)
|
|
|
|
return g
|
2014-12-09 14:09:22 +01:00
|
|
|
}
|
|
|
|
|
2016-05-14 13:43:36 +02:00
|
|
|
// RotateGeo is deprecated as of 1.2.0-alpha. Use Rotate instead.
|
2014-12-26 14:50:02 +01:00
|
|
|
func RotateGeo(theta float64) GeoM {
|
2017-09-16 08:49:12 +02:00
|
|
|
g := GeoM{}
|
|
|
|
g.Rotate(theta)
|
|
|
|
return g
|
2014-12-09 14:09:22 +01:00
|
|
|
}
|