| 1 |
package drawing |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
) |
| 6 |
|
| 7 |
|
| 8 |
type Matrix [6]float64 |
| 9 |
|
| 10 |
const ( |
| 11 |
epsilon = 1e-6 |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
func (tr Matrix) Determinant() float64 { |
| 16 |
return tr[0]*tr[3] - tr[1]*tr[2] |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
func (tr Matrix) Transform(points []float64) { |
| 21 |
for i, j := 0, 1; j < len(points); i, j = i+2, j+2 { |
| 22 |
x := points[i] |
| 23 |
y := points[j] |
| 24 |
points[i] = x*tr[0] + y*tr[2] + tr[4] |
| 25 |
points[j] = x*tr[1] + y*tr[3] + tr[5] |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
func (tr Matrix) TransformPoint(x, y float64) (xres, yres float64) { |
| 31 |
xres = x*tr[0] + y*tr[2] + tr[4] |
| 32 |
yres = x*tr[1] + y*tr[3] + tr[5] |
| 33 |
return xres, yres |
| 34 |
} |
| 35 |
|
| 36 |
func minMax(x, y float64) (min, max float64) { |
| 37 |
if x > y { |
| 38 |
return y, x |
| 39 |
} |
| 40 |
return x, y |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
func (tr Matrix) TransformRectangle(x0, y0, x2, y2 float64) (nx0, ny0, nx2, ny2 float64) { |
| 45 |
points := []float64{x0, y0, x2, y0, x2, y2, x0, y2} |
| 46 |
tr.Transform(points) |
| 47 |
points[0], points[2] = minMax(points[0], points[2]) |
| 48 |
points[4], points[6] = minMax(points[4], points[6]) |
| 49 |
points[1], points[3] = minMax(points[1], points[3]) |
| 50 |
points[5], points[7] = minMax(points[5], points[7]) |
| 51 |
|
| 52 |
nx0 = math.Min(points[0], points[4]) |
| 53 |
ny0 = math.Min(points[1], points[5]) |
| 54 |
nx2 = math.Max(points[2], points[6]) |
| 55 |
ny2 = math.Max(points[3], points[7]) |
| 56 |
return nx0, ny0, nx2, ny2 |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
func (tr Matrix) InverseTransform(points []float64) { |
| 61 |
d := tr.Determinant() |
| 62 |
for i, j := 0, 1; j < len(points); i, j = i+2, j+2 { |
| 63 |
x := points[i] |
| 64 |
y := points[j] |
| 65 |
points[i] = ((x-tr[4])*tr[3] - (y-tr[5])*tr[2]) / d |
| 66 |
points[j] = ((y-tr[5])*tr[0] - (x-tr[4])*tr[1]) / d |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
func (tr Matrix) InverseTransformPoint(x, y float64) (xres, yres float64) { |
| 72 |
d := tr.Determinant() |
| 73 |
xres = ((x-tr[4])*tr[3] - (y-tr[5])*tr[2]) / d |
| 74 |
yres = ((y-tr[5])*tr[0] - (x-tr[4])*tr[1]) / d |
| 75 |
return xres, yres |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
func (tr Matrix) VectorTransform(points []float64) { |
| 81 |
for i, j := 0, 1; j < len(points); i, j = i+2, j+2 { |
| 82 |
x := points[i] |
| 83 |
y := points[j] |
| 84 |
points[i] = x*tr[0] + y*tr[2] |
| 85 |
points[j] = x*tr[1] + y*tr[3] |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
func NewIdentityMatrix() Matrix { |
| 91 |
return Matrix{1, 0, 0, 1, 0, 0} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
func NewTranslationMatrix(tx, ty float64) Matrix { |
| 96 |
return Matrix{1, 0, 0, 1, tx, ty} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
func NewScaleMatrix(sx, sy float64) Matrix { |
| 101 |
return Matrix{sx, 0, 0, sy, 0, 0} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func NewRotationMatrix(angle float64) Matrix { |
| 106 |
c := math.Cos(angle) |
| 107 |
s := math.Sin(angle) |
| 108 |
return Matrix{c, s, -s, c, 0, 0} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
func NewMatrixFromRects(rectangle1, rectangle2 [4]float64) Matrix { |
| 113 |
xScale := (rectangle2[2] - rectangle2[0]) / (rectangle1[2] - rectangle1[0]) |
| 114 |
yScale := (rectangle2[3] - rectangle2[1]) / (rectangle1[3] - rectangle1[1]) |
| 115 |
xOffset := rectangle2[0] - (rectangle1[0] * xScale) |
| 116 |
yOffset := rectangle2[1] - (rectangle1[1] * yScale) |
| 117 |
return Matrix{xScale, 0, 0, yScale, xOffset, yOffset} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
func (tr *Matrix) Inverse() { |
| 122 |
d := tr.Determinant() |
| 123 |
tr0, tr1, tr2, tr3, tr4, tr5 := tr[0], tr[1], tr[2], tr[3], tr[4], tr[5] |
| 124 |
tr[0] = tr3 / d |
| 125 |
tr[1] = -tr1 / d |
| 126 |
tr[2] = -tr2 / d |
| 127 |
tr[3] = tr0 / d |
| 128 |
tr[4] = (tr2*tr5 - tr3*tr4) / d |
| 129 |
tr[5] = (tr1*tr4 - tr0*tr5) / d |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
func (tr Matrix) Copy() Matrix { |
| 134 |
var result Matrix |
| 135 |
copy(result[:], tr[:]) |
| 136 |
return result |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
func (tr *Matrix) Compose(trToCompose Matrix) { |
| 141 |
tr0, tr1, tr2, tr3, tr4, tr5 := tr[0], tr[1], tr[2], tr[3], tr[4], tr[5] |
| 142 |
tr[0] = trToCompose[0]*tr0 + trToCompose[1]*tr2 |
| 143 |
tr[1] = trToCompose[1]*tr3 + trToCompose[0]*tr1 |
| 144 |
tr[2] = trToCompose[2]*tr0 + trToCompose[3]*tr2 |
| 145 |
tr[3] = trToCompose[3]*tr3 + trToCompose[2]*tr1 |
| 146 |
tr[4] = trToCompose[4]*tr0 + trToCompose[5]*tr2 + tr4 |
| 147 |
tr[5] = trToCompose[5]*tr3 + trToCompose[4]*tr1 + tr5 |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
func (tr *Matrix) Scale(sx, sy float64) { |
| 152 |
tr[0] = sx * tr[0] |
| 153 |
tr[1] = sx * tr[1] |
| 154 |
tr[2] = sy * tr[2] |
| 155 |
tr[3] = sy * tr[3] |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
func (tr *Matrix) Translate(tx, ty float64) { |
| 160 |
tr[4] = tx*tr[0] + ty*tr[2] + tr[4] |
| 161 |
tr[5] = ty*tr[3] + tx*tr[1] + tr[5] |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
func (tr *Matrix) Rotate(radians float64) { |
| 166 |
c := math.Cos(radians) |
| 167 |
s := math.Sin(radians) |
| 168 |
t0 := c*tr[0] + s*tr[2] |
| 169 |
t1 := s*tr[3] + c*tr[1] |
| 170 |
t2 := c*tr[2] - s*tr[0] |
| 171 |
t3 := c*tr[3] - s*tr[1] |
| 172 |
tr[0] = t0 |
| 173 |
tr[1] = t1 |
| 174 |
tr[2] = t2 |
| 175 |
tr[3] = t3 |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
func (tr Matrix) GetTranslation() (x, y float64) { |
| 180 |
return tr[4], tr[5] |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
func (tr Matrix) GetScaling() (x, y float64) { |
| 185 |
return tr[0], tr[3] |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
func (tr Matrix) GetScale() float64 { |
| 190 |
x := 0.707106781*tr[0] + 0.707106781*tr[1] |
| 191 |
y := 0.707106781*tr[2] + 0.707106781*tr[3] |
| 192 |
return math.Sqrt(x*x + y*y) |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
func (tr Matrix) Equals(tr2 Matrix) bool { |
| 199 |
for i := 0; i < 6; i = i + 1 { |
| 200 |
if !fequals(tr[i], tr2[i]) { |
| 201 |
return false |
| 202 |
} |
| 203 |
} |
| 204 |
return true |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
func (tr Matrix) IsIdentity() bool { |
| 209 |
return fequals(tr[4], 0) && fequals(tr[5], 0) && tr.IsTranslation() |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
func (tr Matrix) IsTranslation() bool { |
| 214 |
return fequals(tr[0], 1) && fequals(tr[1], 0) && fequals(tr[2], 0) && fequals(tr[3], 1) |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
func fequals(float1, float2 float64) bool { |
| 219 |
return math.Abs(float1-float2) <= epsilon |
| 220 |
} |