2016-10-31 16:13:19 +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.
|
|
|
|
|
|
|
|
package affine
|
|
|
|
|
|
|
|
import (
|
2018-02-18 15:39:24 +01:00
|
|
|
"fmt"
|
2016-10-31 16:13:19 +01:00
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GeoMDim is a dimension of a GeoM.
|
|
|
|
const GeoMDim = 3
|
|
|
|
|
2018-02-26 16:45:57 +01:00
|
|
|
// A GeoM represents a matrix to transform geometry when rendering an image.
|
|
|
|
//
|
|
|
|
// The initial value is identity.
|
|
|
|
type GeoM struct {
|
2018-02-18 15:39:24 +01:00
|
|
|
a float64
|
|
|
|
b float64
|
|
|
|
c float64
|
|
|
|
d float64
|
|
|
|
tx float64
|
|
|
|
ty float64
|
|
|
|
}
|
|
|
|
|
2017-09-16 10:19:45 +02:00
|
|
|
func (g *GeoM) Apply(x, y float64) (x2, y2 float64) {
|
2018-02-26 16:45:57 +01:00
|
|
|
if g == nil {
|
2017-09-16 10:19:45 +02:00
|
|
|
return x, y
|
|
|
|
}
|
2018-02-26 16:45:57 +01:00
|
|
|
return g.a*x + g.b*y + g.tx, g.c*x + g.d*y + g.ty
|
2017-09-16 10:19:45 +02:00
|
|
|
}
|
|
|
|
|
2018-01-14 08:01:55 +01:00
|
|
|
func (g *GeoM) Apply32(x, y float64) (x2, y2 float32) {
|
2018-02-26 16:45:57 +01:00
|
|
|
if g == nil {
|
2018-01-14 08:01:55 +01:00
|
|
|
return float32(x), float32(y)
|
|
|
|
}
|
2018-02-26 16:45:57 +01:00
|
|
|
return float32(g.a*x + g.b*y + g.tx), float32(g.c*x + g.d*y + g.ty)
|
2018-01-14 08:01:55 +01:00
|
|
|
}
|
|
|
|
|
2017-05-23 04:30:54 +02:00
|
|
|
func (g *GeoM) Elements() (a, b, c, d, tx, ty float64) {
|
2018-02-26 16:45:57 +01:00
|
|
|
if g == nil {
|
2017-05-23 04:30:54 +02:00
|
|
|
return 1, 0, 0, 1, 0, 0
|
2017-01-19 16:37:51 +01:00
|
|
|
}
|
2018-02-26 16:45:57 +01:00
|
|
|
return g.a, g.b, g.c, g.d, g.tx, g.ty
|
2017-01-19 04:07:31 +01:00
|
|
|
}
|
|
|
|
|
2017-01-18 18:29:49 +01:00
|
|
|
// SetElement sets an element at (i, j).
|
2018-02-26 16:45:57 +01:00
|
|
|
func (g *GeoM) SetElement(i, j int, element float64) *GeoM {
|
|
|
|
a, b, c, d, tx, ty := 1.0, 0.0, 0.0, 1.0, 0.0, 0.0
|
|
|
|
if g != nil {
|
|
|
|
a, b, c, d, tx, ty = g.a, g.b, g.c, g.d, g.tx, g.ty
|
2017-05-23 04:30:54 +02:00
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case i == 0 && j == 0:
|
2018-02-18 15:39:24 +01:00
|
|
|
a = element
|
2017-05-23 04:30:54 +02:00
|
|
|
case i == 0 && j == 1:
|
2018-02-18 15:39:24 +01:00
|
|
|
b = element
|
2017-05-23 04:30:54 +02:00
|
|
|
case i == 0 && j == 2:
|
2018-02-18 15:39:24 +01:00
|
|
|
tx = element
|
2017-05-23 04:30:54 +02:00
|
|
|
case i == 1 && j == 0:
|
2018-02-18 15:39:24 +01:00
|
|
|
c = element
|
2017-05-23 04:30:54 +02:00
|
|
|
case i == 1 && j == 1:
|
2018-02-18 15:39:24 +01:00
|
|
|
d = element
|
2017-05-23 04:30:54 +02:00
|
|
|
case i == 1 && j == 2:
|
2018-02-18 15:39:24 +01:00
|
|
|
ty = element
|
2017-05-23 04:30:54 +02:00
|
|
|
default:
|
2018-02-18 15:39:24 +01:00
|
|
|
panic(fmt.Sprintf("affine: i or j is out of index: (%d, %d)", i, j))
|
|
|
|
}
|
2018-02-26 16:45:57 +01:00
|
|
|
return &GeoM{
|
2018-02-18 15:39:24 +01:00
|
|
|
a: a,
|
|
|
|
b: b,
|
|
|
|
c: c,
|
|
|
|
d: d,
|
|
|
|
tx: tx,
|
|
|
|
ty: ty,
|
2017-01-19 16:37:51 +01:00
|
|
|
}
|
2016-10-31 16:13:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Concat multiplies a geometry matrix with the other geometry matrix.
|
|
|
|
// This is same as muptiplying the matrix other and the matrix g in this order.
|
2018-02-26 16:45:57 +01:00
|
|
|
func (g *GeoM) Concat(other *GeoM) *GeoM {
|
|
|
|
if g == nil {
|
|
|
|
return other
|
2017-01-19 16:46:25 +01:00
|
|
|
}
|
2018-02-26 16:45:57 +01:00
|
|
|
if other == nil {
|
|
|
|
return g
|
2017-01-19 16:46:25 +01:00
|
|
|
}
|
2018-02-18 15:39:24 +01:00
|
|
|
|
2018-02-26 16:45:57 +01:00
|
|
|
return &GeoM{
|
|
|
|
a: other.a*g.a + other.b*g.c,
|
|
|
|
b: other.a*g.b + other.b*g.d,
|
|
|
|
tx: other.a*g.tx + other.b*g.ty + other.tx,
|
|
|
|
c: other.c*g.a + other.d*g.c,
|
|
|
|
d: other.c*g.b + other.d*g.d,
|
|
|
|
ty: other.c*g.tx + other.d*g.ty + other.ty,
|
2018-02-18 15:39:24 +01:00
|
|
|
}
|
2016-10-31 16:13:19 +01:00
|
|
|
}
|
|
|
|
|
2017-01-18 18:29:49 +01:00
|
|
|
// Add is deprecated.
|
2018-02-26 16:45:57 +01:00
|
|
|
func (g *GeoM) Add(other *GeoM) *GeoM {
|
|
|
|
if g == nil {
|
|
|
|
g = &GeoM{1, 0, 0, 1, 0, 0}
|
2017-01-19 16:46:25 +01:00
|
|
|
}
|
2018-02-26 16:45:57 +01:00
|
|
|
if other == nil {
|
|
|
|
other = &GeoM{1, 0, 0, 1, 0, 0}
|
2017-01-19 16:46:25 +01:00
|
|
|
}
|
2018-02-26 16:45:57 +01:00
|
|
|
return &GeoM{
|
|
|
|
a: g.a + other.a,
|
|
|
|
b: g.b + other.b,
|
|
|
|
c: g.c + other.c,
|
|
|
|
d: g.d + other.d,
|
|
|
|
tx: g.tx + other.tx,
|
|
|
|
ty: g.ty + other.ty,
|
2018-02-18 15:39:24 +01:00
|
|
|
}
|
2016-10-31 16:13:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scale scales the matrix by (x, y).
|
2018-02-26 16:45:57 +01:00
|
|
|
func (g *GeoM) Scale(x, y float64) *GeoM {
|
|
|
|
if g == nil {
|
|
|
|
return &GeoM{
|
2018-02-18 15:39:24 +01:00
|
|
|
a: x,
|
|
|
|
b: 0,
|
|
|
|
c: 0,
|
|
|
|
d: y,
|
|
|
|
tx: 0,
|
|
|
|
ty: 0,
|
|
|
|
}
|
2017-01-19 16:37:51 +01:00
|
|
|
}
|
2018-02-26 16:45:57 +01:00
|
|
|
return &GeoM{
|
|
|
|
a: g.a * x,
|
|
|
|
b: g.b * x,
|
|
|
|
tx: g.tx * x,
|
|
|
|
c: g.c * y,
|
|
|
|
d: g.d * y,
|
|
|
|
ty: g.ty * y,
|
2018-02-18 15:39:24 +01:00
|
|
|
}
|
2016-10-31 16:13:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Translate translates the matrix by (x, y).
|
2018-02-26 16:45:57 +01:00
|
|
|
func (g *GeoM) Translate(tx, ty float64) *GeoM {
|
|
|
|
if g == nil {
|
|
|
|
return &GeoM{
|
2018-02-18 15:39:24 +01:00
|
|
|
a: 1,
|
|
|
|
b: 0,
|
|
|
|
c: 0,
|
|
|
|
d: 1,
|
|
|
|
tx: tx,
|
|
|
|
ty: ty,
|
|
|
|
}
|
2017-01-19 16:37:51 +01:00
|
|
|
}
|
2018-02-26 16:45:57 +01:00
|
|
|
return &GeoM{
|
|
|
|
a: g.a,
|
|
|
|
b: g.b,
|
|
|
|
c: g.c,
|
|
|
|
d: g.d,
|
|
|
|
tx: g.tx + tx,
|
|
|
|
ty: g.ty + ty,
|
2018-02-18 15:39:24 +01:00
|
|
|
}
|
2016-10-31 16:13:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rotate rotates the matrix by theta.
|
2018-02-26 16:45:57 +01:00
|
|
|
func (g *GeoM) Rotate(theta float64) *GeoM {
|
2016-10-31 16:13:19 +01:00
|
|
|
sin, cos := math.Sincos(theta)
|
2018-02-26 16:45:57 +01:00
|
|
|
if g == nil {
|
|
|
|
return &GeoM{
|
2018-02-18 15:39:24 +01:00
|
|
|
a: cos,
|
|
|
|
b: -sin,
|
|
|
|
c: sin,
|
|
|
|
d: cos,
|
|
|
|
tx: 0,
|
|
|
|
ty: 0,
|
|
|
|
}
|
2018-02-26 16:45:57 +01:00
|
|
|
}
|
|
|
|
return &GeoM{
|
|
|
|
a: cos*g.a - sin*g.c,
|
|
|
|
b: cos*g.b - sin*g.d,
|
|
|
|
tx: cos*g.tx - sin*g.ty,
|
|
|
|
c: sin*g.a + cos*g.c,
|
|
|
|
d: sin*g.b + cos*g.d,
|
|
|
|
ty: sin*g.tx + cos*g.ty,
|
2018-02-18 15:39:24 +01:00
|
|
|
}
|
2016-10-31 16:13:19 +01:00
|
|
|
}
|