vec4.monkey2 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. Namespace std.geom
  2. #rem monkeydoc Convenience type alias for Vec4\<Float\>.
  3. #end
  4. Alias Vec4f:Vec4<Float>
  5. #rem monkeydoc The generic Vec4 type provides support for 4 component vectors.
  6. Unless otherwise noted, methods and operators always return a new vec4 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 Vec4<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 monkeydoc The vector w coordinate.
  20. #end
  21. Field w:T
  22. #rem Creates a new vec4.
  23. #end
  24. Method New()
  25. End
  26. Method New( t:T )
  27. x=t;y=t;z=t;w=t
  28. End
  29. Method New( x:T,y:T,z:T,w:T )
  30. Self.x=x;Self.y=y;Self.z=z;Self.w=w
  31. End
  32. Method New( xy:Vec2<T>,z:T,w:T )
  33. Self.x=xy.x;Self.y=xy.y;Self.z=z;Self.w=w
  34. End
  35. Method New( x:T,yz:Vec2<T>,w:T )
  36. Self.x=x;Self.y=yz.x;Self.z=yz.y;Self.w=w
  37. End
  38. Method New( x:T,y:T,zw:Vec2<T> )
  39. Self.x=x;Self.y=y;Self.z=zw.x;Self.w=zw.y
  40. End
  41. Method New( xyz:Vec3<T>,w:T )
  42. Self.x=xyz.x;Self.y=xyz.y;Self.z=xyz.z;Self.w=w
  43. End
  44. Method new( x:T,yzw:Vec3<T> )
  45. Self.x=x;Self.y=yzw.x;self.z=yzw.y;Self.w=yzw.z
  46. End
  47. #Rem monkeydoc Converts the vec4 to a vec4 of a different type or a printable string.
  48. #end
  49. Operator To<C>:Vec4<C>()
  50. Return New Vec4<C>( x,y,z )
  51. End
  52. Operator To:String()
  53. Return "Vec4("+x+","+y+","+z+","+w+")"
  54. End
  55. #rem monkeydoc The X coordinate of the vec4.
  56. #end
  57. Property X:T()
  58. Return x
  59. Setter( x:T )
  60. Self.x=x
  61. End
  62. #rem monkeydoc The Y coordinate of the vec4.
  63. #end
  64. Property Y:T()
  65. Return y
  66. Setter( y:T )
  67. Self.y=y
  68. End
  69. #rem monkeydoc The Y coordinate of the vec4.
  70. #end
  71. Property Z:T()
  72. Return z
  73. Setter( z:T )
  74. Self.z=z
  75. End
  76. #rem monkeydoc The Y coordinate of the vec4.
  77. #end
  78. Property W:T()
  79. Return w
  80. Setter( w:T )
  81. Self.w=w
  82. End
  83. #rem monkeydoc The XY components as a vec2.
  84. #end
  85. Property XY:Vec2<T>()
  86. Return New Vec2<T>( x,y )
  87. Setter( xy:Vec2<T> )
  88. x=xy.x;y=xy.y
  89. End
  90. #rem monkeydoc The XYZ components as a vec3.
  91. #end
  92. Property XYZ:Vec3<T>()
  93. Return New Vec3<T>( x,y,z )
  94. Setter( xyz:Vec3<T> )
  95. x=xyz.x;y=xyz.y;z=xyz.z
  96. End
  97. #rem monkeydoc Negates the vec4.
  98. #end
  99. Operator-:Vec4()
  100. Return New Vec4( -x,-y,-z,-w )
  101. End
  102. #rem monkeydoc Multiplies the vec4 by another vec4.
  103. #end
  104. Operator*:Vec4( v:Vec4 )
  105. Return New Vec4( x*v.x,y*v.y,z*v.z,w*v.w )
  106. End
  107. #rem monkeydoc Divides the vec4 by another vec4.
  108. #end
  109. Operator/:Vec4( v:Vec4 )
  110. Return New Vec4( x/v.x,y/v.y,z/v.z,w/v.w )
  111. End
  112. #rem monkeydoc Adds the vec4 to another vec4.
  113. #end
  114. Operator+:Vec4( v:Vec4 )
  115. Return New Vec4( x+v.x,y+v.y,z+v.z,w+v.w )
  116. End
  117. #rem monkeydoc Subtracts another vec4 from the vec4.
  118. #end
  119. Operator-:Vec4( v:Vec4 )
  120. Return New Vec4( x-v.x,y-v.y,z-v.z,w-v.w )
  121. End
  122. #rem monkeydoc Multiplies the vec4 by a scalar.
  123. #end
  124. Operator*:Vec4( s:Double )
  125. Return New Vec4( x*s,y*s,z*s,w*s )
  126. End
  127. #rem monkeydoc Divides the vec4 by a scalar.
  128. #end
  129. Operator/:Vec4( s:Double )
  130. Return New Vec4( x/s,y/s,z/s,w/s )
  131. End
  132. #rem monkeydoc Adds a scalar to each component of the vec4.
  133. #end
  134. Operator+:Vec4( s:T )
  135. Return New Vec4( x+s,y+s,z+s,w+s )
  136. End
  137. #rem monkeydoc Subtracts a scalar from each component of the vec4.
  138. #end
  139. Operator-:Vec4( s:T )
  140. Return New Vec4( x-s,y-s,z-s,w-s )
  141. End
  142. #rem monkeydoc Returns the length of the vec4.
  143. #end
  144. Property Length:Double()
  145. Return Sqrt( x*x+y*y+z*z )
  146. End
  147. #rem monkeydoc Normalizes the vec4.
  148. #end
  149. Method Normalize:Vec4()
  150. Return Self/Length
  151. End
  152. #rem monkeydoc Returns the dot product of the vec4 with another vec4.
  153. #end
  154. Method Dot:Double( v:Vec4 )
  155. Return x*v.x+y*v.y+z*v.z+w*v.w
  156. End
  157. Method Blend:Vec4( v:Vec4,alpha:Double )
  158. Return New Vec4( (v.x-x)*alpha+x,(v.y-y)*alpha+y,(v.z-z)*alpha+z,(v.w-w)*alpha+w )
  159. End
  160. Method ToString:String()
  161. Return "Vec4("+x+","+y+","+z+","+w+")"
  162. End
  163. End