2007-09-21 23:38:19 +00:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace OpenTK.Math
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a 2D vector.
|
|
|
|
|
/// </summary>
|
2007-10-24 17:26:05 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// The Vector2 structure is suitable for interoperation with unmanaged code requiring two consecutive floats.
|
|
|
|
|
/// </remarks>
|
2007-09-21 23:38:19 +00:00
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
|
public struct Vector2
|
|
|
|
|
{
|
2007-09-24 20:11:44 +00:00
|
|
|
|
#region Fields
|
|
|
|
|
|
2007-09-21 23:38:19 +00:00
|
|
|
|
/// <summary>
|
2007-09-24 20:11:44 +00:00
|
|
|
|
/// The X component of the Vector2.
|
2007-09-21 23:38:19 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public float X;
|
2007-10-24 17:26:05 +00:00
|
|
|
|
|
2007-09-21 23:38:19 +00:00
|
|
|
|
/// <summary>
|
2007-09-24 20:11:44 +00:00
|
|
|
|
/// The Y component of the Vector2.
|
2007-09-21 23:38:19 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public float Y;
|
|
|
|
|
|
2007-09-24 20:11:44 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
2007-09-21 23:38:19 +00:00
|
|
|
|
/// <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;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-25 15:46:52 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs a new Vector2 from the given Vector2.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="v">The Vector2 to copy components from.</param>
|
|
|
|
|
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>
|
|
|
|
|
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>
|
|
|
|
|
public Vector2(Vector4 v)
|
|
|
|
|
{
|
|
|
|
|
X = v.X;
|
|
|
|
|
Y = v.Y;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-24 20:11:44 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-09-25 15:46:52 +00:00
|
|
|
|
#region Functions
|
|
|
|
|
|
2007-10-24 17:26:05 +00:00
|
|
|
|
#region public Vector2 Add(Vector2 right)
|
2007-09-24 20:11:44 +00:00
|
|
|
|
|
2007-09-21 23:38:19 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds the given Vector2 to the current Vector2.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="right">The right operand of the addition.</param>
|
2007-10-24 17:26:05 +00:00
|
|
|
|
/// <returns>The current Vector2, modified by the operation.</returns>
|
2007-09-21 23:38:19 +00:00
|
|
|
|
public Vector2 Add(Vector2 right)
|
|
|
|
|
{
|
2007-10-24 17:26:05 +00:00
|
|
|
|
this.X = X + right.X;
|
|
|
|
|
this.Y = Y + right.Y;
|
|
|
|
|
return this;
|
2007-09-21 23:38:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-24 20:11:44 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-10-24 17:26:05 +00:00
|
|
|
|
#region public Vector2 Sub(Vector2 right)
|
2007-09-24 20:11:44 +00:00
|
|
|
|
|
2007-09-21 23:38:19 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Subtracts the given Vector2 from the current Vector2.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="right">The right operand of the subtraction.</param>
|
2007-10-24 17:26:05 +00:00
|
|
|
|
/// <returns>The current Vector2, modified by the operation.</returns>
|
2007-09-21 23:38:19 +00:00
|
|
|
|
public Vector2 Sub(Vector2 right)
|
|
|
|
|
{
|
2007-10-24 17:26:05 +00:00
|
|
|
|
this.X = X - right.X;
|
|
|
|
|
this.Y = Y - right.Y;
|
|
|
|
|
return this;
|
2007-09-21 23:38:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-24 20:11:44 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-10-24 17:26:05 +00:00
|
|
|
|
#region public float Dot(Vector2 right)
|
2007-09-24 20:11:44 +00:00
|
|
|
|
|
2007-09-21 23:38:19 +00:00
|
|
|
|
/// <summary>
|
2007-09-24 20:11:44 +00:00
|
|
|
|
/// Computes the dot product between the current Vector2 and the given Vector2.
|
2007-09-21 23:38:19 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="right">The right operand of the dot product.</param>
|
2007-10-24 17:26:05 +00:00
|
|
|
|
/// <returns>A float containing the result of the operation.</returns>
|
2007-09-21 23:38:19 +00:00
|
|
|
|
public float Dot(Vector2 right)
|
|
|
|
|
{
|
|
|
|
|
return X * right.X + Y * right.Y;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-24 17:26:05 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public float Length
|
2007-09-21 23:38:19 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2007-10-24 17:26:05 +00:00
|
|
|
|
/// Gets the length (magnitude) of the vector.
|
2007-09-21 23:38:19 +00:00
|
|
|
|
/// </summary>
|
2007-10-24 17:26:05 +00:00
|
|
|
|
/// <see cref="FastLength"/>
|
|
|
|
|
/// <seealso cref="LengthSquared"/>
|
|
|
|
|
public float Length
|
2007-09-21 23:38:19 +00:00
|
|
|
|
{
|
2007-10-24 17:26:05 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return (float)System.Math.Sqrt(X * X + Y * Y);
|
|
|
|
|
}
|
2007-09-21 23:38:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-24 20:11:44 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-10-24 17:26:05 +00:00
|
|
|
|
#region public float LengthFast
|
2007-09-24 20:18:51 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2007-10-24 17:26:05 +00:00
|
|
|
|
/// Gets an approximation of the vector length (magnitude).
|
2007-09-24 20:18:51 +00:00
|
|
|
|
/// </summary>
|
2007-10-24 17:26:05 +00:00
|
|
|
|
/// <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"/>
|
|
|
|
|
/// <seealso cref="OpenTK.Math.FastSqrt"/>
|
|
|
|
|
public float LengthFast
|
2007-09-24 20:18:51 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2007-10-24 17:26:05 +00:00
|
|
|
|
return OpenTK.Math.SqrtFast(X * X + Y * Y);
|
2007-09-24 20:18:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-25 15:46:52 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public float LengthSquared
|
|
|
|
|
|
2007-09-24 20:18:51 +00:00
|
|
|
|
/// <summary>
|
2007-10-24 17:26:05 +00:00
|
|
|
|
/// Gets the square of the vector length (magnitude).
|
2007-09-24 20:18:51 +00:00
|
|
|
|
/// </summary>
|
2007-10-24 17:26:05 +00:00
|
|
|
|
/// <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="FastLength"/>
|
2007-09-24 20:18:51 +00:00
|
|
|
|
public float LengthSquared
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return X * X + Y * Y;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-25 15:46:52 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public Vector2 Normalize()
|
|
|
|
|
|
2007-09-24 20:18:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Scales the Vector2 to unit length.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The normalized version of the current vector.</returns>
|
|
|
|
|
public Vector2 Normalize()
|
|
|
|
|
{
|
|
|
|
|
float length = this.Length;
|
|
|
|
|
return new Vector2(X / length, Y / Length);
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-25 15:46:52 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-10-24 17:26:05 +00:00
|
|
|
|
#region public Vector2 NormalizeFast()
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Scales the Vector2 to unit length.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The normalized version of the current vector.</returns>
|
|
|
|
|
public Vector2 NormalizeFast()
|
|
|
|
|
{
|
|
|
|
|
float length = this.LengthFast;
|
|
|
|
|
this.X = X / length;
|
|
|
|
|
this.Y = Y / length;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-09-25 15:46:52 +00:00
|
|
|
|
#region public Vector2 Scale(float sx, float sy)
|
|
|
|
|
|
2007-09-24 20:18:51 +00:00
|
|
|
|
/// <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>
|
2007-10-24 17:26:05 +00:00
|
|
|
|
/// <returns>The current Vector2, scaled.</returns>
|
2007-09-24 20:18:51 +00:00
|
|
|
|
public Vector2 Scale(float sx, float sy)
|
|
|
|
|
{
|
2007-10-24 17:26:05 +00:00
|
|
|
|
this.X = X * sx;
|
|
|
|
|
this.Y = Y * sy;
|
|
|
|
|
return this;
|
2007-09-24 20:18:51 +00:00
|
|
|
|
}
|
2007-09-25 15:46:52 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Operator overloads
|
|
|
|
|
|
|
|
|
|
public static Vector2 operator +(Vector2 left, Vector2 right)
|
|
|
|
|
{
|
2007-10-24 17:26:05 +00:00
|
|
|
|
return new Vector2(left).Add(right);
|
2007-09-25 15:46:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector2 operator -(Vector2 left, Vector2 right)
|
|
|
|
|
{
|
2007-10-24 17:26:05 +00:00
|
|
|
|
return new Vector2(left).Sub(right);
|
2007-09-25 15:46:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[CLSCompliant(false)]
|
|
|
|
|
unsafe public static implicit operator float*(Vector2 v)
|
|
|
|
|
{
|
|
|
|
|
return &v.X;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-09-25 21:11:16 +00:00
|
|
|
|
|
2007-10-24 17:26:05 +00:00
|
|
|
|
#region public override string ToString()
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a System.String that represents the current Vector2.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2007-09-25 21:11:16 +00:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return String.Format("({0}, {1})", X, Y);
|
|
|
|
|
}
|
2007-10-24 17:26:05 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
2007-09-21 23:38:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|