vec2.monkey2 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 Vec2 type provides support for 2 component vectors.
  9. #end
  10. Struct Vec2<T>
  11. #rem monkeydoc Vector x coordinate.
  12. #end
  13. Field x:T
  14. #rem monkeydoc Vector y coodinate.
  15. #end
  16. Field y:T
  17. #rem monkeydoc Creates a new vector.
  18. #end
  19. Method New()
  20. End
  21. ' Method New( v:Vec2 )
  22. ' x=v.x;y=v.y
  23. ' End
  24. Method New( x:T,y:T )
  25. Self.x=x
  26. Self.y=y
  27. End
  28. #rem monkeydoc The X coordinate of the vector.
  29. #end
  30. Property X:T()
  31. Return x
  32. Setter( x:T )
  33. Self.x=x
  34. End
  35. #rem monkeydoc The Y coordinate of the vector.
  36. #end
  37. Property Y:T()
  38. Return y
  39. Setter( y:T )
  40. Self.y=y
  41. End
  42. #rem monkeydoc Negates the vector components and returns the result.
  43. #end
  44. Operator-:Vec2()
  45. Return New Vec2( -x,-y )
  46. End
  47. #rem monkeydoc Multiplies the vector by another vector and returns the result.
  48. #end
  49. Operator*:Vec2( v:Vec2 )
  50. Return New Vec2( x*v.x,y*v.y )
  51. End
  52. #rem monkeydoc Divides the vector by another vector and returns the result.
  53. #end
  54. Operator/:Vec2( v:Vec2 )
  55. Return New Vec2( x/v.x,y/v.y )
  56. End
  57. #rem monkeydoc Adds another vector to the vector and returns the result.
  58. #end
  59. Operator+:Vec2( v:Vec2 )
  60. Return New Vec2( x+v.x,y+v.y )
  61. End
  62. #rem monkeydoc Subtracts another vector from the vector and returns the result.
  63. #end
  64. Operator-:Vec2( v:Vec2 )
  65. Return New Vec2( x-v.x,y-v.y )
  66. End
  67. #rem monkeydoc Scales the vector by a value and returns the result.
  68. #end
  69. Operator*:Vec2( s:Double )
  70. Return New Vec2( x*s,y*s )
  71. End
  72. #rem monkeydoc Inverse scales the vector by a value and returns the result.
  73. #end
  74. Operator/:Vec2( s:Double )
  75. Return New Vec2( x/s,y/s )
  76. End
  77. #rem monkeydoc Adds a value to the vector components and returns the result.
  78. #end
  79. Operator+:Vec2( s:T )
  80. Return New Vec2( x+s,y+s )
  81. End
  82. #rem monkeydoc Subtracts a value from the vector components and returns the result.
  83. #end
  84. Operator-:Vec2( s:T )
  85. Return New Vec2( x-s,y-s )
  86. End
  87. #rem monkeydoc The length of the vector.
  88. #end
  89. Property Length:Double()
  90. Return Sqrt( x*x+y*y )
  91. End
  92. #rem monkeydoc Computes the dot product of the vector with another vector.
  93. #end
  94. Method Dot:Double( v:Vec2 )
  95. Return x*v.x+y*v.y
  96. End
  97. #rem monkeydoc Normalizes the vector and returns the result.
  98. #end
  99. Method Normalize:Vec2()
  100. Return Self/Length
  101. End
  102. #rem monkeydoc Blends the vector with another vector and returns the result.
  103. #end
  104. Method Blend:Vec2( v:Vec2,alpha:Double )
  105. Return New Vec2( (v.x-x)*alpha+x,(v.y-y)*alpha+y )
  106. End
  107. #rem monkeydoc Gets a string representation for the vector.
  108. #end
  109. Method ToString:String()
  110. Return "Vec2("+x+","+y+")"
  111. End
  112. End
  113. #rem monkeydoc Transforms a Vec2\<Int\> by an AffineMat3.
  114. #end
  115. Function TransformVec2i<T>:Vec2i( vec:Vec2i,matrix:AffineMat3<T> )
  116. Local tmp:=matrix * New Vec2<T>( rect.min.x,rect.min.y )
  117. Return New Vec2i( Round( tmp.x ),Round( tmp.y ) )
  118. End