affine.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package f32
  5. import "fmt"
  6. // An Affine is a 3x3 matrix of float32 values for which the bottom row is
  7. // implicitly always equal to [0 0 1].
  8. // Elements are indexed first by row then column, i.e. m[row][column].
  9. type Affine [2]Vec3
  10. func (m Affine) String() string {
  11. return fmt.Sprintf(`Affine[% 0.3f, % 0.3f, % 0.3f,
  12. % 0.3f, % 0.3f, % 0.3f]`,
  13. m[0][0], m[0][1], m[0][2],
  14. m[1][0], m[1][1], m[1][2])
  15. }
  16. // Identity sets m to be the identity transform.
  17. func (m *Affine) Identity() {
  18. *m = Affine{
  19. {1, 0, 0},
  20. {0, 1, 0},
  21. }
  22. }
  23. // Eq reports whether each component of m is within epsilon of the same
  24. // component in n.
  25. func (m *Affine) Eq(n *Affine, epsilon float32) bool {
  26. for i := range m {
  27. for j := range m[i] {
  28. diff := m[i][j] - n[i][j]
  29. if diff < -epsilon || +epsilon < diff {
  30. return false
  31. }
  32. }
  33. }
  34. return true
  35. }
  36. // Mul sets m to be p × q.
  37. func (m *Affine) Mul(p, q *Affine) {
  38. // Store the result in local variables, in case m == a || m == b.
  39. m00 := p[0][0]*q[0][0] + p[0][1]*q[1][0]
  40. m01 := p[0][0]*q[0][1] + p[0][1]*q[1][1]
  41. m02 := p[0][0]*q[0][2] + p[0][1]*q[1][2] + p[0][2]
  42. m10 := p[1][0]*q[0][0] + p[1][1]*q[1][0]
  43. m11 := p[1][0]*q[0][1] + p[1][1]*q[1][1]
  44. m12 := p[1][0]*q[0][2] + p[1][1]*q[1][2] + p[1][2]
  45. m[0][0] = m00
  46. m[0][1] = m01
  47. m[0][2] = m02
  48. m[1][0] = m10
  49. m[1][1] = m11
  50. m[1][2] = m12
  51. }
  52. // Inverse sets m to be the inverse of p.
  53. func (m *Affine) Inverse(p *Affine) {
  54. m00 := p[1][1]
  55. m01 := -p[0][1]
  56. m02 := p[1][2]*p[0][1] - p[1][1]*p[0][2]
  57. m10 := -p[1][0]
  58. m11 := p[0][0]
  59. m12 := p[1][0]*p[0][2] - p[1][2]*p[0][0]
  60. det := m00*m11 - m10*m01
  61. m[0][0] = m00 / det
  62. m[0][1] = m01 / det
  63. m[0][2] = m02 / det
  64. m[1][0] = m10 / det
  65. m[1][1] = m11 / det
  66. m[1][2] = m12 / det
  67. }
  68. // Scale sets m to be a scale followed by p.
  69. // It is equivalent to m.Mul(p, &Affine{{x,0,0}, {0,y,0}}).
  70. func (m *Affine) Scale(p *Affine, x, y float32) {
  71. m[0][0] = p[0][0] * x
  72. m[0][1] = p[0][1] * y
  73. m[0][2] = p[0][2]
  74. m[1][0] = p[1][0] * x
  75. m[1][1] = p[1][1] * y
  76. m[1][2] = p[1][2]
  77. }
  78. // Translate sets m to be a translation followed by p.
  79. // It is equivalent to m.Mul(p, &Affine{{1,0,x}, {0,1,y}}).
  80. func (m *Affine) Translate(p *Affine, x, y float32) {
  81. m[0][0] = p[0][0]
  82. m[0][1] = p[0][1]
  83. m[0][2] = p[0][0]*x + p[0][1]*y + p[0][2]
  84. m[1][0] = p[1][0]
  85. m[1][1] = p[1][1]
  86. m[1][2] = p[1][0]*x + p[1][1]*y + p[1][2]
  87. }
  88. // Rotate sets m to a rotation in radians followed by p.
  89. // It is equivalent to m.Mul(p, affineRotation).
  90. func (m *Affine) Rotate(p *Affine, radians float32) {
  91. s, c := Sin(radians), Cos(radians)
  92. m.Mul(p, &Affine{
  93. {+c, +s, 0},
  94. {-s, +c, 0},
  95. })
  96. }