affinemat3.monkey2 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 Converts the matrix to a matrix of a different type.
  31. #end
  32. Operator To<C>:AffineMat3<C>()
  33. Return New AffineMat3<C>( i.x,i.y,j.x,j.y,t.x,t.y )
  34. End
  35. #rem monkeydoc Converts the matrix to a printable string.
  36. #end
  37. Operator To:String()
  38. Return "AffineMat3("+i.x+","+i.y+","+j.x+","+j.y+","+t.x+","+t.y+")"
  39. End
  40. #rem monkeydoc Returns the inverse of the matrix.
  41. #end
  42. Operator-:AffineMat3()
  43. Local idet:=1.0/(i.x*j.y-i.y*j.x)
  44. Return New AffineMat3(
  45. j.y*idet , -i.y*idet,
  46. -j.x*idet , i.x*idet,
  47. (j.x*t.y-j.y*t.x)*idet , (i.y*t.x-i.x*t.y)*idet )
  48. End
  49. #rem monkeydoc Multiplies a vector by the matrix and returns the result.
  50. #end
  51. Operator*:Vec2<T>( v:Vec2<T> )
  52. Return New Vec2<T>( i.x*v.x + j.x*v.y + t.x , i.y*v.x + j.y*v.y + t.y )
  53. End
  54. #rem monkeydoc Multiplies the matrix by another matrix and returns the result.
  55. #end
  56. Operator*:AffineMat3( m:AffineMat3 )
  57. Return New AffineMat3(
  58. i.x*m.i.x + j.x*m.i.y , i.y*m.i.x + j.y*m.i.y ,
  59. i.x*m.j.x + j.x*m.j.y , i.y*m.j.x + j.y*m.j.y ,
  60. i.x*m.t.x + j.x*m.t.y + t.x , i.y*m.t.x + j.y*m.t.y + t.y )
  61. End
  62. #rem monkeydoc Multiplies a vector by the matrix and returns the result.
  63. #end
  64. Method Transform:Vec2<T>( v:Vec2<T> )
  65. Return New Vec2<T>( i.x*v.x + j.x*v.y + t.x , i.y*v.x + j.y*v.y + t.y )
  66. End
  67. #rem monkeydoc Multiplies a vector by the matrix and returns the result.
  68. #end
  69. Method Transform:Vec2<T>( x:T,y:T )
  70. Return New Vec2<T>( i.x*x + j.x*y + t.x , i.y*x + j.y*y + t.y )
  71. End
  72. #rem monkeydoc Multiplies the matrix by another matrix and returns the result.
  73. #end
  74. Method Transform:AffineMat3( ix:Float,iy:Float,jx:Float,jy:Float,tx:Float,ty:Float )
  75. Return New AffineMat3(
  76. i.x*ix + j.x*iy , i.y*ix + j.y*iy ,
  77. i.x*jx + j.x*jy , i.y*jx + j.y*jy ,
  78. i.x*tx + j.x*ty + t.x , i.y*tx + j.y*ty + t.y )
  79. End
  80. #rem monkeydoc Applies a translation transformation to the matrix and returns the result.
  81. #end
  82. Method Translate:AffineMat3( tx:T,ty:T )
  83. Return Transform( 1,0,0,1,tx,ty )
  84. End
  85. Method Translate:AffineMat3( tv:Vec2<T> )
  86. Return Transform( 1,0,0,1,tv.x,tv.y )
  87. End
  88. #rem monkeydoc Applies a rotation transformation to the matrix and returns the result.
  89. #end
  90. Method Rotate:AffineMat3( rz:Double )
  91. Return Transform( Cos( rz ),-Sin( rz ),Sin( rz ),Cos( rz ),0,0 )
  92. End
  93. #rem monkeydoc Applies a scale transformation to the matrix and returns the result.
  94. #end
  95. Method Scale:AffineMat3( sx:T,sy:T )
  96. Return Transform( sx,0,0,sy,0,0 )
  97. End
  98. Method Scale:AffineMat3( sv:Vec2<T> )
  99. Return Transform( sv.x,0,0,sv.y,0,0 )
  100. End
  101. Function Translation:AffineMat3( tx:T,ty:T )
  102. Return New AffineMat3( 1,0,0,1,tx,ty )
  103. End
  104. Function Translation:AffineMat3( tv:Vec2<T> )
  105. Return New AffineMat3( 1,0,0,1,tv.x,tv.y )
  106. End
  107. Function Rotation:AffineMat3( rz:Double )
  108. Return New AffineMat3( Cos( rz ),-Sin( rz ),Sin( rz ),Cos( rz ),0,0 )
  109. End
  110. Function Scaling:AffineMat3( sx:T,sy:T )
  111. Return New AffineMat3( sx,0,0,sy,0,0 )
  112. End
  113. Function Scaling:AffineMat3( sv:Vec2<T> )
  114. Return New AffineMat3( sv.x,0,0,sv.y,0,0 )
  115. End
  116. Function Ortho:AffineMat3( left:T,right:T,bottom:T,top:T )
  117. Local w:=right-left,h:=top-bottom
  118. Local r:AffineMat3
  119. r.i.x=2/w
  120. r.j.y=2/h
  121. r.t.x=-(right+left)/w
  122. r.t.y=-(top+bottom)/h
  123. Return r
  124. End
  125. End