mirror of
https://github.com/Ryujinx/Opentk.git
synced 2025-01-22 23:41:11 +00:00
Vector23 math and operator overloads.
This commit is contained in:
parent
61102b359d
commit
4b29b626b8
|
@ -17,16 +17,22 @@ namespace OpenTK.Math
|
|||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Vector2
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// The X coordinate of the Vector2.
|
||||
/// The X component of the Vector2.
|
||||
/// </summary>
|
||||
public float X;
|
||||
|
||||
/// <summary>
|
||||
/// The Y coordinate of the Vector2.
|
||||
/// The Y component of the Vector2.
|
||||
/// </summary>
|
||||
public float Y;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new Vector2.
|
||||
/// </summary>
|
||||
|
@ -38,6 +44,10 @@ namespace OpenTK.Math
|
|||
Y = y;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Add
|
||||
|
||||
/// <summary>
|
||||
/// Adds the given Vector2 to the current Vector2.
|
||||
/// </summary>
|
||||
|
@ -68,6 +78,10 @@ namespace OpenTK.Math
|
|||
return new Vector4(X + right.X, Y + right.Y, right.Z, right.W);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sub
|
||||
|
||||
/// <summary>
|
||||
/// Subtracts the given Vector2 from the current Vector2.
|
||||
/// </summary>
|
||||
|
@ -98,8 +112,12 @@ namespace OpenTK.Math
|
|||
return new Vector4(X - right.X, Y - right.Y, -right.Z, -right.W);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Dot
|
||||
|
||||
/// <summary>
|
||||
/// Computes the dot product between the curren Vector2 and the given Vector2.
|
||||
/// Computes the dot product between the current Vector2 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>
|
||||
|
@ -109,7 +127,7 @@ namespace OpenTK.Math
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes the dot product between the curren Vector2 and the given Vector3.
|
||||
/// Computes the dot product between the current Vector2 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>
|
||||
|
@ -119,7 +137,7 @@ namespace OpenTK.Math
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes the dot product between the curren Vector2 and the given Vector4.
|
||||
/// Computes the dot product between the current Vector2 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>
|
||||
|
@ -128,6 +146,10 @@ namespace OpenTK.Math
|
|||
return X * right.X + Y * right.Y;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Operator overloads
|
||||
|
||||
public static Vector2 operator +(Vector2 left, Vector2 right)
|
||||
{
|
||||
return left.Add(right);
|
||||
|
@ -157,5 +179,13 @@ namespace OpenTK.Math
|
|||
{
|
||||
return left.Sub(right);
|
||||
}
|
||||
|
||||
[CLSCompliant(false)]
|
||||
unsafe public static implicit operator float*(Vector2 v)
|
||||
{
|
||||
return &v.X;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,16 +11,238 @@ using System.Runtime.InteropServices;
|
|||
|
||||
namespace OpenTK.Math
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a three-dimensional vector.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Vector3
|
||||
{
|
||||
public float X, Y, Z;
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// The X component of the Vector3.
|
||||
/// </summary>
|
||||
public float X;
|
||||
|
||||
/// <summary>
|
||||
/// The Y component of the Vector3.
|
||||
/// </summary>
|
||||
public float Y;
|
||||
|
||||
/// <summary>
|
||||
/// The Z component of the Vector3.
|
||||
/// </summary>
|
||||
public float Z;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new Vector3.
|
||||
/// </summary>
|
||||
/// <param name="x">The x component of the Vector3.</param>
|
||||
/// <param name="y">The y component of the Vector3.</param>
|
||||
/// <param name="z">The z component of the Vector3.</param>
|
||||
public Vector3(float x, float y, float z)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
Z = z;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new Vector3 from the given Vector2.
|
||||
/// </summary>
|
||||
/// <param name="v">The Vector2 to copy components from.</param>
|
||||
public Vector3(Vector2 v)
|
||||
{
|
||||
X = v.X;
|
||||
Y = v.Y;
|
||||
Z = 0.0f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new Vector3 from the given Vector3.
|
||||
/// </summary>
|
||||
/// <param name="v">The Vector3 to copy components from.</param>
|
||||
public Vector3(Vector3 v)
|
||||
{
|
||||
X = v.X;
|
||||
Y = v.Y;
|
||||
Z = v.Z;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new Vector3 from the given Vector4.
|
||||
/// </summary>
|
||||
/// <param name="v">The Vector4 to copy components from.</param>
|
||||
public Vector3(Vector4 v)
|
||||
{
|
||||
X = v.X;
|
||||
Y = v.Y;
|
||||
Z = v.Z;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Add
|
||||
|
||||
/// <summary>
|
||||
/// Adds the given Vector2 to the current Vector3. Z-coordinate remains unaffected.
|
||||
/// </summary>
|
||||
/// <param name="right">The right operand of the addition.</param>
|
||||
/// <returns>A new Vector3 containing the result of the addition.</returns>
|
||||
public Vector3 Add(Vector2 right)
|
||||
{
|
||||
return new Vector3(X + right.X, Y + right.Y, Z);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the given Vector3 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>
|
||||
public Vector3 Add(Vector3 right)
|
||||
{
|
||||
return new Vector3(X + right.X, Y + right.Y, Z + right.Z);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the given Vector4 to the current Vector3. 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, right.W);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sub
|
||||
|
||||
/// <summary>
|
||||
/// Subtracts the given Vector2 from the current Vector3. Z component remains unaffected.
|
||||
/// </summary>
|
||||
/// <param name="right">The right operand of the subtraction.</param>
|
||||
/// <returns>A new Vector3 containing the result of the subtraction.</returns>
|
||||
public Vector3 Sub(Vector2 right)
|
||||
{
|
||||
return new Vector3(X - right.X, Y - right.Y, Z);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subtracts the given Vector3 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>
|
||||
public Vector3 Sub(Vector3 right)
|
||||
{
|
||||
return new Vector3(X - right.X, Y - right.Y, Z - right.Z);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subtracts the given Vector4 from the current Vector3.
|
||||
/// </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, -right.W);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Dot
|
||||
|
||||
/// <summary>
|
||||
/// Computes the dot product between the current Vector3 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 Vector3 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 Vector3 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;
|
||||
}
|
||||
|
||||
#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 Operator overloads
|
||||
|
||||
public static Vector3 operator +(Vector3 left, Vector2 right)
|
||||
{
|
||||
return left.Add(right);
|
||||
}
|
||||
|
||||
public static Vector3 operator +(Vector3 left, Vector3 right)
|
||||
{
|
||||
return left.Add(right);
|
||||
}
|
||||
|
||||
public static Vector4 operator +(Vector3 left, Vector4 right)
|
||||
{
|
||||
return left.Add(right);
|
||||
}
|
||||
|
||||
public static Vector3 operator -(Vector3 left, Vector2 right)
|
||||
{
|
||||
return left.Sub(right);
|
||||
}
|
||||
|
||||
public static Vector3 operator -(Vector3 left, Vector3 right)
|
||||
{
|
||||
return left.Sub(right);
|
||||
}
|
||||
|
||||
public static Vector4 operator -(Vector3 left, Vector4 right)
|
||||
{
|
||||
return left.Sub(right);
|
||||
}
|
||||
|
||||
[CLSCompliant(false)]
|
||||
unsafe public static implicit operator float*(Vector3 v)
|
||||
{
|
||||
return &v.X;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue