Complete Vector2, Vector3 and Vector4 structs.

This commit is contained in:
the_fiddler 2007-09-25 15:46:52 +00:00
parent 38d779eb1a
commit 038f9a4b08
3 changed files with 456 additions and 46 deletions

View file

@ -44,8 +44,40 @@ namespace OpenTK.Math
Y = y;
}
/// <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;
}
#endregion
#region Functions
#region Add
/// <summary>
@ -148,6 +180,67 @@ namespace OpenTK.Math
#endregion
#region public float Length
/// <summary>
/// Gets the length of the vector.
/// </summary>
public float Length
{
get
{
return (float)System.Math.Sqrt(this.LengthSquared);
}
}
#endregion
#region public float LengthSquared
/// <summary>
/// Gets the square of the vector length.
/// </summary>
public float LengthSquared
{
get
{
return X * X + Y * Y;
}
}
#endregion
#region public Vector2 Normalize()
/// <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);
}
#endregion
#region public Vector2 Scale(float sx, float sy)
/// <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>
/// <returns>A new, scaled Vector2.</returns>
public Vector2 Scale(float sx, float sy)
{
return new Vector2(X * sx, Y * sy);
}
#endregion
#endregion
#region Operator overloads
public static Vector2 operator +(Vector2 left, Vector2 right)
@ -187,48 +280,5 @@ namespace OpenTK.Math
}
#endregion
/// <summary>
/// Returns the length of the vector.
/// </summary>
public float Length
{
get
{
return (float)System.Math.Sqrt(this.LengthSquared);
}
}
/// <summary>
/// Returns the square of the vector length.
/// </summary>
public float LengthSquared
{
get
{
return X * X + Y * Y;
}
}
/// <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);
}
/// <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>
/// <returns>A new, scaled Vector2.</returns>
public Vector2 Scale(float sx, float sy)
{
return new Vector2(X * sx, Y * sy);
}
}
}

View file

@ -86,10 +86,12 @@ namespace OpenTK.Math
#endregion
#region Functions
#region Add
/// <summary>
/// Adds the given Vector2 to the current Vector3. Z-coordinate remains unaffected.
/// Adds the given Vector2 to the current Vector3.
/// </summary>
/// <param name="right">The right operand of the addition.</param>
/// <returns>A new Vector3 containing the result of the addition.</returns>
@ -123,7 +125,7 @@ namespace OpenTK.Math
#region Sub
/// <summary>
/// Subtracts the given Vector2 from the current Vector3. Z component remains unaffected.
/// Subtracts the given Vector2 from the current Vector3.
/// </summary>
/// <param name="right">The right operand of the subtraction.</param>
/// <returns>A new Vector3 containing the result of the subtraction.</returns>
@ -205,6 +207,68 @@ namespace OpenTK.Math
#endregion
#region public float Length
/// <summary>
/// Gets the length of the vector.
/// </summary>
public float Length
{
get
{
return (float)System.Math.Sqrt(this.LengthSquared);
}
}
#endregion
#region public float LengthSquared
/// <summary>
/// Gets the square of the vector length.
/// </summary>
public float LengthSquared
{
get
{
return X * X + Y * Y + Z * Z;
}
}
#endregion
#region public Vector3 Normalize()
/// <summary>
/// Scales the Vector3 to unit length.
/// </summary>
/// <returns>The normalized version of the current vector.</returns>
public Vector3 Normalize()
{
float length = this.Length;
return new Vector3(X / length, Y / Length, Z / length);
}
#endregion
#region public Vector3 Scale(float sx, float sy, float sz)
/// <summary>
/// Scales the current Vector3 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>
/// <param name="sz">The scale of the Z component.</param>
/// <returns>A new, scaled Vector3.</returns>
public Vector3 Scale(float sx, float sy, float sz)
{
return new Vector3(X * sx, Y * sy, Z * sz);
}
#endregion
#endregion
#region Operator overloads
public static Vector3 operator +(Vector3 left, Vector2 right)

View file

@ -11,11 +11,45 @@ using System.Runtime.InteropServices;
namespace OpenTK.Math
{
/// <summary>
/// Represents a four-dimensional vector.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Vector4
{
public float X, Y, Z, W;
#region Fields
/// <summary>
/// The X component of the Vector4.
/// </summary>
public float X;
/// <summary>
/// The Y component of the Vector4.
/// </summary>
public float Y;
/// <summary>
/// The Z component of the Vector4.
/// </summary>
public float Z;
/// <summary>
/// The Z component of the Vector4.
/// </summary>
public float W;
#endregion
#region Constructors
/// <summary>
/// Constructs a new Vector4.
/// </summary>
/// <param name="x">The x component of the Vector4.</param>
/// <param name="y">The y component of the Vector4.</param>
/// <param name="z">The z component of the Vector4.</param>
/// <param name="w">The z component of the Vector4.</param>
public Vector4(float x, float y, float z, float w)
{
X = x;
@ -23,5 +57,267 @@ namespace OpenTK.Math
Z = z;
W = w;
}
/// <summary>
/// Constructs a new Vector4 from the given Vector2.
/// </summary>
/// <param name="v">The Vector2 to copy components from.</param>
public Vector4(Vector2 v)
{
X = v.X;
Y = v.Y;
Z = 0.0f;
W = 0.0f;
}
/// <summary>
/// Constructs a new Vector4 from the given Vector3.
/// </summary>
/// <param name="v">The Vector3 to copy components from.</param>
public Vector4(Vector3 v)
{
X = v.X;
Y = v.Y;
Z = v.Z;
W = 0.0f;
}
/// <summary>
/// Constructs a new Vector4 from the given Vector4.
/// </summary>
/// <param name="v">The Vector4 to copy components from.</param>
public Vector4(Vector4 v)
{
X = v.X;
Y = v.Y;
Z = v.Z;
W = v.W;
}
#endregion
#region Functions
#region Add
/// <summary>
/// Adds the given Vector2 to the current Vector4.
/// </summary>
/// <param name="right">The right operand of the addition.</param>
/// <returns>A new Vector4 containing the result of the addition.</returns>
public Vector4 Add(Vector2 right)
{
return new Vector4(X + right.X, Y + right.Y, Z, W);
}
/// <summary>
/// Adds the given Vector3 to the current Vector4.
/// </summary>
/// <param name="right">The right operand of the addition.</param>
/// <returns>A new Vector4 containing the result of the addition.</returns>
public Vector4 Add(Vector3 right)
{
return new Vector4(X + right.X, Y + right.Y, Z + right.Z, W);
}
/// <summary>
/// Adds the given Vector4 to the current Vector4. W-coordinate remains unaffected.
/// </summary>
/// <param name="right">The right operand of the addition.</param>
/// <returns>A new Vector4 containing the result of the addition.</returns>
public Vector4 Add(Vector4 right)
{
return new Vector4(X + right.X, Y + right.Y, Z + right.Z, W + right.W);
}
#endregion
#region Sub
/// <summary>
/// Subtracts the given Vector2 from the current Vector4.
/// </summary>
/// <param name="right">The right operand of the subtraction.</param>
/// <returns>A new Vector4 containing the result of the subtraction.</returns>
public Vector4 Sub(Vector2 right)
{
return new Vector4(X - right.X, Y - right.Y, Z, W);
}
/// <summary>
/// Subtracts the given Vector3 from the current Vector4.
/// </summary>
/// <param name="right">The right operand of the subtraction.</param>
/// <returns>A new Vector4 containing the result of the subtraction.</returns>
public Vector4 Sub(Vector3 right)
{
return new Vector4(X - right.X, Y - right.Y, Z - right.Z, W);
}
/// <summary>
/// Subtracts the given Vector4 from the current Vector4.
/// </summary>
/// <param name="right">The right operand of the subtraction.</param>
/// <returns>A new Vector4 containing the result of the subtraction.</returns>
public Vector4 Sub(Vector4 right)
{
return new Vector4(X - right.X, Y - right.Y, Z - right.Z, W - right.W);
}
#endregion
#region Dot
/// <summary>
/// Computes the dot product between the current Vector4 and the given Vector2.
/// </summary>
/// <param name="right">The right operand of the dot product.</param>
/// <returns>A float containing the result of the dot product.</returns>
public float Dot(Vector2 right)
{
return X * right.X + Y * right.Y;
}
/// <summary>
/// Computes the dot product between the current Vector4 and the given Vector3.
/// </summary>
/// <param name="right">The right operand of the dot product.</param>
/// <returns>A float containing the result of the dot product.</returns>
public float Dot(Vector3 right)
{
return X * right.X + Y * right.Y + Z * right.Z;
}
/// <summary>
/// Computes the dot product between the current Vector4 and the given Vector4.
/// </summary>
/// <param name="right">The right operand of the dot product.</param>
/// <returns>A float containing the result of the dot product.</returns>
public float Dot(Vector4 right)
{
return X * right.X + Y * right.Y + Z * right.Z + W * right.W;
}
#endregion
/*
#region Cross
/// <summary>
/// Computes the cross product between the current and the given Vector3.
/// </summary>
/// <param name="right">The right operand of the cross product</param>
/// <returns>A new Vector3 containing the result of the computation.</returns>
public Vector3 Cross(Vector3 right)
{
return new Vector3(
Y * right.Z - Z * right.Y,
Z * right.X - X * right.Z,
X * right.Y - Y * right.X);
}
#endregion
*/
#region public float Length
/// <summary>
/// Gets the length of the vector.
/// </summary>
public float Length
{
get
{
return (float)System.Math.Sqrt(this.LengthSquared);
}
}
#endregion
#region public float LengthSquared
/// <summary>
/// Gets the square of the vector length.
/// </summary>
public float LengthSquared
{
get
{
return X * X + Y * Y + Z * Z + W * W;
}
}
#endregion
#region public Vector4 Normalize()
/// <summary>
/// Scales the Vector4 to unit length.
/// </summary>
/// <returns>The normalized version of the current vector.</returns>
public Vector4 Normalize()
{
float length = this.Length;
return new Vector4(X / length, Y / Length, Z / length, W / length);
}
#endregion
#region public Vector2 Scale(float sx, float sy, float sz, float sw)
/// <summary>
/// Scales the current Vector4 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>
/// <param name="sz">The scale of the Z component.</param>
/// <param name="sw">The scale of the Z component.</param>
/// <returns>A new, scaled Vector4.</returns>
public Vector4 Scale(float sx, float sy, float sz, float sw)
{
return new Vector4(X * sx, Y * sy, Z * sz, W * sw);
}
#endregion
#endregion
#region Operator overloads
public static Vector4 operator +(Vector4 left, Vector2 right)
{
return left.Add(right);
}
public static Vector4 operator +(Vector4 left, Vector3 right)
{
return left.Add(right);
}
public static Vector4 operator +(Vector4 left, Vector4 right)
{
return left.Add(right);
}
public static Vector4 operator -(Vector4 left, Vector2 right)
{
return left.Sub(right);
}
public static Vector4 operator -(Vector4 left, Vector3 right)
{
return left.Sub(right);
}
public static Vector4 operator -(Vector4 left, Vector4 right)
{
return left.Sub(right);
}
[CLSCompliant(false)]
unsafe public static implicit operator float*(Vector4 v)
{
return &v.X;
}
#endregion
}
}