vec2.monkey2 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. Namespace std.geom
  2. #rem monkeydoc Convenience type alias for Vec2\<Int\>.
  3. #end
  4. Alias Vec2i:Vec2<Int>
  5. #rem monkeydoc Convenience type alias for Vec2\<Float\>.
  6. #end
  7. Alias Vec2f:Vec2<Float>
  8. #rem monkeydoc The generic Vec2 type provides support for 2 component vectors.
  9. Unless otherwise noted, methods and operators always return a new vec2 containing the result, without modifying any parameters or 'self'.
  10. This allows you to chain operators together easily just like 'real' expressions.
  11. #end
  12. Struct Vec2<T>
  13. #rem monkeydoc Vector x coordinate.
  14. #end
  15. Field x:T
  16. #rem monkeydoc Vector y coodinate.
  17. #end
  18. Field y:T
  19. #rem monkeydoc Creates a new vector.
  20. #end
  21. Method New()
  22. End
  23. Method New( t:T )
  24. x=t;y=t
  25. End
  26. Method New( x:T,y:T )
  27. Self.x=x;Self.y=y
  28. End
  29. #rem monkeydoc Converts the vector to a vector of a different type or a printable string.
  30. #end
  31. Operator To<C>:Vec2<C>()
  32. Return New Vec2<C>( x,y )
  33. End
  34. Operator To:String()
  35. Return "Vec2("+x+","+y+")"
  36. End
  37. #rem monkeydoc The X coordinate of the vector.
  38. #end
  39. Property X:T()
  40. Return x
  41. Setter( x:T )
  42. Self.x=x
  43. End
  44. #rem monkeydoc The Y coordinate of the vector.
  45. #end
  46. Property Y:T()
  47. Return y
  48. Setter( y:T )
  49. Self.y=y
  50. End
  51. #rem monkeydoc Negates the vector.
  52. #end
  53. Operator-:Vec2()
  54. Return New Vec2( -x,-y )
  55. End
  56. #rem monkeydoc Multiplies the vector by another vector.
  57. #end
  58. Operator*:Vec2( v:Vec2 )
  59. Return New Vec2( x*v.x,y*v.y )
  60. End
  61. #rem monkeydoc Divides the vector by another vector.
  62. #end
  63. Operator/:Vec2( v:Vec2 )
  64. Return New Vec2( x/v.x,y/v.y )
  65. End
  66. #rem monkeydoc Adds another vector to the vector.
  67. #end
  68. Operator+:Vec2( v:Vec2 )
  69. Return New Vec2( x+v.x,y+v.y )
  70. End
  71. #rem monkeydoc Subtracts another vector from the vector.
  72. #end
  73. Operator-:Vec2( v:Vec2 )
  74. Return New Vec2( x-v.x,y-v.y )
  75. End
  76. #rem monkeydoc Multiplies the vector by a scalar.
  77. #end
  78. Operator*:Vec2( s:Double )
  79. Return New Vec2( x*s,y*s )
  80. End
  81. #rem monkeydoc Divides the vector by a scalar.
  82. #end
  83. Operator/:Vec2( s:Double )
  84. Return New Vec2( x/s,y/s )
  85. End
  86. #rem monkeydoc Adds a scalar to the vector.
  87. #end
  88. Operator+:Vec2( s:T )
  89. Return New Vec2( x+s,y+s )
  90. End
  91. #rem monkeydoc Subtracts a scalar from the vector.
  92. #end
  93. Operator-:Vec2( s:T )
  94. Return New Vec2( x-s,y-s )
  95. End
  96. #rem monkeydoc The length of the vector.
  97. #end
  98. Property Length:Double()
  99. Return Sqrt( x*x+y*y )
  100. End
  101. #rem monkeydoc The normal to the vector.
  102. #end
  103. Property Normal:Vec2()
  104. Return New Vec2( -y,x )
  105. End
  106. #rem monkeydoc Computes the distance from this vector to another.
  107. #end
  108. Method Distance:Double( v:Vec2 )
  109. Return (v-Self).Length
  110. End
  111. #rem monkeydoc Normalizes the vector.
  112. #end
  113. Method Normalize:Vec2()
  114. Return Self/Length
  115. End
  116. #rem monkeydoc Computes the dot product of the vector with another vector.
  117. #end
  118. Method Dot:T( v:Vec2 )
  119. Return x*v.x+y*v.y
  120. End
  121. #rem monkeydoc Blends the vector with another vector.
  122. #end
  123. Method Blend:Vec2( v:Vec2,alpha:Double )
  124. Return New Vec2( (v.x-x)*alpha+x,(v.y-y)*alpha+y )
  125. End
  126. End
  127. #rem monkeydoc Transforms a Vec2\<Int\> by an AffineMat3.
  128. #end
  129. Function TransformVec2i<T>:Vec2i( vec:Vec2i,matrix:AffineMat3<T> )
  130. Local tmp:=matrix * New Vec2<T>( rect.min.x,rect.min.y )
  131. Return New Vec2i( Round( tmp.x ),Round( tmp.y ) )
  132. End