Vector2.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. <h1>[name]</h1>
  11. <p class="desc">
  12. Class representing a 2D [link:https://en.wikipedia.org/wiki/Vector_space vector].
  13. A 2D vector is an ordered pair of numbers (labeled x and y),
  14. which can be used to represent a number of things, such as:
  15. </p>
  16. <ul>
  17. <li>A point in 2D space (i.e. a position on a plane).</li>
  18. <li>
  19. A direction and length across a plane. In three.js the length will
  20. always be the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean distance]
  21. (straight-line distance) from `(0, 0)` to `(x, y)`
  22. and the direction is also measured from `(0, 0)` towards `(x, y)`.
  23. </li>
  24. <li>Any arbitrary ordered pair of numbers.</li>
  25. </ul>
  26. <p>
  27. There are other things a 2D vector can be used to represent, such as
  28. momentum vectors, complex numbers and so on, however these are the most
  29. common uses in three.js.
  30. </p>
  31. <p>
  32. Iterating through a [name] instance will yield its components `(x, y)` in
  33. the corresponding order.
  34. </p>
  35. <h2>Code Example</h2>
  36. <code>
  37. const a = new THREE.Vector2( 0, 1 );
  38. //no arguments; will be initialised to (0, 0)
  39. const b = new THREE.Vector2( );
  40. const d = a.distanceTo( b );
  41. </code>
  42. <h2>Constructor</h2>
  43. <h3>[name]( [param:Float x], [param:Float y] )</h3>
  44. <p>
  45. [page:Float x] - the x value of this vector. Default is `0`.<br />
  46. [page:Float y] - the y value of this vector. Default is `0`.<br /><br />
  47. Creates a new [name].
  48. </p>
  49. <h2>Properties</h2>
  50. <h3>[property:Float height]</h3>
  51. <p>Alias for [page:.y y].</p>
  52. <h3>[property:Boolean isVector2]</h3>
  53. <p>Read-only flag to check if a given object is of type [name].</p>
  54. <h3>[property:Float width]</h3>
  55. <p>Alias for [page:.x x].</p>
  56. <h3>[property:Float x]</h3>
  57. <h3>[property:Float y]</h3>
  58. <h2>Methods</h2>
  59. <h3>[method:this add]( [param:Vector2 v] )</h3>
  60. <p>Adds [page:Vector2 v] to this vector.</p>
  61. <h3>[method:this addScalar]( [param:Float s] )</h3>
  62. <p>
  63. Adds the scalar value [page:Float s] to this vector's [page:.x x] and
  64. [page:.y y] values.
  65. </p>
  66. <h3>[method:this addScaledVector]( [param:Vector2 v], [param:Float s] )</h3>
  67. <p>
  68. Adds the multiple of [page:Vector2 v] and [page:Float s] to this vector.
  69. </p>
  70. <h3>[method:this addVectors]( [param:Vector2 a], [param:Vector2 b] )</h3>
  71. <p>Sets this vector to [page:Vector2 a] + [page:Vector2 b].</p>
  72. <h3>[method:Float angle]()</h3>
  73. <p>
  74. Computes the angle in radians of this vector with respect to the positive
  75. x-axis.
  76. </p>
  77. <h3>[method:Float angleTo]( [param:Vector2 v] )</h3>
  78. <p>
  79. Returns the angle between this vector and vector [page:Vector2 v] in
  80. radians.
  81. </p>
  82. <h3>[method:this applyMatrix3]( [param:Matrix3 m] )</h3>
  83. <p>
  84. Multiplies this vector (with an implicit 1 as the 3rd component) by m.
  85. </p>
  86. <h3>[method:this ceil]()</h3>
  87. <p>
  88. The [page:.x x] and [page:.y y] components of this vector are rounded up
  89. to the nearest integer value.
  90. </p>
  91. <h3>[method:this clamp]( [param:Vector2 min], [param:Vector2 max] )</h3>
  92. <p>
  93. [page:Vector2 min] - the minimum x and y values.<br />
  94. [page:Vector2 max] - the maximum x and y values in the desired range<br /><br />
  95. If this vector's x or y value is greater than the max vector's x or y
  96. value, it is replaced by the corresponding value. <br /><br />
  97. If this vector's x or y value is less than the min vector's x or y value,
  98. it is replaced by the corresponding value.
  99. </p>
  100. <h3>[method:this clampLength]( [param:Float min], [param:Float max] )</h3>
  101. <p>
  102. [page:Float min] - the minimum value the length will be clamped to <br />
  103. [page:Float max] - the maximum value the length will be clamped to<br /><br />
  104. If this vector's length is greater than the max value, it is replaced by
  105. the max value. <br /><br />
  106. If this vector's length is less than the min value, it is replaced by the
  107. min value.
  108. </p>
  109. <h3>[method:this clampScalar]( [param:Float min], [param:Float max] )</h3>
  110. <p>
  111. [page:Float min] - the minimum value the components will be clamped to
  112. <br />
  113. [page:Float max] - the maximum value the components will be clamped to<br /><br />
  114. If this vector's x or y values are greater than the max value, they are
  115. replaced by the max value. <br /><br />
  116. If this vector's x or y values are less than the min value, they are
  117. replaced by the min value.
  118. </p>
  119. <h3>[method:Vector2 clone]()</h3>
  120. <p>
  121. Returns a new Vector2 with the same [page:.x x] and [page:.y y] values as
  122. this one.
  123. </p>
  124. <h3>[method:this copy]( [param:Vector2 v] )</h3>
  125. <p>
  126. Copies the values of the passed Vector2's [page:.x x] and [page:.y y]
  127. properties to this Vector2.
  128. </p>
  129. <h3>[method:Float distanceTo]( [param:Vector2 v] )</h3>
  130. <p>Computes the distance from this vector to [page:Vector2 v].</p>
  131. <h3>[method:Float manhattanDistanceTo]( [param:Vector2 v] )</h3>
  132. <p>
  133. Computes the [link:https://en.wikipedia.org/wiki/Taxicab_geometry Manhattan distance]
  134. from this vector to [page:Vector2 v].
  135. </p>
  136. <h3>[method:Float distanceToSquared]( [param:Vector2 v] )</h3>
  137. <p>
  138. Computes the squared distance from this vector to [page:Vector2 v]. If you
  139. are just comparing the distance with another distance, you should compare
  140. the distance squared instead as it is slightly more efficient to
  141. calculate.
  142. </p>
  143. <h3>[method:this divide]( [param:Vector2 v] )</h3>
  144. <p>Divides this vector by [page:Vector2 v].</p>
  145. <h3>[method:this divideScalar]( [param:Float s] )</h3>
  146. <p>Divides this vector by scalar [page:Float s].</p>
  147. <h3>[method:Float dot]( [param:Vector2 v] )</h3>
  148. <p>
  149. Calculates the [link:https://en.wikipedia.org/wiki/Dot_product dot product]
  150. of this vector and [page:Vector2 v].
  151. </p>
  152. <h3>[method:Float cross]( [param:Vector2 v] )</h3>
  153. <p>
  154. Calculates the [link:https://en.wikipedia.org/wiki/Cross_product cross product]
  155. of this vector and [page:Vector2 v]. Note that a 'cross-product'
  156. in 2D is not well-defined. This function computes a geometric
  157. cross-product often used in 2D graphics
  158. </p>
  159. <h3>[method:Boolean equals]( [param:Vector2 v] )</h3>
  160. <p>
  161. Returns `true` if the components of this vector and [page:Vector2 v] are
  162. strictly equal; `false` otherwise.
  163. </p>
  164. <h3>[method:this floor]()</h3>
  165. <p>
  166. The components of this vector are rounded down to the nearest integer
  167. value.
  168. </p>
  169. <h3>
  170. [method:this fromArray]( [param:Array array], [param:Integer offset] )
  171. </h3>
  172. <p>
  173. [page:Array array] - the source array.<br />
  174. [page:Integer offset] - (optional) offset into the array. Default is `0`.<br /><br />
  175. Sets this vector's [page:.x x] value to be `array[ offset ]` and [page:.y y]
  176. value to be `array[ offset + 1 ]`.
  177. </p>
  178. <h3>
  179. [method:this fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )
  180. </h3>
  181. <p>
  182. [page:BufferAttribute attribute] - the source attribute.<br />
  183. [page:Integer index] - index in the attribute.<br /><br />
  184. Sets this vector's [page:.x x] and [page:.y y] values from the
  185. [page:BufferAttribute attribute].
  186. </p>
  187. <h3>[method:Float getComponent]( [param:Integer index] )</h3>
  188. <p>
  189. [page:Integer index] - `0` or `1`.<br /><br />
  190. If index equals `0` returns the [page:.x x] value. <br />
  191. If index equals `1` returns the [page:.y y] value.
  192. </p>
  193. <h3>[method:Float length]()</h3>
  194. <p>
  195. Computes the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  196. (straight-line length) from (0, 0) to (x, y).
  197. </p>
  198. <h3>[method:Float manhattanLength]()</h3>
  199. <p>
  200. Computes the [link:http://en.wikipedia.org/wiki/Taxicab_geometry Manhattan length] of this vector.
  201. </p>
  202. <h3>[method:Float lengthSq]()</h3>
  203. <p>
  204. Computes the square of the
  205. [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  206. (straight-line length) from (0, 0) to (x, y). If you are comparing the
  207. lengths of vectors, you should compare the length squared instead as it is
  208. slightly more efficient to calculate.
  209. </p>
  210. <h3>[method:this lerp]( [param:Vector2 v], [param:Float alpha] )</h3>
  211. <p>
  212. [page:Vector2 v] - [page:Vector2] to interpolate towards.<br />
  213. [page:Float alpha] - interpolation factor, typically in the closed
  214. interval `[0, 1]`.<br /><br />
  215. Linearly interpolates between this vector and [page:Vector2 v], where
  216. alpha is the percent distance along the line - alpha = 0 will be this
  217. vector, and alpha = 1 will be [page:Vector2 v].
  218. </p>
  219. <h3>
  220. [method:this lerpVectors]( [param:Vector2 v1], [param:Vector2 v2], [param:Float alpha] )
  221. </h3>
  222. <p>
  223. [page:Vector2 v1] - the starting [page:Vector2].<br />
  224. [page:Vector2 v2] - [page:Vector2] to interpolate towards.<br />
  225. [page:Float alpha] - interpolation factor, typically in the closed
  226. interval `[0, 1]`.<br /><br />
  227. Sets this vector to be the vector linearly interpolated between
  228. [page:Vector2 v1] and [page:Vector2 v2] where alpha is the percent
  229. distance along the line connecting the two vectors - alpha = 0 will be
  230. [page:Vector2 v1], and alpha = 1 will be [page:Vector2 v2].
  231. </p>
  232. <h3>[method:this negate]()</h3>
  233. <p>Inverts this vector - i.e. sets x = -x and y = -y.</p>
  234. <h3>[method:this normalize]()</h3>
  235. <p>
  236. Converts this vector to a [link:https://en.wikipedia.org/wiki/Unit_vector unit vector] -
  237. that is, sets it equal to a vector with the same direction
  238. as this one, but [page:.length length] 1.
  239. </p>
  240. <h3>[method:this max]( [param:Vector2 v] )</h3>
  241. <p>
  242. If this vector's x or y value is less than [page:Vector2 v]'s x or y
  243. value, replace that value with the corresponding max value.
  244. </p>
  245. <h3>[method:this min]( [param:Vector2 v] )</h3>
  246. <p>
  247. If this vector's x or y value is greater than [page:Vector2 v]'s x or y
  248. value, replace that value with the corresponding min value.
  249. </p>
  250. <h3>[method:this multiply]( [param:Vector2 v] )</h3>
  251. <p>Multiplies this vector by [page:Vector2 v].</p>
  252. <h3>[method:this multiplyScalar]( [param:Float s] )</h3>
  253. <p>Multiplies this vector by scalar [page:Float s].</p>
  254. <h3>
  255. [method:this rotateAround]( [param:Vector2 center], [param:Float angle] )
  256. </h3>
  257. <p>
  258. [page:Vector2 center] - the point around which to rotate.<br />
  259. [page:Float angle] - the angle to rotate, in radians.<br /><br />
  260. Rotates this vector around [page:Vector2 center] by [page:Float angle]
  261. radians.
  262. </p>
  263. <h3>[method:this round]()</h3>
  264. <p>
  265. The components of this vector are rounded to the nearest integer value.
  266. </p>
  267. <h3>[method:this roundToZero]()</h3>
  268. <p>
  269. The components of this vector are rounded towards zero (up if negative,
  270. down if positive) to an integer value.
  271. </p>
  272. <h3>[method:this set]( [param:Float x], [param:Float y] )</h3>
  273. <p>Sets the [page:.x x] and [page:.y y] components of this vector.</p>
  274. <h3>
  275. [method:this setComponent]( [param:Integer index], [param:Float value] )
  276. </h3>
  277. <p>
  278. [page:Integer index] - `0` or `1`.<br />
  279. [page:Float value] - [page:Float]<br /><br />
  280. If index equals `0` set [page:.x x] to [page:Float value]. <br />
  281. If index equals `1` set [page:.y y] to [page:Float value]
  282. </p>
  283. <h3>[method:this setLength]( [param:Float l] )</h3>
  284. <p>
  285. Sets this vector to a vector with the same direction as this one, but
  286. [page:.length length] [page:Float l].
  287. </p>
  288. <h3>[method:this setScalar]( [param:Float scalar] )</h3>
  289. <p>
  290. Sets the [page:.x x] and [page:.y y] values of this vector both equal to
  291. [page:Float scalar].
  292. </p>
  293. <h3>[method:this setX]( [param:Float x] )</h3>
  294. <p>Replaces this vector's [page:.x x] value with [page:Float x].</p>
  295. <h3>[method:this setY]( [param:Float y] )</h3>
  296. <p>Replaces this vector's [page:.y y] value with [page:Float y].</p>
  297. <h3>[method:this sub]( [param:Vector2 v] )</h3>
  298. <p>Subtracts [page:Vector2 v] from this vector.</p>
  299. <h3>[method:this subScalar]( [param:Float s] )</h3>
  300. <p>
  301. Subtracts [page:Float s] from this vector's [page:.x x] and [page:.y y]
  302. components.
  303. </p>
  304. <h3>[method:this subVectors]( [param:Vector2 a], [param:Vector2 b] )</h3>
  305. <p>Sets this vector to [page:Vector2 a] - [page:Vector2 b].</p>
  306. <h3>
  307. [method:Array toArray]( [param:Array array], [param:Integer offset] )
  308. </h3>
  309. <p>
  310. [page:Array array] - (optional) array to store this vector to. If this is
  311. not provided, a new array will be created.<br />
  312. [page:Integer offset] - (optional) optional offset into the array.<br /><br />
  313. Returns an array [x, y], or copies x and y into the provided [page:Array array].
  314. </p>
  315. <h3>[method:this random]()</h3>
  316. <p>
  317. Sets each component of this vector to a pseudo-random value between `0` and
  318. `1`, excluding `1`.
  319. </p>
  320. <h2>Source</h2>
  321. <p>
  322. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  323. </p>
  324. </body>
  325. </html>