| 1 |
package gfx |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math" |
| 6 |
) |
| 7 |
|
| 8 |
// Matrix is a 2x3 affine matrix that can be used for all kinds of spatial transforms, such |
| 9 |
// as movement, scaling and rotations. |
| 10 |
// |
| 11 |
// Matrix has a handful of useful methods, each of which adds a transformation to the matrix. For |
| 12 |
// example: |
| 13 |
// |
| 14 |
// gfx.IM.Moved(gfx.V(100, 200)).Rotated(gfx.ZV, math.Pi/2) |
| 15 |
// |
| 16 |
// This code creates a Matrix that first moves everything by 100 units horizontally and 200 units |
| 17 |
// vertically and then rotates everything by 90 degrees around the origin. |
| 18 |
// |
| 19 |
// Layout is: |
| 20 |
// [0] [2] [4] |
| 21 |
// [1] [3] [5] |
| 22 |
// |
| 23 |
// 0 0 1 (implicit row) |
| 24 |
type Matrix [6]float64 |
| 25 |
|
| 26 |
// IM stands for identity matrix. Does nothing, no transformation. |
| 27 |
var IM = Matrix{1, 0, 0, 1, 0, 0} |
| 28 |
|
| 29 |
// String returns a string representation of the Matrix. |
| 30 |
// |
| 31 |
// m := gfx.IM |
| 32 |
// fmt.Println(m) // Matrix(1 0 0 | 0 1 0) |
| 33 |
func (m Matrix) String() string { |
| 34 |
return fmt.Sprintf( |
| 35 |
"Matrix(%v %v %v | %v %v %v)", |
| 36 |
m[0], m[2], m[4], |
| 37 |
m[1], m[3], m[5], |
| 38 |
) |
| 39 |
} |
| 40 |
|
| 41 |
// Moved moves everything by the delta vector. |
| 42 |
func (m Matrix) Moved(delta Vec) Matrix { |
| 43 |
m[4], m[5] = m[4]+delta.X, m[5]+delta.Y |
| 44 |
|
| 45 |
return m |
| 46 |
} |
| 47 |
|
| 48 |
// ScaledXY scales everything around a given point by the scale factor in each axis respectively. |
| 49 |
func (m Matrix) ScaledXY(around Vec, scale Vec) Matrix { |
| 50 |
m[4], m[5] = m[4]-around.X, m[5]-around.Y |
| 51 |
m[0], m[2], m[4] = m[0]*scale.X, m[2]*scale.X, m[4]*scale.X |
| 52 |
m[1], m[3], m[5] = m[1]*scale.Y, m[3]*scale.Y, m[5]*scale.Y |
| 53 |
m[4], m[5] = m[4]+around.X, m[5]+around.Y |
| 54 |
|
| 55 |
return m |
| 56 |
} |
| 57 |
|
| 58 |
// Scaled scales everything around a given point by the scale factor. |
| 59 |
func (m Matrix) Scaled(around Vec, scale float64) Matrix { |
| 60 |
return m.ScaledXY(around, V(scale, scale)) |
| 61 |
} |
| 62 |
|
| 63 |
// Rotated rotates everything around a given point by the given angle in radians. |
| 64 |
func (m Matrix) Rotated(around Vec, angle float64) Matrix { |
| 65 |
sint, cost := math.Sincos(angle) |
| 66 |
m[4], m[5] = m[4]-around.X, m[5]-around.Y |
| 67 |
m = m.Chained(Matrix{cost, sint, -sint, cost, 0, 0}) |
| 68 |
m[4], m[5] = m[4]+around.X, m[5]+around.Y |
| 69 |
|
| 70 |
return m |
| 71 |
} |
| 72 |
|
| 73 |
// RotatedDegrees rotates everything around a given point by the given number of degrees. |
| 74 |
func (m Matrix) RotatedDegrees(around Vec, degrees float64) Matrix { |
| 75 |
return m.Rotated(around, degrees*math.Pi/180) |
| 76 |
} |
| 77 |
|
| 78 |
// Chained adds another Matrix to this one. All tranformations by the next Matrix will be applied |
| 79 |
// after the transformations of this Matrix. |
| 80 |
func (m Matrix) Chained(next Matrix) Matrix { |
| 81 |
return Matrix{ |
| 82 |
next[0]*m[0] + next[2]*m[1], |
| 83 |
next[1]*m[0] + next[3]*m[1], |
| 84 |
next[0]*m[2] + next[2]*m[3], |
| 85 |
next[1]*m[2] + next[3]*m[3], |
| 86 |
next[0]*m[4] + next[2]*m[5] + next[4], |
| 87 |
next[1]*m[4] + next[3]*m[5] + next[5], |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// Project applies all transformations added to the Matrix to a vector u and returns the result. |
| 92 |
// |
| 93 |
// Time complexity is O(1). |
| 94 |
func (m Matrix) Project(u Vec) Vec { |
| 95 |
return Vec{m[0]*u.X + m[2]*u.Y + m[4], m[1]*u.X + m[3]*u.Y + m[5]} |
| 96 |
} |
| 97 |
|
| 98 |
// Unproject does the inverse operation to Project. |
| 99 |
// |
| 100 |
// Time complexity is O(1). |
| 101 |
func (m Matrix) Unproject(u Vec) Vec { |
| 102 |
det := m[0]*m[3] - m[2]*m[1] |
| 103 |
return Vec{ |
| 104 |
(m[3]*(u.X-m[4]) - m[2]*(u.Y-m[5])) / det, |
| 105 |
(-m[1]*(u.X-m[4]) + m[0]*(u.Y-m[5])) / det, |
| 106 |
} |
| 107 |
} |