vec3.monkey2 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. Namespace std.geom
  2. #rem monkeydoc Convenience type alias for Vec3\<Float\>.
  3. #end
  4. Alias Vec3f:Vec3<Float>
  5. #rem monkeydoc The generic Vec3 type provides support for 3 component vectors.
  6. Unless otherwise noted, methods and operators always return a new vec3 containing the result, without modifying any parameters or 'self'.
  7. This allows you to chain operators together easily just like 'real' expressions.
  8. #end
  9. Struct Vec3<T>
  10. #rem monkeydoc The vector x coordinate.
  11. #end
  12. Field x:T
  13. #rem monkeydoc The vector y coordinate.
  14. #end
  15. Field y:T
  16. #rem monkeydoc The vector z coordinate.
  17. #end
  18. Field z:T
  19. #rem Creates a new vec3.
  20. #end
  21. Method New()
  22. End
  23. Method New( t:T )
  24. x=t;y=t;z=t
  25. End
  26. Method New( x:T,y:T,z:T )
  27. Self.x=x;Self.y=y;Self.z=z
  28. End
  29. Method New( v:Vec2<T>,z:T )
  30. Self.x=v.x;Self.y=v.y;Self.z=z
  31. End
  32. Method New( x:T,v:Vec2<T> )
  33. Self.x=z;Self.y=v.x;Self.z=v.y
  34. End
  35. #Rem monkeydoc Converts the vec3 to a vec3 of a different type or a printable string.
  36. #end
  37. Method To<C>:Vec3<C>()
  38. Return New Vec3<C>( x,y,z )
  39. End
  40. Method To:String()
  41. Return "Vec3("+x+","+y+","+z+")"
  42. End
  43. #rem monkeydoc The X coordinate of the vec3.
  44. #end
  45. Property X:T()
  46. Return x
  47. Setter( x:T )
  48. Self.x=x
  49. End
  50. #rem monkeydoc The Y coordinate of the vec3.
  51. #end
  52. Property Y:T()
  53. Return y
  54. Setter( y:T )
  55. Self.y=y
  56. End
  57. #rem monkeydoc The X coordinate of the vec3.
  58. #end
  59. Property Z:T()
  60. Return z
  61. Setter( z:T )
  62. Self.z=z
  63. End
  64. #rem monkeydoc The XY components of the vec3 as a vec2.
  65. #end
  66. Property XY:Vec2<T>()
  67. Return New Vec2<T>( x,y )
  68. Setter( xy:Vec2<T> )
  69. x=xy.x;y=xy.y
  70. End
  71. #rem monkeydoc The YZ components of the vec3 as a vec2.
  72. #end
  73. Property YZ:Vec2<T>()
  74. Return New Vec2<T>( y,z )
  75. Setter( yz:Vec2<T> )
  76. y=yz.x;z=yz.y
  77. End
  78. #rem monkeydoc The XZ components of the vec3 as a vec2.
  79. #end
  80. Property XZ:Vec2<T>()
  81. Return New Vec2<T>( x,z )
  82. Setter( xz:Vec2<T> )
  83. x=xz.x;z=xz.y
  84. End
  85. #rem monkeydoc Negates the vec3.
  86. #end
  87. Operator-:Vec3()
  88. Return New Vec3( -x,-y,-z )
  89. End
  90. #rem monkeydoc Multiplies the vec3 by another vec3.
  91. #end
  92. Operator*:Vec3( v:Vec3 )
  93. Return New Vec3( x*v.x,y*v.y,z*v.z )
  94. End
  95. #rem monkeydoc Divides the vec3 by another vec3.
  96. #end
  97. Operator/:Vec3( v:Vec3 )
  98. Return New Vec3( x/v.x,y/v.y,z/v.z )
  99. End
  100. #rem monkeydoc Adds the vec3 to another vec3.
  101. #end
  102. Operator+:Vec3( v:Vec3 )
  103. Return New Vec3( x+v.x,y+v.y,z+v.z )
  104. End
  105. #rem monkeydoc Subtracts another vec3 from the vec3.
  106. #end
  107. Operator-:Vec3( v:Vec3 )
  108. Return New Vec3( x-v.x,y-v.y,z-v.z )
  109. End
  110. #rem monkeydoc Multiplies the vec3 by a scalar.
  111. #end
  112. Operator*:Vec3( s:Double )
  113. Return New Vec3( x*s,y*s,z*s )
  114. End
  115. #rem monkeydoc Divides the vec3 by a scalar.
  116. #end
  117. Operator/:Vec3( s:Double )
  118. Return New Vec3( x/s,y/s,z/s )
  119. End
  120. #rem monkeydoc Adds a scalar to each component of the vec3.
  121. #end
  122. Operator+:Vec3( s:T )
  123. Return New Vec3( x+s,y+s,z+s )
  124. End
  125. #rem monkeydoc Subtracts a scalar from each component of the vec3.
  126. #end
  127. Operator-:Vec3( s:T )
  128. Return New Vec3( x-s,y-s,z-s )
  129. End
  130. #rem monkeydoc Returns the pitch of the vec3.
  131. Pitch is the angle of rotation, in radians, of the vec3 around the x axis.
  132. #end
  133. Property Pitch:Double()
  134. return -ATan2( y,Sqrt( x*x+z*z ) )
  135. End
  136. #rem monkeydoc Returns the yaw of the vec3.
  137. Yaw is the angle of rotation, in radians, of the vec3 around the y axis.
  138. #end
  139. Property Yaw:Double()
  140. return -ATan2( x,z )
  141. End
  142. #rem monkeydoc Returns the length of the vec3.
  143. #end
  144. Property Length:Double()
  145. Return Sqrt( x*x+y*y+z*z )
  146. End
  147. #rem monkeydoc Returns the distance of the vec to another vec3.
  148. #end
  149. Method Distance:Double( v:Vec3 )
  150. Return (v-Self).Length
  151. End
  152. #rem monkeydoc Normalizes the vec3.
  153. #end
  154. Method Normalize:Vec3()
  155. Return Self/Length
  156. End
  157. #rem monkeydoc Returns the dot product of the vec3 with another vec3.
  158. #end
  159. Method Dot:T( v:Vec3 )
  160. Return x*v.x+y*v.y+z*v.z
  161. End
  162. #rem monkeydoc Returns the cross product of the vec3 with another vec3.
  163. #end
  164. Method Cross:Vec3( v:Vec3 )
  165. Return New Vec3( y*v.z-z*v.y,z*v.x-x*v.z,x*v.y-y*v.x )
  166. End
  167. #rem monkeydoc Blends the vec3 with another vec3.
  168. Components are linearly blended using `alpha` as a weighting factor.
  169. If alpha is 0, self is returned.
  170. If alpha is 1, `v` is returned.
  171. #end
  172. Method Blend:Vec3( v:Vec3,alpha:Double )
  173. Return New Vec3( (v.x-x)*alpha+x,(v.y-y)*alpha+y,(v.z-z)*alpha+z )
  174. End
  175. End