affinemat3.monkey2 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. Namespace std.geom
  2. #rem monkeydoc Convenience type alias for AffineMat3\<Float\>
  3. #end
  4. Alias AffineMat3f:AffineMat3<Float>
  5. #rem monkeydoc Affine 3x3 matrix class.
  6. An affine 3x3 matrix is a 3x3 matrix whose right hand column is always 0,0,1.
  7. Affine 3x3 matrices are often used for 2d transformations such as scaling, rotation and translation.
  8. #end
  9. Struct AffineMat3<T>
  10. #rem monkeydoc The first row of the matrix.
  11. #end
  12. Field i:Vec2<T>
  13. #rem monkeydoc The second row of the matrix.
  14. #end
  15. Field j:Vec2<T>
  16. #rem monkeydoc The third row of the matrix.
  17. #end
  18. Field t:Vec2<T>
  19. #rem monkeydoc Creates a new matrix.
  20. #end
  21. Method New()
  22. i.x=1;j.y=1
  23. End
  24. Method New( i:Vec2<T>,j:Vec2<T>,t:Vec2<T> )
  25. Self.i=i;Self.j=j;Self.t=t
  26. End
  27. Method New( ix:T,iy:T,jx:T,jy:T,tx:T,ty:T )
  28. Self.i.x=ix;Self.i.y=iy;Self.j.x=jx;Self.j.y=jy;Self.t.x=tx;Self.t.y=ty
  29. End
  30. #rem monkeydoc Returns the inverse of the matrix.
  31. #end
  32. Operator-:AffineMat3()
  33. Local idet:=1.0/(i.x*j.y-i.y*j.x)
  34. Return New AffineMat3(
  35. j.y*idet , -i.y*idet,
  36. -j.x*idet , i.x*idet,
  37. (j.x*t.y-j.y*t.x)*idet , (i.y*t.x-i.x*t.y)*idet )
  38. End
  39. #rem monkeydoc Multiplies a vector by the matrix and returns the result.
  40. #end
  41. Operator*:Vec2<T>( v:Vec2<T> )
  42. Return New Vec2<T>( i.x*v.x + j.x*v.y + t.x , i.y*v.x + j.y*v.y + t.y )
  43. End
  44. #rem monkeydoc Multiplies the matrix by another matrix and returns the result.
  45. #end
  46. Operator*:AffineMat3( m:AffineMat3 )
  47. Return New AffineMat3(
  48. i.x*m.i.x + j.x*m.i.y , i.y*m.i.x + j.y*m.i.y ,
  49. i.x*m.j.x + j.x*m.j.y , i.y*m.j.x + j.y*m.j.y ,
  50. i.x*m.t.x + j.x*m.t.y + t.x , i.y*m.t.x + j.y*m.t.y + t.y )
  51. End
  52. #rem monkeydoc Multiplies a vector by the matrix and returns the result.
  53. #end
  54. Method Transform:Vec2<T>( x:T,y:T )
  55. Return New Vec2<T>( i.x*x + j.x*y + t.x , i.y*x + j.y*y + t.y )
  56. End
  57. #rem monkeydoc Multiplies the matrix by another matrix and returns the result.
  58. #end
  59. Method Transform:AffineMat3( ix:Float,iy:Float,jx:Float,jy:Float,tx:Float,ty:Float )
  60. Return New AffineMat3(
  61. i.x*ix + j.x*iy , i.y*ix + j.y*iy ,
  62. i.x*jx + j.x*jy , i.y*jx + j.y*jy ,
  63. i.x*tx + j.x*ty + t.x , i.y*tx + j.y*ty + t.y )
  64. End
  65. #rem monkeydoc Applies a translation transformation to the matrix and returns the result.
  66. #end
  67. Method Translate:AffineMat3( v:Vec2<T> )
  68. Return Transform( 1,0,0,1,v.x,v.y )
  69. End
  70. Method Translate:AffineMat3( tx:T,ty:T )
  71. Return Transform( 1,0,0,1,tx,ty )
  72. End
  73. #rem monkeydoc Applies a rotation transformation to the matrix and returns the result.
  74. #end
  75. Method Rotate:AffineMat3( rz:Double )
  76. Return Transform( Cos( rz ),-Sin( rz ),Sin( rz ),Cos( rz ),0,0 )
  77. End
  78. #rem monkeydoc Applies a scale transformation to the matrix and returns the result.
  79. #end
  80. Method Scale:AffineMat3( v:Vec2<T> )
  81. Return Transform( v.x,0,0,v.y,0,0 )
  82. End
  83. Method Scale:AffineMat3( sx:T,sy:T )
  84. Return Transform( sx,0,0,sy,0,0 )
  85. End
  86. #rem monkeydoc @hidden
  87. #end
  88. Function Ortho:AffineMat3( left:T,right:T,bottom:T,top:T )
  89. Local w:=right-left,h:=top-bottom
  90. Local r:AffineMat3
  91. r.i.x=2/w
  92. r.j.y=2/h
  93. r.t.x=-(right+left)/w
  94. r.t.y=-(top+bottom)/h
  95. Return r
  96. End
  97. End