ebiten/geom.go

137 lines
3.4 KiB
Go
Raw Normal View History

// 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 (
"fmt"
2016-10-31 16:13:19 +01:00
"github.com/hajimehoshi/ebiten/internal/affine"
2014-12-09 14:09:22 +01:00
)
// GeoMDim is a dimension of a GeoM.
2016-10-31 16:13:19 +01:00
const GeoMDim = affine.GeoMDim
2014-12-09 14:09:22 +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.
type GeoM struct {
impl *affine.GeoM
2015-01-03 14:54:01 +01:00
}
// String returns a string representation of GeoM.
func (g *GeoM) String() string {
a, b, c, d, tx, ty := g.impl.Elements()
return fmt.Sprintf("[[%f, %f, %f], [%f, %f, %f]]", a, b, tx, c, d, ty)
}
2017-05-25 15:50:47 +02:00
// Reset resets the GeoM as identity.
func (g *GeoM) Reset() {
g.impl = nil
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.
2017-09-16 10:19:45 +02:00
func (g *GeoM) Apply(x, y float64) (x2, y2 float64) {
return g.impl.Apply(x, y)
}
2014-12-14 14:05:44 +01:00
// Element returns a value of a matrix at (i, j).
func (g *GeoM) Element(i, j int) float64 {
2017-05-23 04:30:54 +02:00
a, b, c, d, tx, ty := g.impl.Elements()
switch {
case i == 0 && j == 0:
return a
case i == 0 && j == 1:
return b
case i == 0 && j == 2:
return tx
case i == 1 && j == 0:
return c
case i == 1 && j == 1:
return d
case i == 1 && j == 2:
return ty
default:
panic("ebiten: i or j is out of index")
}
}
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) {
g.impl = g.impl.Concat(other.impl)
2014-12-09 14:09:22 +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) {
g.impl = g.impl.Add(other.impl)
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) {
g.impl = g.impl.Scale(x, y)
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) {
g.impl = g.impl.Translate(tx, ty)
2015-01-03 10:35:44 +01:00
}
// IsInvertible returns a boolean value indicating
// whether the matrix g is invertible or not.
func (g *GeoM) IsInvertible() bool {
return g.impl.IsInvertible()
}
// Invert inverts the matrix.
// If g is not invertible, Invert panics.
func (g *GeoM) Invert() {
g.impl = g.impl.Invert()
}
2016-02-15 15:08:10 +01:00
// Rotate rotates the matrix by theta.
2017-09-30 18:59:34 +02:00
// The unit is radian.
2015-01-05 02:30:33 +01:00
func (g *GeoM) Rotate(theta float64) {
g.impl = g.impl.Rotate(theta)
2015-01-05 02:30:33 +01:00
}
2014-12-26 02:33:50 +01:00
// SetElement sets an element at (i, j).
func (g *GeoM) SetElement(i, j int, element float64) {
g.impl = g.impl.SetElement(i, j, element)
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.
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.
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.
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
}