| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091 |
- #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 Urho
- {
- /// <summary>Represents a 2D vector using two single-precision floating-point numbers.</summary>
- /// <remarks>
- /// The Vector2 structure is suitable for interoperation with unmanaged code requiring two consecutive floats.
- /// </remarks>
- [StructLayout(LayoutKind.Sequential)]
- public struct Vector2 : IEquatable<Vector2>
- {
- #region Fields
- /// <summary>
- /// The X component of the Vector2.
- /// </summary>
- public float X;
- /// <summary>
- /// The Y component of the Vector2.
- /// </summary>
- public float Y;
- #endregion
- #region Constructors
- /// <summary>
- /// Constructs a new Vector2.
- /// </summary>
- /// <param name="x">The x coordinate of the net Vector2.</param>
- /// <param name="y">The y coordinate of the net Vector2.</param>
- public Vector2(float x, float y)
- {
- X = x;
- Y = y;
- }
- /// <summary>
- /// Constructs a new Vector2 from the given Vector2.
- /// </summary>
- /// <param name="v">The Vector2 to copy components from.</param>
- [Obsolete]
- public Vector2(Vector2 v)
- {
- X = v.X;
- Y = v.Y;
- }
- /// <summary>
- /// Constructs a new Vector2 from the given Vector3.
- /// </summary>
- /// <param name="v">The Vector3 to copy components from. Z is discarded.</param>
- [Obsolete]
- public Vector2(Vector3 v)
- {
- X = v.X;
- Y = v.Y;
- }
- /// <summary>
- /// Constructs a new Vector2 from the given Vector4.
- /// </summary>
- /// <param name="v">The Vector4 to copy components from. Z and W are discarded.</param>
- [Obsolete]
- public Vector2(Vector4 v)
- {
- X = v.X;
- Y = 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(Vector2 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 Vector2 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(Vector2 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 Vector2 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(float 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(float f)
- {
- float mult = 1.0f / f;
- this.X *= mult;
- this.Y *= mult;
- }
- #endregion public void Div()
- #region public float Length
- /// <summary>
- /// Gets the length (magnitude) of the vector.
- /// </summary>
- /// <see cref="LengthFast"/>
- /// <seealso cref="LengthSquared"/>
- public float Length
- {
- get
- {
- return (float)System.Math.Sqrt(X * X + Y * Y);
- }
- }
- #endregion
- #region public float 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 float LengthFast
- {
- get
- {
- return 1.0f / MathHelper.InverseSqrtFast(X * X + Y * Y);
- }
- }
- #endregion
- #region public float 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 float LengthSquared
- {
- get
- {
- return X * X + Y * Y;
- }
- }
- #endregion
- #region public Vector2 PerpendicularRight
- /// <summary>
- /// Gets the perpendicular vector on the right side of this vector.
- /// </summary>
- public Vector2 PerpendicularRight
- {
- get
- {
- return new Vector2(Y, -X);
- }
- }
- #endregion
- #region public Vector2 PerpendicularLeft
- /// <summary>
- /// Gets the perpendicular vector on the left side of this vector.
- /// </summary>
- public Vector2 PerpendicularLeft
- {
- get
- {
- return new Vector2(-Y, X);
- }
- }
- #endregion
- #region public void Normalize()
- /// <summary>
- /// Scales the Vector2 to unit length.
- /// </summary>
- public void Normalize()
- {
- float scale = 1.0f / this.Length;
- X *= scale;
- Y *= scale;
- }
- #endregion
- #region public void NormalizeFast()
- /// <summary>
- /// Scales the Vector2 to approximately unit length.
- /// </summary>
- public void NormalizeFast()
- {
- float scale = MathHelper.InverseSqrtFast(X * X + Y * Y);
- X *= scale;
- Y *= scale;
- }
- #endregion
- #region public void Scale()
- /// <summary>
- /// Scales the current Vector2 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(float sx, float 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(Vector2 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 Vector2 scale)
- {
- this.X *= scale.X;
- this.Y *= scale.Y;
- }
- #endregion public void Scale()
- #endregion
- #region Static
- #region Fields
- /// <summary>
- /// Defines a unit-length Vector2 that points towards the X-axis.
- /// </summary>
- public static readonly Vector2 UnitX = new Vector2(1, 0);
- /// <summary>
- /// Defines a unit-length Vector2 that points towards the Y-axis.
- /// </summary>
- public static readonly Vector2 UnitY = new Vector2(0, 1);
- /// <summary>
- /// Defines a zero-length Vector2.
- /// </summary>
- public static readonly Vector2 Zero = new Vector2(0, 0);
- /// <summary>
- /// Defines an instance with all components set to 1.
- /// </summary>
- public static readonly Vector2 One = new Vector2(1, 1);
- /// <summary>
- /// Defines the size of the Vector2 struct in bytes.
- /// </summary>
- public static readonly int SizeInBytes = Marshal.SizeOf(new Vector2());
- #endregion
- #region Obsolete
- #region Sub
- /// <summary>
- /// Subtract one Vector from another
- /// </summary>
- /// <param name="a">First operand</param>
- /// <param name="b">Second operand</param>
- /// <returns>Result of subtraction</returns>
- [Obsolete("Use static Subtract() method instead.")]
- public static Vector2 Sub(Vector2 a, Vector2 b)
- {
- a.X -= b.X;
- a.Y -= b.Y;
- 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>
- [Obsolete("Use static Subtract() method instead.")]
- public static void Sub(ref Vector2 a, ref Vector2 b, out Vector2 result)
- {
- result.X = a.X - b.X;
- result.Y = a.Y - b.Y;
- }
- #endregion
- #region Mult
- /// <summary>
- /// Multiply a vector and a scalar
- /// </summary>
- /// <param name="a">Vector operand</param>
- /// <param name="f">Scalar operand</param>
- /// <returns>Result of the multiplication</returns>
- [Obsolete("Use static Multiply() method instead.")]
- public static Vector2 Mult(Vector2 a, float f)
- {
- a.X *= f;
- a.Y *= f;
- return a;
- }
- /// <summary>
- /// Multiply a vector and a scalar
- /// </summary>
- /// <param name="a">Vector operand</param>
- /// <param name="f">Scalar operand</param>
- /// <param name="result">Result of the multiplication</param>
- [Obsolete("Use static Multiply() method instead.")]
- public static void Mult(ref Vector2 a, float f, out Vector2 result)
- {
- result.X = a.X * f;
- result.Y = a.Y * f;
- }
- #endregion
- #region Div
- /// <summary>
- /// Divide a vector by a scalar
- /// </summary>
- /// <param name="a">Vector operand</param>
- /// <param name="f">Scalar operand</param>
- /// <returns>Result of the division</returns>
- [Obsolete("Use static Divide() method instead.")]
- public static Vector2 Div(Vector2 a, float f)
- {
- float mult = 1.0f / f;
- a.X *= mult;
- a.Y *= mult;
- return a;
- }
- /// <summary>
- /// Divide a vector by a scalar
- /// </summary>
- /// <param name="a">Vector operand</param>
- /// <param name="f">Scalar operand</param>
- /// <param name="result">Result of the division</param>
- [Obsolete("Use static Divide() method instead.")]
- public static void Div(ref Vector2 a, float f, out Vector2 result)
- {
- float mult = 1.0f / f;
- result.X = a.X * mult;
- result.Y = a.Y * mult;
- }
- #endregion
- #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 Vector2 Add(Vector2 a, Vector2 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 Vector2 a, ref Vector2 b, out Vector2 result)
- {
- result = new Vector2(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 Vector2 Subtract(Vector2 a, Vector2 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 Vector2 a, ref Vector2 b, out Vector2 result)
- {
- result = new Vector2(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 Vector2 Multiply(Vector2 vector, float 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 Vector2 vector, float scale, out Vector2 result)
- {
- result = new Vector2(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 Vector2 Multiply(Vector2 vector, Vector2 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 Vector2 vector, ref Vector2 scale, out Vector2 result)
- {
- result = new Vector2(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 Vector2 Divide(Vector2 vector, float 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 Vector2 vector, float scale, out Vector2 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 Vector2 Divide(Vector2 vector, Vector2 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 Vector2 vector, ref Vector2 scale, out Vector2 result)
- {
- result = new Vector2(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 Vector2 ComponentMin(Vector2 a, Vector2 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 Vector2 a, ref Vector2 b, out Vector2 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 Vector2 ComponentMax(Vector2 a, Vector2 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 Vector2 a, ref Vector2 b, out Vector2 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 Vector2 Min(Vector2 left, Vector2 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 Vector2 Max(Vector2 left, Vector2 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 Vector2 Clamp(Vector2 vec, Vector2 min, Vector2 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 Vector2 vec, ref Vector2 min, ref Vector2 max, out Vector2 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 Vector2 Normalize(Vector2 vec)
- {
- float scale = 1.0f / vec.Length;
- vec.X *= scale;
- vec.Y *= scale;
- 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 Vector2 vec, out Vector2 result)
- {
- float scale = 1.0f / vec.Length;
- result.X = vec.X * scale;
- result.Y = vec.Y * scale;
- }
- #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 Vector2 NormalizeFast(Vector2 vec)
- {
- float scale = 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 Vector2 vec, out Vector2 result)
- {
- float scale = 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 float Dot(Vector2 left, Vector2 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 Vector2 left, ref Vector2 right, out float 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 Vector2 Lerp(Vector2 a, Vector2 b, float 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 Vector2 a, ref Vector2 b, float blend, out Vector2 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 Vector2 BaryCentric(Vector2 a, Vector2 b, Vector2 c, float u, float 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 Vector2 a, ref Vector2 b, ref Vector2 c, float u, float v, out Vector2 result)
- {
- result = a; // copy
- Vector2 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
- #region CalculateAngle
- /// <summary>
- /// Calculate the angle (in radians) between the two inputs
- /// </summary>
- /// <param name="left">First operand</param>
- /// <param name="right">Second operand</param>
- /// <returns>The angle (in radians) between the two inputs</returns>
- /// <remarks>Note that the returned angle is never bigger than the constant Pi.</remarks>
- public static float Angle(Vector2 left, Vector2 right)
- {
- return (float)System.Math.Acos(Dot(left, right) / (left.Length * right.Length));
- }
- /// <summary>
- /// Calculate the angle (in radians) between the two inputs
- /// </summary>
- /// <param name="left">First operand</param>
- /// <param name="right">Second operand</param>
- /// <param name="result">The angle (in radians) between the two inputs</param>
- /// <remarks>Note that the returned angle is never bigger than the constant Pi.</remarks>
- public static void Angle(ref Vector2 left, ref Vector2 right, out float result)
- {
- float temp;
- Vector2.Dot(ref left, ref right, out temp);
- result = (float)System.Math.Acos(temp / (left.Length * right.Length));
- }
- #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 Vector2 operator +(Vector2 left, Vector2 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 Vector2 operator -(Vector2 left, Vector2 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 Vector2 operator -(Vector2 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 Vector2 operator *(Vector2 vec, float 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 Vector2 operator *(float scale, Vector2 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 Vector2 operator /(Vector2 vec, float scale)
- {
- float mult = 1.0f / scale;
- vec.X *= mult;
- vec.Y *= mult;
- 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 ==(Vector2 left, Vector2 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 !=(Vector2 left, Vector2 right)
- {
- return !left.Equals(right);
- }
- #endregion
- #region Overrides
- #region public override string ToString()
- /// <summary>
- /// Returns a System.String that represents the current Vector2.
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- return String.Format("({0}, {1})", X, Y);
- }
- public string ToString(int cellSize, int precision = 2, string separator = " | ")
- {
- return ($"{separator}{MathHelper.ToFixedSizeString(X, cellSize, precision)}" +
- $"{separator}{MathHelper.ToFixedSizeString(Y, cellSize, precision)}{separator}").Trim();
- }
- #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 Vector2))
- return false;
- return this.Equals((Vector2)obj);
- }
- #endregion
- #endregion
- #endregion
- #region IEquatable<Vector2> 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(Vector2 other)
- {
- return
- X == other.X &&
- Y == other.Y;
- }
- #endregion
- }
- }
|