vector.monkey2 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #Import "<mojo>"
  2. #Import "<std>"
  3. Using mojo..
  4. Using std..
  5. Class PVector2D
  6. Field x:Float
  7. Field y:Float
  8. Method New()
  9. End method
  10. Method New(x:Float,y:Float)
  11. Set(x,y)
  12. End Method
  13. Method New(v:PVector2D)
  14. Set(v)
  15. End Method
  16. Method Set:PVector2D(v:PVector2D)
  17. x = v.x
  18. y = v.y
  19. Return Self
  20. End Method
  21. Method Set:PVector2D(x:Float,y:Float)
  22. Self.x = x
  23. Self.y = y
  24. Return Self
  25. End Method
  26. Method Add:PVector2D(x:Float,y:Float)
  27. Self.x += x
  28. Self.y += y
  29. Return Self
  30. End Method
  31. Method Add:PVector2D(v:PVector2D)
  32. Self.x += v.x
  33. Self.y += v.y
  34. Return Self
  35. End Method
  36. Method Add:PVector2D(value:Float)
  37. Self.x += value
  38. Self.y += value
  39. Return Self
  40. End Method
  41. Method Subtract:PVector2D(x:Float,y:Float)
  42. Self.x -= x
  43. Self.y -= y
  44. Return Self
  45. End Method
  46. Method Subtract:PVector2D(v:PVector2D)
  47. Self.x -= v.x
  48. Self.y -= v.y
  49. Return Self
  50. End Method
  51. Method Subtract:PVector2D(value:Float)
  52. Self.x -= value
  53. Self.y -= value
  54. Return Self
  55. End Method
  56. Method Multiply:PVector2D(x:Float,y:Float)
  57. Self.x *= x
  58. Self.y *= y
  59. Return Self
  60. End Method
  61. Method Multiply:PVector2D(v:PVector2D)
  62. Self.x *= v.x
  63. Self.y *= v.y
  64. Return Self
  65. End Method
  66. Method Multiply:PVector2D(value:Float)
  67. Self.x *= value
  68. Self.y *= value
  69. Return Self
  70. End Method
  71. Method Divide:PVector2D(x:Float,y:Float)
  72. Self.x /= x
  73. Self.y /= y
  74. Return Self
  75. End Method
  76. Method Divide:PVector2D(v:PVector2D)
  77. Self.x /= v.x
  78. Self.y /= v.y
  79. Return Self
  80. End Method
  81. Method Divide:PVector2D(value:Float)
  82. Self.x /= value
  83. Self.y /= value
  84. Return Self
  85. End Method
  86. Method DotProduct:Float(x:Float,y:Float)
  87. Return Self.x * x + Self.y * y
  88. End Method
  89. Method DotProduct:Float(v:PVector2D)
  90. Return Self.x * v.x + Self.y * v.y
  91. End Method
  92. Method PerpDotProduct:Float(x:Float,y:Float)
  93. Return Self.x * y - Self.y * x
  94. End Method
  95. Method PerpDotProduct:Float(v:PVector2D)
  96. Return Self.x * v.y - Self.y * v.x
  97. End Method
  98. Method MagnitudeSquare:Float()
  99. Return Self.x * Self.x + Self.y * Self.y
  100. End Method
  101. Method Magnitude:Float()
  102. Return Sqrt(MagnitudeSquare())
  103. End Method
  104. Method LeftNormal:PVector2D()
  105. Local n:Float = y
  106. y = -x
  107. x = n
  108. Return Self
  109. End Method
  110. Method RightNormal:PVector2D()
  111. Local n:Float = y
  112. y = x
  113. x = -n
  114. Return Self
  115. End Method
  116. Method Normalize:PVector2D()
  117. Divide(Magnitude())
  118. Return Self
  119. End Method
  120. Method GetAngle:Float()
  121. Return ATan2(y,x)
  122. End Method
  123. Method Rotate:PVector2D(angle:Float)
  124. Local c:Float = Cos(angle)
  125. Local s:Float = Sin(angle)
  126. Local t:Float = x * c - y * s
  127. y = y * c + x * s
  128. x = t
  129. Return Self
  130. End Method
  131. Method ToString:String()
  132. Return x+", "+y
  133. End Method
  134. Method Draw:Void(canvas:Canvas,x:Float=0,y:Float=0)
  135. canvas.DrawLine(x,y,x+Self.x,y+Self.y)
  136. End Method
  137. End Class
  138. Function Projection:PVector2D(this:PVector2D,into:PVector2D,destination:PVector2D=Null)
  139. If destination = Null destination = New PVector2D
  140. Return destination.Set(into).Normalize().Multiply(destination.DotProduct(this))
  141. End Function
  142. Function CrossProjection:PVector2D(this:PVector2D,into:PVector2D,destination:PVector2D=Null)
  143. If destination = Null destination = New PVector2D
  144. Return destination.Set(into.y,-into.x).Normalize().Multiply(destination.DotProduct(this))
  145. End Function