+ comments to the SM.Base

+ Length, Normalize methods for CVectors
This commit is contained in:
Michel Fedde 2020-12-19 20:49:16 +01:00
parent 5d4b360b05
commit 9b917ac181
18 changed files with 248 additions and 33 deletions

View file

@ -1,20 +1,90 @@
namespace SM.Base.Types
using System;
using OpenTK;
namespace SM.Base.Types
{
/// <summary>
/// A One-dimensional Vector (also known as <see cref="float"/>), in a class.
/// </summary>
public class CVector1
{
/// <summary>
/// X - Component
/// </summary>
public float X { get; set; }
/// <summary>
/// The length/magnitute of the vector.
/// </summary>
public float Length => GetLength();
/// <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>
public float LengthSquared => GetLength(true);
/// <summary>
/// Creates a class vector
/// </summary>
/// <param name="x">X-Component</param>
public CVector1(float x)
{
X = x;
}
/// <summary>
/// Sets the X-Component.
/// </summary>
/// <param name="x">X-Component</param>
public virtual void Set(float x)
{
X = x;
}
/// <summary>
/// Get the length of the vector.
/// </summary>
/// <param name="squared">If true, it will return the squared product.</param>
/// <returns></returns>
public float GetLength(bool squared = false)
{
float length = GetLengthProcess();
if (squared) return length;
return (float) Math.Sqrt(length);
}
/// <summary>
/// Normalizes the vector.
/// </summary>
public void Normalize()
{
float length = GetLength();
NormalizationProcess(length);
}
/// <summary>
/// Conversion into <see cref="float"/>
/// </summary>
public static implicit operator float(CVector1 vector1) => vector1.X;
/// <summary>
/// Conversion from <see cref="float"/> to One-dimensional Vector.
/// </summary>
/// <param name="f"></param>
/// <returns></returns>
public static implicit operator CVector1(float f) => new CVector1(f);
private protected virtual float GetLengthProcess()
{
return X * X;
}
private protected virtual void NormalizationProcess(float length)
{
X *= length;
}
}
}

View file

@ -2,38 +2,81 @@
namespace SM.Base.Types
{
/// <summary>
/// A two-dimensional vector.
/// </summary>
public class CVector2 : CVector1
{
/// <summary>
/// Y-component
/// </summary>
public float Y { get; set; }
/// <summary>
/// Creates a vector, where each component is the same value.
/// </summary>
/// <param name="uniform">The Value</param>
public CVector2(float uniform) : base(uniform)
{
Y = uniform;
}
/// <summary>
/// Creates a vector
/// </summary>
public CVector2(float x, float y) : base(x)
{
Y = y;
}
public virtual void Set(float uniform)
private protected override float GetLengthProcess()
{
return base.GetLengthProcess() + Y * Y;
}
private protected override void NormalizationProcess(float length)
{
base.NormalizationProcess(length);
Y *= length;
}
/// <summary>
/// Sets each component to the same value
/// </summary>
/// <param name="uniform"></param>
public override void Set(float uniform)
{
base.Set(uniform);
Y = uniform;
}
/// <summary>
/// Sets each component to the <see cref="Vector2"/> counter-part.
/// </summary>
/// <param name="vector"></param>
public void Set(Vector2 vector)
{
Set(vector.X, vector.Y);
}
/// <summary>
/// Sets the a own value to each component.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public void Set(float x, float y)
{
base.Set(x);
Y = y;
}
/// <summary>
/// Converts to <see cref="Vector2"/>
/// </summary>
public static implicit operator Vector2(CVector2 vector2) => new Vector2(vector2.X, vector2.Y);
/// <summary>
/// Converts from <see cref="Vector2"/> to <see cref="CVector2"/>.
/// </summary>
public static implicit operator CVector2(Vector2 vector2) => new CVector2(vector2.X, vector2.Y);
}
}

View file

@ -2,38 +2,74 @@
namespace SM.Base.Types
{
/// <summary>
/// A three-dimensional vector.
/// </summary>
public class CVector3 : CVector2
{
/// <summary>
/// Z-component
/// </summary>
public float Z { get; set; }
/// <summary>
/// Creates a vector, where each component is the same value.
/// </summary>
/// <param name="uniform">The Value</param>
public CVector3(float uniform) : base(uniform)
{
Z = uniform;
}
/// <summary>
/// Creates a vector
/// </summary>
public CVector3(float x, float y, float z) : base(x, y)
{
Z = z;
}
private protected override float GetLengthProcess()
{
return base.GetLengthProcess() + Z * Z;
}
private protected override void NormalizationProcess(float length)
{
base.NormalizationProcess(length);
Z *= length;
}
/// <inheritdoc />
public override void Set(float uniform)
{
base.Set(uniform);
Z = uniform;
}
/// <summary>
/// Sets the a own value to each component.
/// </summary>
public void Set(float x, float y, float z)
{
base.Set(x,y);
Z = z;
}
/// <summary>
/// Sets each component to the <see cref="Vector3"/> counter-part.
/// </summary>
/// <param name="vector"></param>
public void Set(Vector3 vector)
{
Set(vector.X, vector.Y, vector.Z);
}
/// <summary>
/// Converts to <see cref="Vector3"/>
/// </summary>
public static implicit operator Vector3(CVector3 vector) => new Vector3(vector.X, vector.Y, vector.Z);
/// <summary>
/// Converts from <see cref="Vector3"/> to <see cref="CVector3"/>.
/// </summary>
public static implicit operator CVector3(Vector3 vector) => new CVector3(vector.X, vector.Y, vector.Z);
}
}