IntVector2.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. #region --- License ---
  2. /*
  3. Copyright (c) 2006 - 2008 The Open Toolkit library.
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do
  9. so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #endregion
  21. using System;
  22. using System.Runtime.InteropServices;
  23. namespace AtomicEngine
  24. {
  25. /// <summary>Represents a 2D vector using two single-precision inting-point numbers.</summary>
  26. /// <remarks>
  27. /// The IntVector2 structure is suitable for interoperation with unmanaged code requiring two consecutive ints.
  28. /// </remarks>
  29. [StructLayout(LayoutKind.Sequential)]
  30. public struct IntVector2 : IEquatable<IntVector2>
  31. {
  32. #region Fields
  33. /// <summary>
  34. /// The X component of the IntVector2.
  35. /// </summary>
  36. public int X;
  37. /// <summary>
  38. /// The Y component of the IntVector2.
  39. /// </summary>
  40. public int Y;
  41. #endregion
  42. #region Constructors
  43. /// <summary>
  44. /// Constructs a new IntVector2.
  45. /// </summary>
  46. /// <param name="x">The x coordinate of the net IntVector2.</param>
  47. /// <param name="y">The y coordinate of the net IntVector2.</param>
  48. public IntVector2(int x, int y)
  49. {
  50. X = x;
  51. Y = y;
  52. }
  53. /// <summary>
  54. /// Constructs a new IntVector2 from the given IntVector2.
  55. /// </summary>
  56. /// <param name="v">The IntVector2 to copy components from.</param>
  57. [Obsolete]
  58. public IntVector2(IntVector2 v)
  59. {
  60. X = v.X;
  61. Y = v.Y;
  62. }
  63. /// <summary>
  64. /// Constructs a new IntVector2 from the given Vector2.
  65. /// </summary>
  66. /// <param name="v">The IntVector2 to copy components from.</param>
  67. [Obsolete]
  68. public IntVector2(Vector2 v)
  69. {
  70. X = (int) v.X;
  71. Y = (int) v.Y;
  72. }
  73. #endregion
  74. #region Public Members
  75. #region Instance
  76. #region public void Add()
  77. /// <summary>Add the Vector passed as parameter to this instance.</summary>
  78. /// <param name="right">Right operand. This parameter is only read from.</param>
  79. [Obsolete("Use static Add() method instead.")]
  80. public void Add(IntVector2 right)
  81. {
  82. this.X += right.X;
  83. this.Y += right.Y;
  84. }
  85. /// <summary>Add the Vector passed as parameter to this instance.</summary>
  86. /// <param name="right">Right operand. This parameter is only read from.</param>
  87. [CLSCompliant(false)]
  88. [Obsolete("Use static Add() method instead.")]
  89. public void Add(ref IntVector2 right)
  90. {
  91. this.X += right.X;
  92. this.Y += right.Y;
  93. }
  94. #endregion public void Add()
  95. #region public void Sub()
  96. /// <summary>Subtract the Vector passed as parameter from this instance.</summary>
  97. /// <param name="right">Right operand. This parameter is only read from.</param>
  98. [Obsolete("Use static Subtract() method instead.")]
  99. public void Sub(IntVector2 right)
  100. {
  101. this.X -= right.X;
  102. this.Y -= right.Y;
  103. }
  104. /// <summary>Subtract the Vector passed as parameter from this instance.</summary>
  105. /// <param name="right">Right operand. This parameter is only read from.</param>
  106. [CLSCompliant(false)]
  107. [Obsolete("Use static Subtract() method instead.")]
  108. public void Sub(ref IntVector2 right)
  109. {
  110. this.X -= right.X;
  111. this.Y -= right.Y;
  112. }
  113. #endregion public void Sub()
  114. #region public void Mult()
  115. /// <summary>Multiply this instance by a scalar.</summary>
  116. /// <param name="f">Scalar operand.</param>
  117. [Obsolete("Use static Multiply() method instead.")]
  118. public void Mult(int f)
  119. {
  120. this.X *= f;
  121. this.Y *= f;
  122. }
  123. #endregion public void Mult()
  124. #region public void Div()
  125. /// <summary>Divide this instance by a scalar.</summary>
  126. /// <param name="f">Scalar operand.</param>
  127. [Obsolete("Use static Divide() method instead.")]
  128. public void Div(int f)
  129. {
  130. this.X = X / f;
  131. this.Y = Y / f;
  132. }
  133. #endregion public void Div()
  134. #region public int Length
  135. /// <summary>
  136. /// Gets the length (magnitude) of the vector.
  137. /// </summary>
  138. /// <see cref="LengthFast"/>
  139. /// <seealso cref="LengthSquared"/>
  140. public int Length
  141. {
  142. get
  143. {
  144. return (int)System.Math.Sqrt(X * X + Y * Y);
  145. }
  146. }
  147. #endregion
  148. #region public int LengthFast
  149. /// <summary>
  150. /// Gets an approximation of the vector length (magnitude).
  151. /// </summary>
  152. /// <remarks>
  153. /// This property uses an approximation of the square root function to calculate vector magnitude, with
  154. /// an upper error bound of 0.001.
  155. /// </remarks>
  156. /// <see cref="Length"/>
  157. /// <seealso cref="LengthSquared"/>
  158. public int LengthFast
  159. {
  160. get
  161. {
  162. return (int) (1.0f / MathHelper.InverseSqrtFast(X * X + Y * Y));
  163. }
  164. }
  165. #endregion
  166. #region public int LengthSquared
  167. /// <summary>
  168. /// Gets the square of the vector length (magnitude).
  169. /// </summary>
  170. /// <remarks>
  171. /// This property avoids the costly square root operation required by the Length property. This makes it more suitable
  172. /// for comparisons.
  173. /// </remarks>
  174. /// <see cref="Length"/>
  175. /// <seealso cref="LengthFast"/>
  176. public int LengthSquared
  177. {
  178. get
  179. {
  180. return X * X + Y * Y;
  181. }
  182. }
  183. #endregion
  184. #region public IntVector2 PerpendicularRight
  185. /// <summary>
  186. /// Gets the perpendicular vector on the right side of this vector.
  187. /// </summary>
  188. public IntVector2 PerpendicularRight
  189. {
  190. get
  191. {
  192. return new IntVector2(Y, -X);
  193. }
  194. }
  195. #endregion
  196. #region public IntVector2 PerpendicularLeft
  197. /// <summary>
  198. /// Gets the perpendicular vector on the left side of this vector.
  199. /// </summary>
  200. public IntVector2 PerpendicularLeft
  201. {
  202. get
  203. {
  204. return new IntVector2(-Y, X);
  205. }
  206. }
  207. #endregion
  208. #region public void Normalize()
  209. /// <summary>
  210. /// Scales the IntVector2 to unit length.
  211. /// </summary>
  212. public void Normalize()
  213. {
  214. X = X / this.Length;
  215. Y *= Y / this.Length;
  216. }
  217. #endregion
  218. #region public void NormalizeFast()
  219. /// <summary>
  220. /// Scales the IntVector2 to approximately unit length.
  221. /// </summary>
  222. public void NormalizeFast()
  223. {
  224. int scale = (int) MathHelper.InverseSqrtFast(X * X + Y * Y);
  225. X *= scale;
  226. Y *= scale;
  227. }
  228. #endregion
  229. #region public void Scale()
  230. /// <summary>
  231. /// Scales the current IntVector2 by the given amounts.
  232. /// </summary>
  233. /// <param name="sx">The scale of the X component.</param>
  234. /// <param name="sy">The scale of the Y component.</param>
  235. [Obsolete("Use static Multiply() method instead.")]
  236. public void Scale(int sx, int sy)
  237. {
  238. this.X = X * sx;
  239. this.Y = Y * sy;
  240. }
  241. /// <summary>Scales this instance by the given parameter.</summary>
  242. /// <param name="scale">The scaling of the individual components.</param>
  243. [Obsolete("Use static Multiply() method instead.")]
  244. public void Scale(IntVector2 scale)
  245. {
  246. this.X *= scale.X;
  247. this.Y *= scale.Y;
  248. }
  249. /// <summary>Scales this instance by the given parameter.</summary>
  250. /// <param name="scale">The scaling of the individual components.</param>
  251. [CLSCompliant(false)]
  252. [Obsolete("Use static Multiply() method instead.")]
  253. public void Scale(ref IntVector2 scale)
  254. {
  255. this.X *= scale.X;
  256. this.Y *= scale.Y;
  257. }
  258. #endregion public void Scale()
  259. #endregion
  260. #region Static
  261. #region Fields
  262. /// <summary>
  263. /// Defines a unit-length IntVector2 that points towards the X-axis.
  264. /// </summary>
  265. public static readonly IntVector2 UnitX = new IntVector2(1, 0);
  266. /// <summary>
  267. /// Defines a unit-length IntVector2 that points towards the Y-axis.
  268. /// </summary>
  269. public static readonly IntVector2 UnitY = new IntVector2(0, 1);
  270. /// <summary>
  271. /// Defines a zero-length IntVector2.
  272. /// </summary>
  273. public static readonly IntVector2 Zero = new IntVector2(0, 0);
  274. /// <summary>
  275. /// Defines an instance with all components set to 1.
  276. /// </summary>
  277. public static readonly IntVector2 One = new IntVector2(1, 1);
  278. /// <summary>
  279. /// Defines the size of the IntVector2 struct in bytes.
  280. /// </summary>
  281. public static readonly int SizeInBytes = Marshal.SizeOf(new IntVector2());
  282. #endregion
  283. #region Add
  284. /// <summary>
  285. /// Adds two vectors.
  286. /// </summary>
  287. /// <param name="a">Left operand.</param>
  288. /// <param name="b">Right operand.</param>
  289. /// <returns>Result of operation.</returns>
  290. public static IntVector2 Add(IntVector2 a, IntVector2 b)
  291. {
  292. Add(ref a, ref b, out a);
  293. return a;
  294. }
  295. /// <summary>
  296. /// Adds two vectors.
  297. /// </summary>
  298. /// <param name="a">Left operand.</param>
  299. /// <param name="b">Right operand.</param>
  300. /// <param name="result">Result of operation.</param>
  301. public static void Add(ref IntVector2 a, ref IntVector2 b, out IntVector2 result)
  302. {
  303. result = new IntVector2(a.X + b.X, a.Y + b.Y);
  304. }
  305. #endregion
  306. #region Subtract
  307. /// <summary>
  308. /// Subtract one Vector from another
  309. /// </summary>
  310. /// <param name="a">First operand</param>
  311. /// <param name="b">Second operand</param>
  312. /// <returns>Result of subtraction</returns>
  313. public static IntVector2 Subtract(IntVector2 a, IntVector2 b)
  314. {
  315. Subtract(ref a, ref b, out a);
  316. return a;
  317. }
  318. /// <summary>
  319. /// Subtract one Vector from another
  320. /// </summary>
  321. /// <param name="a">First operand</param>
  322. /// <param name="b">Second operand</param>
  323. /// <param name="result">Result of subtraction</param>
  324. public static void Subtract(ref IntVector2 a, ref IntVector2 b, out IntVector2 result)
  325. {
  326. result = new IntVector2(a.X - b.X, a.Y - b.Y);
  327. }
  328. #endregion
  329. #region Multiply
  330. /// <summary>
  331. /// Multiplies a vector by a scalar.
  332. /// </summary>
  333. /// <param name="vector">Left operand.</param>
  334. /// <param name="scale">Right operand.</param>
  335. /// <returns>Result of the operation.</returns>
  336. public static IntVector2 Multiply(IntVector2 vector, int scale)
  337. {
  338. Multiply(ref vector, scale, out vector);
  339. return vector;
  340. }
  341. /// <summary>
  342. /// Multiplies a vector by a scalar.
  343. /// </summary>
  344. /// <param name="vector">Left operand.</param>
  345. /// <param name="scale">Right operand.</param>
  346. /// <param name="result">Result of the operation.</param>
  347. public static void Multiply(ref IntVector2 vector, int scale, out IntVector2 result)
  348. {
  349. result = new IntVector2(vector.X * scale, vector.Y * scale);
  350. }
  351. /// <summary>
  352. /// Multiplies a vector by the components a vector (scale).
  353. /// </summary>
  354. /// <param name="vector">Left operand.</param>
  355. /// <param name="scale">Right operand.</param>
  356. /// <returns>Result of the operation.</returns>
  357. public static IntVector2 Multiply(IntVector2 vector, IntVector2 scale)
  358. {
  359. Multiply(ref vector, ref scale, out vector);
  360. return vector;
  361. }
  362. /// <summary>
  363. /// Multiplies a vector by the components of a vector (scale).
  364. /// </summary>
  365. /// <param name="vector">Left operand.</param>
  366. /// <param name="scale">Right operand.</param>
  367. /// <param name="result">Result of the operation.</param>
  368. public static void Multiply(ref IntVector2 vector, ref IntVector2 scale, out IntVector2 result)
  369. {
  370. result = new IntVector2(vector.X * scale.X, vector.Y * scale.Y);
  371. }
  372. #endregion
  373. #region Divide
  374. /// <summary>
  375. /// Divides a vector by a scalar.
  376. /// </summary>
  377. /// <param name="vector">Left operand.</param>
  378. /// <param name="scale">Right operand.</param>
  379. /// <returns>Result of the operation.</returns>
  380. public static IntVector2 Divide(IntVector2 vector, int scale)
  381. {
  382. Divide(ref vector, scale, out vector);
  383. return vector;
  384. }
  385. /// <summary>
  386. /// Divides a vector by a scalar.
  387. /// </summary>
  388. /// <param name="vector">Left operand.</param>
  389. /// <param name="scale">Right operand.</param>
  390. /// <param name="result">Result of the operation.</param>
  391. public static void Divide(ref IntVector2 vector, int scale, out IntVector2 result)
  392. {
  393. Multiply(ref vector, 1 / scale, out result);
  394. }
  395. /// <summary>
  396. /// Divides a vector by the components of a vector (scale).
  397. /// </summary>
  398. /// <param name="vector">Left operand.</param>
  399. /// <param name="scale">Right operand.</param>
  400. /// <returns>Result of the operation.</returns>
  401. public static IntVector2 Divide(IntVector2 vector, IntVector2 scale)
  402. {
  403. Divide(ref vector, ref scale, out vector);
  404. return vector;
  405. }
  406. /// <summary>
  407. /// Divide a vector by the components of a vector (scale).
  408. /// </summary>
  409. /// <param name="vector">Left operand.</param>
  410. /// <param name="scale">Right operand.</param>
  411. /// <param name="result">Result of the operation.</param>
  412. public static void Divide(ref IntVector2 vector, ref IntVector2 scale, out IntVector2 result)
  413. {
  414. result = new IntVector2(vector.X / scale.X, vector.Y / scale.Y);
  415. }
  416. #endregion
  417. #region ComponentMin
  418. /// <summary>
  419. /// Calculate the component-wise minimum of two vectors
  420. /// </summary>
  421. /// <param name="a">First operand</param>
  422. /// <param name="b">Second operand</param>
  423. /// <returns>The component-wise minimum</returns>
  424. public static IntVector2 ComponentMin(IntVector2 a, IntVector2 b)
  425. {
  426. a.X = a.X < b.X ? a.X : b.X;
  427. a.Y = a.Y < b.Y ? a.Y : b.Y;
  428. return a;
  429. }
  430. /// <summary>
  431. /// Calculate the component-wise minimum of two vectors
  432. /// </summary>
  433. /// <param name="a">First operand</param>
  434. /// <param name="b">Second operand</param>
  435. /// <param name="result">The component-wise minimum</param>
  436. public static void ComponentMin(ref IntVector2 a, ref IntVector2 b, out IntVector2 result)
  437. {
  438. result.X = a.X < b.X ? a.X : b.X;
  439. result.Y = a.Y < b.Y ? a.Y : b.Y;
  440. }
  441. #endregion
  442. #region ComponentMax
  443. /// <summary>
  444. /// Calculate the component-wise maximum of two vectors
  445. /// </summary>
  446. /// <param name="a">First operand</param>
  447. /// <param name="b">Second operand</param>
  448. /// <returns>The component-wise maximum</returns>
  449. public static IntVector2 ComponentMax(IntVector2 a, IntVector2 b)
  450. {
  451. a.X = a.X > b.X ? a.X : b.X;
  452. a.Y = a.Y > b.Y ? a.Y : b.Y;
  453. return a;
  454. }
  455. /// <summary>
  456. /// Calculate the component-wise maximum of two vectors
  457. /// </summary>
  458. /// <param name="a">First operand</param>
  459. /// <param name="b">Second operand</param>
  460. /// <param name="result">The component-wise maximum</param>
  461. public static void ComponentMax(ref IntVector2 a, ref IntVector2 b, out IntVector2 result)
  462. {
  463. result.X = a.X > b.X ? a.X : b.X;
  464. result.Y = a.Y > b.Y ? a.Y : b.Y;
  465. }
  466. #endregion
  467. #region Min
  468. /// <summary>
  469. /// Returns the Vector3 with the minimum magnitude
  470. /// </summary>
  471. /// <param name="left">Left operand</param>
  472. /// <param name="right">Right operand</param>
  473. /// <returns>The minimum Vector3</returns>
  474. public static IntVector2 Min(IntVector2 left, IntVector2 right)
  475. {
  476. return left.LengthSquared < right.LengthSquared ? left : right;
  477. }
  478. #endregion
  479. #region Max
  480. /// <summary>
  481. /// Returns the Vector3 with the minimum magnitude
  482. /// </summary>
  483. /// <param name="left">Left operand</param>
  484. /// <param name="right">Right operand</param>
  485. /// <returns>The minimum Vector3</returns>
  486. public static IntVector2 Max(IntVector2 left, IntVector2 right)
  487. {
  488. return left.LengthSquared >= right.LengthSquared ? left : right;
  489. }
  490. #endregion
  491. #region Clamp
  492. /// <summary>
  493. /// Clamp a vector to the given minimum and maximum vectors
  494. /// </summary>
  495. /// <param name="vec">Input vector</param>
  496. /// <param name="min">Minimum vector</param>
  497. /// <param name="max">Maximum vector</param>
  498. /// <returns>The clamped vector</returns>
  499. public static IntVector2 Clamp(IntVector2 vec, IntVector2 min, IntVector2 max)
  500. {
  501. vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
  502. vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
  503. return vec;
  504. }
  505. /// <summary>
  506. /// Clamp a vector to the given minimum and maximum vectors
  507. /// </summary>
  508. /// <param name="vec">Input vector</param>
  509. /// <param name="min">Minimum vector</param>
  510. /// <param name="max">Maximum vector</param>
  511. /// <param name="result">The clamped vector</param>
  512. public static void Clamp(ref IntVector2 vec, ref IntVector2 min, ref IntVector2 max, out IntVector2 result)
  513. {
  514. result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
  515. result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
  516. }
  517. #endregion
  518. #region Normalize
  519. /// <summary>
  520. /// Scale a vector to unit length
  521. /// </summary>
  522. /// <param name="vec">The input vector</param>
  523. /// <returns>The normalized vector</returns>
  524. public static IntVector2 Normalize(IntVector2 vec)
  525. {
  526. vec.X = vec.X / vec.Length;
  527. vec.Y = vec.Y / vec.Length;
  528. return vec;
  529. }
  530. /// <summary>
  531. /// Scale a vector to unit length
  532. /// </summary>
  533. /// <param name="vec">The input vector</param>
  534. /// <param name="result">The normalized vector</param>
  535. public static void Normalize(ref IntVector2 vec, out IntVector2 result)
  536. {
  537. result.X = vec.X / vec.Length;
  538. result.Y = vec.Y / vec.Length;
  539. }
  540. #endregion
  541. #region NormalizeFast
  542. /// <summary>
  543. /// Scale a vector to approximately unit length
  544. /// </summary>
  545. /// <param name="vec">The input vector</param>
  546. /// <returns>The normalized vector</returns>
  547. public static IntVector2 NormalizeFast(IntVector2 vec)
  548. {
  549. int scale = (int) MathHelper.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y);
  550. vec.X *= scale;
  551. vec.Y *= scale;
  552. return vec;
  553. }
  554. /// <summary>
  555. /// Scale a vector to approximately unit length
  556. /// </summary>
  557. /// <param name="vec">The input vector</param>
  558. /// <param name="result">The normalized vector</param>
  559. public static void NormalizeFast(ref IntVector2 vec, out IntVector2 result)
  560. {
  561. int scale = (int) MathHelper.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y);
  562. result.X = vec.X * scale;
  563. result.Y = vec.Y * scale;
  564. }
  565. #endregion
  566. #region Dot
  567. /// <summary>
  568. /// Calculate the dot (scalar) product of two vectors
  569. /// </summary>
  570. /// <param name="left">First operand</param>
  571. /// <param name="right">Second operand</param>
  572. /// <returns>The dot product of the two inputs</returns>
  573. public static int Dot(IntVector2 left, IntVector2 right)
  574. {
  575. return left.X * right.X + left.Y * right.Y;
  576. }
  577. /// <summary>
  578. /// Calculate the dot (scalar) product of two vectors
  579. /// </summary>
  580. /// <param name="left">First operand</param>
  581. /// <param name="right">Second operand</param>
  582. /// <param name="result">The dot product of the two inputs</param>
  583. public static void Dot(ref IntVector2 left, ref IntVector2 right, out int result)
  584. {
  585. result = left.X * right.X + left.Y * right.Y;
  586. }
  587. #endregion
  588. #region Lerp
  589. /// <summary>
  590. /// Returns a new Vector that is the linear blend of the 2 given Vectors
  591. /// </summary>
  592. /// <param name="a">First input vector</param>
  593. /// <param name="b">Second input vector</param>
  594. /// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
  595. /// <returns>a when blend=0, b when blend=1, and a linear combination otherwise</returns>
  596. public static IntVector2 Lerp(IntVector2 a, IntVector2 b, int blend)
  597. {
  598. a.X = blend * (b.X - a.X) + a.X;
  599. a.Y = blend * (b.Y - a.Y) + a.Y;
  600. return a;
  601. }
  602. /// <summary>
  603. /// Returns a new Vector that is the linear blend of the 2 given Vectors
  604. /// </summary>
  605. /// <param name="a">First input vector</param>
  606. /// <param name="b">Second input vector</param>
  607. /// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
  608. /// <param name="result">a when blend=0, b when blend=1, and a linear combination otherwise</param>
  609. public static void Lerp(ref IntVector2 a, ref IntVector2 b, int blend, out IntVector2 result)
  610. {
  611. result.X = blend * (b.X - a.X) + a.X;
  612. result.Y = blend * (b.Y - a.Y) + a.Y;
  613. }
  614. #endregion
  615. #region Barycentric
  616. /// <summary>
  617. /// Interpolate 3 Vectors using Barycentric coordinates
  618. /// </summary>
  619. /// <param name="a">First input Vector</param>
  620. /// <param name="b">Second input Vector</param>
  621. /// <param name="c">Third input Vector</param>
  622. /// <param name="u">First Barycentric Coordinate</param>
  623. /// <param name="v">Second Barycentric Coordinate</param>
  624. /// <returns>a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise</returns>
  625. public static IntVector2 BaryCentric(IntVector2 a, IntVector2 b, IntVector2 c, int u, int v)
  626. {
  627. return a + u * (b - a) + v * (c - a);
  628. }
  629. /// <summary>Interpolate 3 Vectors using Barycentric coordinates</summary>
  630. /// <param name="a">First input Vector.</param>
  631. /// <param name="b">Second input Vector.</param>
  632. /// <param name="c">Third input Vector.</param>
  633. /// <param name="u">First Barycentric Coordinate.</param>
  634. /// <param name="v">Second Barycentric Coordinate.</param>
  635. /// <param name="result">Output Vector. a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise</param>
  636. public static void BaryCentric(ref IntVector2 a, ref IntVector2 b, ref IntVector2 c, int u, int v, out IntVector2 result)
  637. {
  638. result = a; // copy
  639. IntVector2 temp = b; // copy
  640. Subtract(ref temp, ref a, out temp);
  641. Multiply(ref temp, u, out temp);
  642. Add(ref result, ref temp, out result);
  643. temp = c; // copy
  644. Subtract(ref temp, ref a, out temp);
  645. Multiply(ref temp, v, out temp);
  646. Add(ref result, ref temp, out result);
  647. }
  648. #endregion
  649. #endregion
  650. #region Operators
  651. /// <summary>
  652. /// Adds the specified instances.
  653. /// </summary>
  654. /// <param name="left">Left operand.</param>
  655. /// <param name="right">Right operand.</param>
  656. /// <returns>Result of addition.</returns>
  657. public static IntVector2 operator +(IntVector2 left, IntVector2 right)
  658. {
  659. left.X += right.X;
  660. left.Y += right.Y;
  661. return left;
  662. }
  663. /// <summary>
  664. /// Subtracts the specified instances.
  665. /// </summary>
  666. /// <param name="left">Left operand.</param>
  667. /// <param name="right">Right operand.</param>
  668. /// <returns>Result of subtraction.</returns>
  669. public static IntVector2 operator -(IntVector2 left, IntVector2 right)
  670. {
  671. left.X -= right.X;
  672. left.Y -= right.Y;
  673. return left;
  674. }
  675. /// <summary>
  676. /// Negates the specified instance.
  677. /// </summary>
  678. /// <param name="vec">Operand.</param>
  679. /// <returns>Result of negation.</returns>
  680. public static IntVector2 operator -(IntVector2 vec)
  681. {
  682. vec.X = -vec.X;
  683. vec.Y = -vec.Y;
  684. return vec;
  685. }
  686. /// <summary>
  687. /// Multiplies the specified instance by a scalar.
  688. /// </summary>
  689. /// <param name="vec">Left operand.</param>
  690. /// <param name="scale">Right operand.</param>
  691. /// <returns>Result of multiplication.</returns>
  692. public static IntVector2 operator *(IntVector2 vec, int scale)
  693. {
  694. vec.X *= scale;
  695. vec.Y *= scale;
  696. return vec;
  697. }
  698. /// <summary>
  699. /// Multiplies the specified instance by a scalar.
  700. /// </summary>
  701. /// <param name="scale">Left operand.</param>
  702. /// <param name="vec">Right operand.</param>
  703. /// <returns>Result of multiplication.</returns>
  704. public static IntVector2 operator *(int scale, IntVector2 vec)
  705. {
  706. vec.X *= scale;
  707. vec.Y *= scale;
  708. return vec;
  709. }
  710. /// <summary>
  711. /// Divides the specified instance by a scalar.
  712. /// </summary>
  713. /// <param name="vec">Left operand</param>
  714. /// <param name="scale">Right operand</param>
  715. /// <returns>Result of the division.</returns>
  716. public static IntVector2 operator /(IntVector2 vec, int scale)
  717. {
  718. vec.X = vec.X / scale;
  719. vec.Y = vec.Y / scale;
  720. return vec;
  721. }
  722. /// <summary>
  723. /// Compares the specified instances for equality.
  724. /// </summary>
  725. /// <param name="left">Left operand.</param>
  726. /// <param name="right">Right operand.</param>
  727. /// <returns>True if both instances are equal; false otherwise.</returns>
  728. public static bool operator ==(IntVector2 left, IntVector2 right)
  729. {
  730. return left.Equals(right);
  731. }
  732. /// <summary>
  733. /// Compares the specified instances for inequality.
  734. /// </summary>
  735. /// <param name="left">Left operand.</param>
  736. /// <param name="right">Right operand.</param>
  737. /// <returns>True if both instances are not equal; false otherwise.</returns>
  738. public static bool operator !=(IntVector2 left, IntVector2 right)
  739. {
  740. return !left.Equals(right);
  741. }
  742. #endregion
  743. #region Overrides
  744. #region public override string ToString()
  745. /// <summary>
  746. /// Returns a System.String that represents the current IntVector2.
  747. /// </summary>
  748. /// <returns></returns>
  749. public override string ToString()
  750. {
  751. return String.Format("({0}, {1})", X, Y);
  752. }
  753. #endregion
  754. #region public override int GetHashCode()
  755. /// <summary>
  756. /// Returns the hashcode for this instance.
  757. /// </summary>
  758. /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
  759. public override int GetHashCode()
  760. {
  761. return X.GetHashCode() ^ Y.GetHashCode();
  762. }
  763. #endregion
  764. #region public override bool Equals(object obj)
  765. /// <summary>
  766. /// Indicates whether this instance and a specified object are equal.
  767. /// </summary>
  768. /// <param name="obj">The object to compare to.</param>
  769. /// <returns>True if the instances are equal; false otherwise.</returns>
  770. public override bool Equals(object obj)
  771. {
  772. if (!(obj is IntVector2))
  773. return false;
  774. return this.Equals((IntVector2)obj);
  775. }
  776. #endregion
  777. #endregion
  778. #endregion
  779. #region IEquatable<IntVector2> Members
  780. /// <summary>Indicates whether the current vector is equal to another vector.</summary>
  781. /// <param name="other">A vector to compare with this vector.</param>
  782. /// <returns>true if the current vector is equal to the vector parameter; otherwise, false.</returns>
  783. public bool Equals(IntVector2 other)
  784. {
  785. return
  786. X == other.X &&
  787. Y == other.Y;
  788. }
  789. #endregion
  790. public bool IsEmpty => X == 0 && Y == 0;
  791. }
  792. }