| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943 |
- #region --- License ---
- /*
- Copyright (c) 2006 - 2008 The Open Toolkit library.
- Permission is hereby granted, free of charge, to any person obtaining a copy of
- this software and associated documentation files (the "Software"), to deal in
- the Software without restriction, including without limitation the rights to
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
- of the Software, and to permit persons to whom the Software is furnished to do
- so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- */
- #endregion
- using System;
- using System.Runtime.InteropServices;
- namespace AtomicEngine
- {
- /// <summary>Represents a 2D vector using two single-precision inting-point numbers.</summary>
- /// <remarks>
- /// The IntVector2 structure is suitable for interoperation with unmanaged code requiring two consecutive ints.
- /// </remarks>
- [StructLayout(LayoutKind.Sequential)]
- public struct IntVector2 : IEquatable<IntVector2>
- {
- #region Fields
- /// <summary>
- /// The X component of the IntVector2.
- /// </summary>
- public int X;
- /// <summary>
- /// The Y component of the IntVector2.
- /// </summary>
- public int Y;
- #endregion
- #region Constructors
- /// <summary>
- /// Constructs a new IntVector2.
- /// </summary>
- /// <param name="x">The x coordinate of the net IntVector2.</param>
- /// <param name="y">The y coordinate of the net IntVector2.</param>
- public IntVector2(int x, int y)
- {
- X = x;
- Y = y;
- }
- /// <summary>
- /// Constructs a new IntVector2 from the given IntVector2.
- /// </summary>
- /// <param name="v">The IntVector2 to copy components from.</param>
- [Obsolete]
- public IntVector2(IntVector2 v)
- {
- X = v.X;
- Y = v.Y;
- }
- /// <summary>
- /// Constructs a new IntVector2 from the given Vector2.
- /// </summary>
- /// <param name="v">The IntVector2 to copy components from.</param>
- [Obsolete]
- public IntVector2(Vector2 v)
- {
- X = (int) v.X;
- Y = (int) v.Y;
- }
- #endregion
- #region Public Members
- #region Instance
- #region public void Add()
- /// <summary>Add the Vector passed as parameter to this instance.</summary>
- /// <param name="right">Right operand. This parameter is only read from.</param>
- [Obsolete("Use static Add() method instead.")]
- public void Add(IntVector2 right)
- {
- this.X += right.X;
- this.Y += right.Y;
- }
- /// <summary>Add the Vector passed as parameter to this instance.</summary>
- /// <param name="right">Right operand. This parameter is only read from.</param>
- [CLSCompliant(false)]
- [Obsolete("Use static Add() method instead.")]
- public void Add(ref IntVector2 right)
- {
- this.X += right.X;
- this.Y += right.Y;
- }
- #endregion public void Add()
- #region public void Sub()
- /// <summary>Subtract the Vector passed as parameter from this instance.</summary>
- /// <param name="right">Right operand. This parameter is only read from.</param>
- [Obsolete("Use static Subtract() method instead.")]
- public void Sub(IntVector2 right)
- {
- this.X -= right.X;
- this.Y -= right.Y;
- }
- /// <summary>Subtract the Vector passed as parameter from this instance.</summary>
- /// <param name="right">Right operand. This parameter is only read from.</param>
- [CLSCompliant(false)]
- [Obsolete("Use static Subtract() method instead.")]
- public void Sub(ref IntVector2 right)
- {
- this.X -= right.X;
- this.Y -= right.Y;
- }
- #endregion public void Sub()
- #region public void Mult()
- /// <summary>Multiply this instance by a scalar.</summary>
- /// <param name="f">Scalar operand.</param>
- [Obsolete("Use static Multiply() method instead.")]
- public void Mult(int f)
- {
- this.X *= f;
- this.Y *= f;
- }
- #endregion public void Mult()
- #region public void Div()
- /// <summary>Divide this instance by a scalar.</summary>
- /// <param name="f">Scalar operand.</param>
- [Obsolete("Use static Divide() method instead.")]
- public void Div(int f)
- {
- this.X = X / f;
- this.Y = Y / f;
- }
- #endregion public void Div()
- #region public int Length
- /// <summary>
- /// Gets the length (magnitude) of the vector.
- /// </summary>
- /// <see cref="LengthFast"/>
- /// <seealso cref="LengthSquared"/>
- public int Length
- {
- get
- {
- return (int)System.Math.Sqrt(X * X + Y * Y);
- }
- }
- #endregion
- #region public int LengthFast
- /// <summary>
- /// Gets an approximation of the vector length (magnitude).
- /// </summary>
- /// <remarks>
- /// This property uses an approximation of the square root function to calculate vector magnitude, with
- /// an upper error bound of 0.001.
- /// </remarks>
- /// <see cref="Length"/>
- /// <seealso cref="LengthSquared"/>
- public int LengthFast
- {
- get
- {
- return (int) (1.0f / MathHelper.InverseSqrtFast(X * X + Y * Y));
- }
- }
- #endregion
- #region public int LengthSquared
- /// <summary>
- /// Gets the square of the vector length (magnitude).
- /// </summary>
- /// <remarks>
- /// This property avoids the costly square root operation required by the Length property. This makes it more suitable
- /// for comparisons.
- /// </remarks>
- /// <see cref="Length"/>
- /// <seealso cref="LengthFast"/>
- public int LengthSquared
- {
- get
- {
- return X * X + Y * Y;
- }
- }
- #endregion
- #region public IntVector2 PerpendicularRight
- /// <summary>
- /// Gets the perpendicular vector on the right side of this vector.
- /// </summary>
- public IntVector2 PerpendicularRight
- {
- get
- {
- return new IntVector2(Y, -X);
- }
- }
- #endregion
- #region public IntVector2 PerpendicularLeft
- /// <summary>
- /// Gets the perpendicular vector on the left side of this vector.
- /// </summary>
- public IntVector2 PerpendicularLeft
- {
- get
- {
- return new IntVector2(-Y, X);
- }
- }
- #endregion
- #region public void Normalize()
- /// <summary>
- /// Scales the IntVector2 to unit length.
- /// </summary>
- public void Normalize()
- {
- X = X / this.Length;
- Y *= Y / this.Length;
- }
- #endregion
- #region public void NormalizeFast()
- /// <summary>
- /// Scales the IntVector2 to approximately unit length.
- /// </summary>
- public void NormalizeFast()
- {
- int scale = (int) MathHelper.InverseSqrtFast(X * X + Y * Y);
- X *= scale;
- Y *= scale;
- }
- #endregion
- #region public void Scale()
- /// <summary>
- /// Scales the current IntVector2 by the given amounts.
- /// </summary>
- /// <param name="sx">The scale of the X component.</param>
- /// <param name="sy">The scale of the Y component.</param>
- [Obsolete("Use static Multiply() method instead.")]
- public void Scale(int sx, int sy)
- {
- this.X = X * sx;
- this.Y = Y * sy;
- }
- /// <summary>Scales this instance by the given parameter.</summary>
- /// <param name="scale">The scaling of the individual components.</param>
- [Obsolete("Use static Multiply() method instead.")]
- public void Scale(IntVector2 scale)
- {
- this.X *= scale.X;
- this.Y *= scale.Y;
- }
- /// <summary>Scales this instance by the given parameter.</summary>
- /// <param name="scale">The scaling of the individual components.</param>
- [CLSCompliant(false)]
- [Obsolete("Use static Multiply() method instead.")]
- public void Scale(ref IntVector2 scale)
- {
- this.X *= scale.X;
- this.Y *= scale.Y;
- }
- #endregion public void Scale()
- #endregion
- #region Static
- #region Fields
- /// <summary>
- /// Defines a unit-length IntVector2 that points towards the X-axis.
- /// </summary>
- public static readonly IntVector2 UnitX = new IntVector2(1, 0);
- /// <summary>
- /// Defines a unit-length IntVector2 that points towards the Y-axis.
- /// </summary>
- public static readonly IntVector2 UnitY = new IntVector2(0, 1);
- /// <summary>
- /// Defines a zero-length IntVector2.
- /// </summary>
- public static readonly IntVector2 Zero = new IntVector2(0, 0);
- /// <summary>
- /// Defines an instance with all components set to 1.
- /// </summary>
- public static readonly IntVector2 One = new IntVector2(1, 1);
- /// <summary>
- /// Defines the size of the IntVector2 struct in bytes.
- /// </summary>
- public static readonly int SizeInBytes = Marshal.SizeOf(new IntVector2());
- #endregion
- #region Add
- /// <summary>
- /// Adds two vectors.
- /// </summary>
- /// <param name="a">Left operand.</param>
- /// <param name="b">Right operand.</param>
- /// <returns>Result of operation.</returns>
- public static IntVector2 Add(IntVector2 a, IntVector2 b)
- {
- Add(ref a, ref b, out a);
- return a;
- }
- /// <summary>
- /// Adds two vectors.
- /// </summary>
- /// <param name="a">Left operand.</param>
- /// <param name="b">Right operand.</param>
- /// <param name="result">Result of operation.</param>
- public static void Add(ref IntVector2 a, ref IntVector2 b, out IntVector2 result)
- {
- result = new IntVector2(a.X + b.X, a.Y + b.Y);
- }
- #endregion
- #region Subtract
- /// <summary>
- /// Subtract one Vector from another
- /// </summary>
- /// <param name="a">First operand</param>
- /// <param name="b">Second operand</param>
- /// <returns>Result of subtraction</returns>
- public static IntVector2 Subtract(IntVector2 a, IntVector2 b)
- {
- Subtract(ref a, ref b, out a);
- return a;
- }
- /// <summary>
- /// Subtract one Vector from another
- /// </summary>
- /// <param name="a">First operand</param>
- /// <param name="b">Second operand</param>
- /// <param name="result">Result of subtraction</param>
- public static void Subtract(ref IntVector2 a, ref IntVector2 b, out IntVector2 result)
- {
- result = new IntVector2(a.X - b.X, a.Y - b.Y);
- }
- #endregion
- #region Multiply
- /// <summary>
- /// Multiplies a vector by a scalar.
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <returns>Result of the operation.</returns>
- public static IntVector2 Multiply(IntVector2 vector, int scale)
- {
- Multiply(ref vector, scale, out vector);
- return vector;
- }
- /// <summary>
- /// Multiplies a vector by a scalar.
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <param name="result">Result of the operation.</param>
- public static void Multiply(ref IntVector2 vector, int scale, out IntVector2 result)
- {
- result = new IntVector2(vector.X * scale, vector.Y * scale);
- }
- /// <summary>
- /// Multiplies a vector by the components a vector (scale).
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <returns>Result of the operation.</returns>
- public static IntVector2 Multiply(IntVector2 vector, IntVector2 scale)
- {
- Multiply(ref vector, ref scale, out vector);
- return vector;
- }
- /// <summary>
- /// Multiplies a vector by the components of a vector (scale).
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <param name="result">Result of the operation.</param>
- public static void Multiply(ref IntVector2 vector, ref IntVector2 scale, out IntVector2 result)
- {
- result = new IntVector2(vector.X * scale.X, vector.Y * scale.Y);
- }
- #endregion
- #region Divide
- /// <summary>
- /// Divides a vector by a scalar.
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <returns>Result of the operation.</returns>
- public static IntVector2 Divide(IntVector2 vector, int scale)
- {
- Divide(ref vector, scale, out vector);
- return vector;
- }
- /// <summary>
- /// Divides a vector by a scalar.
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <param name="result">Result of the operation.</param>
- public static void Divide(ref IntVector2 vector, int scale, out IntVector2 result)
- {
- Multiply(ref vector, 1 / scale, out result);
- }
- /// <summary>
- /// Divides a vector by the components of a vector (scale).
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <returns>Result of the operation.</returns>
- public static IntVector2 Divide(IntVector2 vector, IntVector2 scale)
- {
- Divide(ref vector, ref scale, out vector);
- return vector;
- }
- /// <summary>
- /// Divide a vector by the components of a vector (scale).
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <param name="result">Result of the operation.</param>
- public static void Divide(ref IntVector2 vector, ref IntVector2 scale, out IntVector2 result)
- {
- result = new IntVector2(vector.X / scale.X, vector.Y / scale.Y);
- }
- #endregion
- #region ComponentMin
- /// <summary>
- /// Calculate the component-wise minimum of two vectors
- /// </summary>
- /// <param name="a">First operand</param>
- /// <param name="b">Second operand</param>
- /// <returns>The component-wise minimum</returns>
- public static IntVector2 ComponentMin(IntVector2 a, IntVector2 b)
- {
- a.X = a.X < b.X ? a.X : b.X;
- a.Y = a.Y < b.Y ? a.Y : b.Y;
- return a;
- }
- /// <summary>
- /// Calculate the component-wise minimum of two vectors
- /// </summary>
- /// <param name="a">First operand</param>
- /// <param name="b">Second operand</param>
- /// <param name="result">The component-wise minimum</param>
- public static void ComponentMin(ref IntVector2 a, ref IntVector2 b, out IntVector2 result)
- {
- result.X = a.X < b.X ? a.X : b.X;
- result.Y = a.Y < b.Y ? a.Y : b.Y;
- }
- #endregion
- #region ComponentMax
- /// <summary>
- /// Calculate the component-wise maximum of two vectors
- /// </summary>
- /// <param name="a">First operand</param>
- /// <param name="b">Second operand</param>
- /// <returns>The component-wise maximum</returns>
- public static IntVector2 ComponentMax(IntVector2 a, IntVector2 b)
- {
- a.X = a.X > b.X ? a.X : b.X;
- a.Y = a.Y > b.Y ? a.Y : b.Y;
- return a;
- }
- /// <summary>
- /// Calculate the component-wise maximum of two vectors
- /// </summary>
- /// <param name="a">First operand</param>
- /// <param name="b">Second operand</param>
- /// <param name="result">The component-wise maximum</param>
- public static void ComponentMax(ref IntVector2 a, ref IntVector2 b, out IntVector2 result)
- {
- result.X = a.X > b.X ? a.X : b.X;
- result.Y = a.Y > b.Y ? a.Y : b.Y;
- }
- #endregion
- #region Min
- /// <summary>
- /// Returns the Vector3 with the minimum magnitude
- /// </summary>
- /// <param name="left">Left operand</param>
- /// <param name="right">Right operand</param>
- /// <returns>The minimum Vector3</returns>
- public static IntVector2 Min(IntVector2 left, IntVector2 right)
- {
- return left.LengthSquared < right.LengthSquared ? left : right;
- }
- #endregion
- #region Max
- /// <summary>
- /// Returns the Vector3 with the minimum magnitude
- /// </summary>
- /// <param name="left">Left operand</param>
- /// <param name="right">Right operand</param>
- /// <returns>The minimum Vector3</returns>
- public static IntVector2 Max(IntVector2 left, IntVector2 right)
- {
- return left.LengthSquared >= right.LengthSquared ? left : right;
- }
- #endregion
- #region Clamp
- /// <summary>
- /// Clamp a vector to the given minimum and maximum vectors
- /// </summary>
- /// <param name="vec">Input vector</param>
- /// <param name="min">Minimum vector</param>
- /// <param name="max">Maximum vector</param>
- /// <returns>The clamped vector</returns>
- public static IntVector2 Clamp(IntVector2 vec, IntVector2 min, IntVector2 max)
- {
- vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
- vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
- return vec;
- }
- /// <summary>
- /// Clamp a vector to the given minimum and maximum vectors
- /// </summary>
- /// <param name="vec">Input vector</param>
- /// <param name="min">Minimum vector</param>
- /// <param name="max">Maximum vector</param>
- /// <param name="result">The clamped vector</param>
- public static void Clamp(ref IntVector2 vec, ref IntVector2 min, ref IntVector2 max, out IntVector2 result)
- {
- result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
- result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
- }
- #endregion
- #region Normalize
- /// <summary>
- /// Scale a vector to unit length
- /// </summary>
- /// <param name="vec">The input vector</param>
- /// <returns>The normalized vector</returns>
- public static IntVector2 Normalize(IntVector2 vec)
- {
- vec.X = vec.X / vec.Length;
- vec.Y = vec.Y / vec.Length;
- return vec;
- }
- /// <summary>
- /// Scale a vector to unit length
- /// </summary>
- /// <param name="vec">The input vector</param>
- /// <param name="result">The normalized vector</param>
- public static void Normalize(ref IntVector2 vec, out IntVector2 result)
- {
- result.X = vec.X / vec.Length;
- result.Y = vec.Y / vec.Length;
- }
- #endregion
- #region NormalizeFast
- /// <summary>
- /// Scale a vector to approximately unit length
- /// </summary>
- /// <param name="vec">The input vector</param>
- /// <returns>The normalized vector</returns>
- public static IntVector2 NormalizeFast(IntVector2 vec)
- {
- int scale = (int) MathHelper.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y);
- vec.X *= scale;
- vec.Y *= scale;
- return vec;
- }
- /// <summary>
- /// Scale a vector to approximately unit length
- /// </summary>
- /// <param name="vec">The input vector</param>
- /// <param name="result">The normalized vector</param>
- public static void NormalizeFast(ref IntVector2 vec, out IntVector2 result)
- {
- int scale = (int) MathHelper.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y);
- result.X = vec.X * scale;
- result.Y = vec.Y * scale;
- }
- #endregion
- #region Dot
- /// <summary>
- /// Calculate the dot (scalar) product of two vectors
- /// </summary>
- /// <param name="left">First operand</param>
- /// <param name="right">Second operand</param>
- /// <returns>The dot product of the two inputs</returns>
- public static int Dot(IntVector2 left, IntVector2 right)
- {
- return left.X * right.X + left.Y * right.Y;
- }
- /// <summary>
- /// Calculate the dot (scalar) product of two vectors
- /// </summary>
- /// <param name="left">First operand</param>
- /// <param name="right">Second operand</param>
- /// <param name="result">The dot product of the two inputs</param>
- public static void Dot(ref IntVector2 left, ref IntVector2 right, out int result)
- {
- result = left.X * right.X + left.Y * right.Y;
- }
- #endregion
- #region Lerp
- /// <summary>
- /// Returns a new Vector that is the linear blend of the 2 given Vectors
- /// </summary>
- /// <param name="a">First input vector</param>
- /// <param name="b">Second input vector</param>
- /// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
- /// <returns>a when blend=0, b when blend=1, and a linear combination otherwise</returns>
- public static IntVector2 Lerp(IntVector2 a, IntVector2 b, int blend)
- {
- a.X = blend * (b.X - a.X) + a.X;
- a.Y = blend * (b.Y - a.Y) + a.Y;
- return a;
- }
- /// <summary>
- /// Returns a new Vector that is the linear blend of the 2 given Vectors
- /// </summary>
- /// <param name="a">First input vector</param>
- /// <param name="b">Second input vector</param>
- /// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
- /// <param name="result">a when blend=0, b when blend=1, and a linear combination otherwise</param>
- public static void Lerp(ref IntVector2 a, ref IntVector2 b, int blend, out IntVector2 result)
- {
- result.X = blend * (b.X - a.X) + a.X;
- result.Y = blend * (b.Y - a.Y) + a.Y;
- }
- #endregion
- #region Barycentric
- /// <summary>
- /// Interpolate 3 Vectors using Barycentric coordinates
- /// </summary>
- /// <param name="a">First input Vector</param>
- /// <param name="b">Second input Vector</param>
- /// <param name="c">Third input Vector</param>
- /// <param name="u">First Barycentric Coordinate</param>
- /// <param name="v">Second Barycentric Coordinate</param>
- /// <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>
- public static IntVector2 BaryCentric(IntVector2 a, IntVector2 b, IntVector2 c, int u, int v)
- {
- return a + u * (b - a) + v * (c - a);
- }
- /// <summary>Interpolate 3 Vectors using Barycentric coordinates</summary>
- /// <param name="a">First input Vector.</param>
- /// <param name="b">Second input Vector.</param>
- /// <param name="c">Third input Vector.</param>
- /// <param name="u">First Barycentric Coordinate.</param>
- /// <param name="v">Second Barycentric Coordinate.</param>
- /// <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>
- public static void BaryCentric(ref IntVector2 a, ref IntVector2 b, ref IntVector2 c, int u, int v, out IntVector2 result)
- {
- result = a; // copy
- IntVector2 temp = b; // copy
- Subtract(ref temp, ref a, out temp);
- Multiply(ref temp, u, out temp);
- Add(ref result, ref temp, out result);
- temp = c; // copy
- Subtract(ref temp, ref a, out temp);
- Multiply(ref temp, v, out temp);
- Add(ref result, ref temp, out result);
- }
- #endregion
- #endregion
- #region Operators
- /// <summary>
- /// Adds the specified instances.
- /// </summary>
- /// <param name="left">Left operand.</param>
- /// <param name="right">Right operand.</param>
- /// <returns>Result of addition.</returns>
- public static IntVector2 operator +(IntVector2 left, IntVector2 right)
- {
- left.X += right.X;
- left.Y += right.Y;
- return left;
- }
- /// <summary>
- /// Subtracts the specified instances.
- /// </summary>
- /// <param name="left">Left operand.</param>
- /// <param name="right">Right operand.</param>
- /// <returns>Result of subtraction.</returns>
- public static IntVector2 operator -(IntVector2 left, IntVector2 right)
- {
- left.X -= right.X;
- left.Y -= right.Y;
- return left;
- }
- /// <summary>
- /// Negates the specified instance.
- /// </summary>
- /// <param name="vec">Operand.</param>
- /// <returns>Result of negation.</returns>
- public static IntVector2 operator -(IntVector2 vec)
- {
- vec.X = -vec.X;
- vec.Y = -vec.Y;
- return vec;
- }
- /// <summary>
- /// Multiplies the specified instance by a scalar.
- /// </summary>
- /// <param name="vec">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <returns>Result of multiplication.</returns>
- public static IntVector2 operator *(IntVector2 vec, int scale)
- {
- vec.X *= scale;
- vec.Y *= scale;
- return vec;
- }
- /// <summary>
- /// Multiplies the specified instance by a scalar.
- /// </summary>
- /// <param name="scale">Left operand.</param>
- /// <param name="vec">Right operand.</param>
- /// <returns>Result of multiplication.</returns>
- public static IntVector2 operator *(int scale, IntVector2 vec)
- {
- vec.X *= scale;
- vec.Y *= scale;
- return vec;
- }
- /// <summary>
- /// Divides the specified instance by a scalar.
- /// </summary>
- /// <param name="vec">Left operand</param>
- /// <param name="scale">Right operand</param>
- /// <returns>Result of the division.</returns>
- public static IntVector2 operator /(IntVector2 vec, int scale)
- {
- vec.X = vec.X / scale;
- vec.Y = vec.Y / scale;
- return vec;
- }
- /// <summary>
- /// Compares the specified instances for equality.
- /// </summary>
- /// <param name="left">Left operand.</param>
- /// <param name="right">Right operand.</param>
- /// <returns>True if both instances are equal; false otherwise.</returns>
- public static bool operator ==(IntVector2 left, IntVector2 right)
- {
- return left.Equals(right);
- }
- /// <summary>
- /// Compares the specified instances for inequality.
- /// </summary>
- /// <param name="left">Left operand.</param>
- /// <param name="right">Right operand.</param>
- /// <returns>True if both instances are not equal; false otherwise.</returns>
- public static bool operator !=(IntVector2 left, IntVector2 right)
- {
- return !left.Equals(right);
- }
- #endregion
- #region Overrides
- #region public override string ToString()
- /// <summary>
- /// Returns a System.String that represents the current IntVector2.
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- return String.Format("({0}, {1})", X, Y);
- }
- #endregion
- #region public override int GetHashCode()
- /// <summary>
- /// Returns the hashcode for this instance.
- /// </summary>
- /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
- public override int GetHashCode()
- {
- return X.GetHashCode() ^ Y.GetHashCode();
- }
- #endregion
- #region public override bool Equals(object obj)
- /// <summary>
- /// Indicates whether this instance and a specified object are equal.
- /// </summary>
- /// <param name="obj">The object to compare to.</param>
- /// <returns>True if the instances are equal; false otherwise.</returns>
- public override bool Equals(object obj)
- {
- if (!(obj is IntVector2))
- return false;
- return this.Equals((IntVector2)obj);
- }
- #endregion
- #endregion
- #endregion
- #region IEquatable<IntVector2> Members
- /// <summary>Indicates whether the current vector is equal to another vector.</summary>
- /// <param name="other">A vector to compare with this vector.</param>
- /// <returns>true if the current vector is equal to the vector parameter; otherwise, false.</returns>
- public bool Equals(IntVector2 other)
- {
- return
- X == other.X &&
- Y == other.Y;
- }
- #endregion
- public bool IsEmpty => X == 0 && Y == 0;
- }
- }
|