Dimension 1.0.3
See the version list below for details.
dotnet add package Dimension --version 1.0.3
NuGet\Install-Package Dimension -Version 1.0.3
<PackageReference Include="Dimension" Version="1.0.3" />
<PackageVersion Include="Dimension" Version="1.0.3" />
<PackageReference Include="Dimension" />
paket add Dimension --version 1.0.3
#r "nuget: Dimension, 1.0.3"
#:package Dimension@1.0.3
#addin nuget:?package=Dimension&version=1.0.3
#tool nuget:?package=Dimension&version=1.0.3
Dimension Library
Content :
IVector
Start from the interface IVector for Vector2 (2D) & Vector3 (3D)
public interface IVector<T> where T : IVector<T>
{
public static T Zero;
public float X { get; set; }
public float Y { get; set; }
public static abstract T operator /(T v, T v2);
public static abstract T operator /(T v, float val);
public static abstract T operator *(T v, T v2);
public static abstract T operator *(T v, float val);
public static abstract T operator +(T v, T v2);
public static abstract T operator +(T v, float val);
public static abstract T operator -(T v, T v2);
public static abstract T operator -(T v, float val);
public static abstract bool operator <(T v, T v2);
public static abstract bool operator >(T v, T v2);
public static abstract bool operator <=(T v, T v2);
public static abstract bool operator >=(T v, T v2);
public float GetSum();
public float GetAbsoluteSum();
public float GetMagnitude();
public T GetSquared();
public T GetAbsolute();
public T NormalizeInBounds(T min, T max);
public T GetNormalizedL1();
public T GetSqrt();
public T GetCubed();
public T Clamp(T min_range,T max_range);
public bool HasZero();
public bool HasNegative();
public bool HasGreater(T vector);
public bool HasEquals(T vector);
}
IVector GetSum method
Returns the total sum of Vector's values
Implementation for Vector2
public readonly float GetSum() => X + Y;
Basic usage
Console.WriteLine(new Vector2(25,-16).GetSum());// we get 9
Implementation for Vector3
public readonly float GetSum() => X + Y + Z;
Basic usage
Console.WriteLine(new Vector3(25,-16, 5).GetSum());// we get 14
IVector GetAbsoluteSum method
Returns the total absolute sum of Vector's values
Implementation for Vector2
public readonly float GetAbsoluteSum() => GetAbsolute().GetSum();
Basic usage
Console.WriteLine(new Vector2(25,-16).GetAbsoluteSum());// we get 41
This method also used in IVector.GetNormilized()
Implementation for Vector3
public readonly float GetAbsoluteSum() => GetAbsolute().GetSum();
Basic usage
Console.WriteLine(new Vector3(25,-16, 5).GetAbsoluteSum());// we get 46
IVector GetMagnitude method
Returns the magnitude of Vector
Implementation for Vector2
public readonly float GetMagnitude() => (float)Math.Sqrt(GetSquared().GetSum());
Basic usage
Console.WriteLine(new Vector2(8,-6).GetAbsoluteSum());// we get 10
This method also used in Vectors.GetMagnitude()
Implementation for Vector3
public readonly float GetMagnitude() => (float)Math.Sqrt(GetSquared().GetSum());
Basic usage
Console.WriteLine(new Vector3(2,4,4).GetAbsoluteSum());// we get 6
IVector GetSquared method
Returns new Vector with squared values
Implementation for Vector2
public readonly Vector2 GetSquared() => this*this;
Basic usage
Console.WriteLine(new Vector2(25,-10).GetSquared());// we get Vector2{625,100}
This method also used in Vectors.GetMagnitude()
Implementation for Vector3
public readonly Vector3 GetSquared() => this*this;
Basic usage
Console.WriteLine(new Vector3(4,-9, 2).GetSquared());// we get Vector3{16,81,4}
IVector GetCubed method
Returns new Vector with cubed values
Implementation for Vector2
public readonly Vector2 GetCubed() => this*this*this;
Basic usage
Console.WriteLine(new Vector2(10,-10).GetCubed());// we get Vector2{1000,1000}
Implementation for Vector3
public readonly Vector3 GetCubed() => this*this*this;
Basic usage
Console.WriteLine(new Vector3(4,-1, 2).GetCubed());// we get Vector3{64,1,8}
IVector GetAbsolute method
Returns new Vector with absolute values
Implementation for Vector2
public readonly Vector2 GetAbsolute() => new(Math.Abs(X),Math.Abs(Y));
Basic usage
Console.WriteLine(new Vector2(25,-10).GetAbsolute());// we get Vector2{25,10}
Implementation for Vector3
public readonly Vector3 GetAbsolute() => new(Math.Abs(X),Math.Abs(Y),Math.Abs(Z));
Basic usage
Console.WriteLine(new Vector3(4,-9, -2).GetAbsolute());// we get Vector3{4,9,2}
IVector GetNormalizedL1 method
Returns vector with normalized l1 values
Implementation for Vector2
public readonly Vector2 GetNormalizedL1()
{
float sum = GetAbsoluteSum();
if (sum == 0)
throw new DivideByZeroException();
return this / sum;
}
Basic usage
Console.WriteLine(new Vector2(20,-40).GetNormalizedL1());// we get Vector2{0.(3),-0.(6)}
Implementation for Vector3
public readonly Vector3 GetNormalizedL1()
{
float sum = GetAbsoluteSum();
if (sum == 0)
throw new DivideByZeroException();
return this / sum;
}
Basic usage
Console.WriteLine(new Vector3(4,-9, -2).GetNormalizedL1());// we get Vector3{0.1(3),-0.6,-0.2(6)}
IVector NormalizeInBounds method
Returns normalized vector between min and max Parameters min - min threshold , max - max threshold
Implementation for Vector2
public readonly Vector2 NormalizeInBounds(Vector2 min, Vector2 max)
{
if (min.HasEqauls(max)) throw new DivideByZeroException("Vectors has equals numbers.");
if (min.HasGreater(max)) throw new ArgumentException("Min cannot be greater then max");
Vector2 clamped = Clamp(min, max);
return new Vector2((clamped.X - min.X) / (max.X - min.X), (clamped.Y - min.Y ) / (max.Y - min.Y));
}
Basic usage
Console.WriteLine(new Vector2(4,5).NormalizeInBounds( new Vector2(2,0) , new Vector2(8,10)));// we get Vector2{0.(3),0.5}
Implementation for Vector3
public readonly Vector3 NormalizeInBounds(Vector3 min, Vector3 max)
{
if (min.HasEqauls(max)) throw new DivideByZeroException("Vectors has equals numbers.");
if (min.HasGreater(max)) throw new ArgumentException("Min cannot be greater then max");
Vector3 clamped = Clamp(min, max);
return new Vector3((clamped.X - min.X) / (max.X - min.X), (clamped.Y - min.Y) / (max.Y - min.Y), (clamped.Z - min.Z) / (max.Z - min.Z));
}
Basic usage
Console.WriteLine(new Vector3(3,2,1).NormalizeInBounds(new Vector3(0,1,1), new Vector3(9,4,10)));// we get Vector3{0.(3),0.(3),0}
IVector GetSqrt method
Returns vector with sqrt values
Implementation for Vector2
public readonly Vector2 GetSqrt()
{
if (HasNegative())
throw new ArgumentException("Cannot calculate from negative number");
return new((float)Math.Sqrt(X), (float)Math.Sqrt(Y));
}
Basic usage
Console.WriteLine(new Vector2(121,40).GetSqrt());// we get Vector2{11,√40}
Implementation for Vector3
public readonly Vector3 GetSqrt()
{
if (HasNegative())
throw new ArgumentException("Cannot calculate sqrt from negative number");
return new((float)Math.Sqrt(X), (float)Math.Sqrt(Y), (float)Math.Sqrt(Z));
}
Basic usage
Console.WriteLine(new Vector3(25,9, 100).GetSqrt());// we get Vector3{5,3,10}
IVector HasZero method
Returns true if Vector has zero among it's values
Implementation for Vector2
public readonly bool HasZero() => X == 0 || Y == 0;
Basic usage
Console.WriteLine(new Vector2(11,40).HasZero());// we get false
Implementation for Vector3
public readonly bool HasZero() => X == 0 || Y == 0 || Z == 0;
Basic usage
Console.WriteLine(new Vector3(44,0, 1).HasZero());// we get true
IVector HasNegative method
Returns true if Vector has negative number among it's values
Implementation for Vector2
public readonly bool HasNegative() => X < 0 || Y < 0 || Z < 0;
Basic usage
Console.WriteLine(new Vector2(104.4f,0).HasNegative());// we get false
Implementation for Vector3
public readonly bool HasNegative() => X < 0 || Y < 0 || Z < 0;
Basic usage
Console.WriteLine(new Vector3(52,0, -100).HasNegative());// we get false
IVector HasEquals method
Returns true if Vector has at least one vector equals to one of given vector parameters : vector - vector with that we compare
Implementation for Vector2
public readonly bool HasEquals(Vector2 vector) => X == vector.X || Y == vector.Y
Basic usage
Console.WriteLine(new Vector2(104.4f,0).HasEquals(new Vector2(104.4f,23.5f))));// we get true
Implementation for Vector3
public readonly bool HasEquals(Vector3 vector) => X == vector.X || Y == vector.Y || Z == vector.Z;
Basic usage
Console.WriteLine(new Vector3(52,0, -100).HasEquals(new Vector3(504,52.4f,-102.1f)));// we get false
IVector HasGreater method
Returns true if Vector has at least one vector greater then one of given vector parameters : vector - vector with that we compare
Implementation for Vector2
public readonly bool HasGreater(Vector2 vector) => X > vector.X || Y > vector.Y;
Basic usage
Console.WriteLine(new Vector2(104.4f,0).HasGreater(new Vector2(104.399f,0)));// we get true
Implementation for Vector3
public readonly bool HasGreater(Vector3 vector) => X > vector.X || Y > vector.Y || Z > vector.Z;
Basic usage
Console.WriteLine(new Vector3(52,0, -100).HasGerater(52,10,100));// we get false
IVector Clamp method
Returns vector with clamped values
parameters : Vector min_range - min ranges by that we clamps, Vector max_range - max ranges, that is this.X = Math.Clamp(min_range.X,max_range.X) here we clamps our value between range_vectors;
Implementation for Vector2
public readonly Vector2 Clamp(Vector2 min_range, Vector2 max_range) => new Vector2(Math.Clamp(X, min_range.X, max_range.X), Math.Clamp(Y, min_range.Y,max_range.Y));
Basic usage
Console.WriteLine(new Vector2(14,2).Clamp(new Vector2(0 , 3)) , new Vector2(10,5));// we get { 10 , 2}
Implementation for Vector3
public readonly Vector3 Clamp(Vector3 min_range, Vector3 max_range) => new Vector3(Math.Clamp(X, min_range.X, max_range.X), Math.Clamp(Y, min_range.Y,max_range.Y), Math.Clamp(Z, min_range.Z, max_range.Z));
Basic usage
Console.WriteLine(new Vector3(52,33,-4).Clamp(new Vector3(50,40.4f,-10), new Vector3(100,50.4f,-5)));// we get { 52, 40.4f,-5}
Vectors
static class to comfortablier use Vectors, has very useful Methods
public static class Vectors
{
public static float GetMagnitude<T>(T origin, T target) where T : IVector<T> => (origin-target).GetMagnitude();
public static T GetDirection<T>(T origin, T target) where T : IVector<T> => (target-origin).GetNormalizedL1();
public static float GetDot<T>(T vector1, T vector2) where T : IVector<T> => (vector1 * vector2).GetSum();
public static T GetCenter<T>(IEnumerable<T> vectors) where T : IVector<T> , new()
{
/// <summary> Return Vector center of given vectors </summary>
if (!vectors.Any())
throw new ArgumentException();
int count = 0;
T total_sum = new();
foreach(var vector in vectors)
{
total_sum = (total_sum + vector);
count++;
}
return total_sum / count;
}
public static T GetNormalizedInBounds<T>(T min, T max, float value) where T : IVector<T>
{
/// <summary> Return Vector that was normalized between min and max </summary>
if (min.HasEquals(max)) throw new DivideByZeroException("Vectors has equals numbers.");
if (min.HasGreater(max)) throw new ArgumentException("Min cannot be greater then max");
Math.Clamp(value, 0, 1);
return min+(max-min)*value;
}
}
Vectors GetMagnitude method
return : magnitude of given vectors
parameters : Vector origin , Vector target
usages :
Console.WriteLine(Vectors.GetMagnitude(new Vector3(1, 2, 4), new Vector3(2, 2, 2)));//we get √5
Console.WriteLine(Vectors.GetMagnitude(new Vector2(3,15), new Vector2(6,5)));//we get √109
Vectors GetDirection method
return : normalized direction of two Vectors parameters : Vector origin , Vector target
usages :
Console.WriteLine(Vectors.GetDirection(new Vector3(1, 2, 4), new Vector3(2, 2, 2)));//we get {0.(3),0,0.(6)}
Console.WriteLine(Vectors.GetDirection(new Vector2(3,15), new Vector2(6,5)));//we get {0.23,0.77}
Vectors GetCenter method
return : normalized direction of two Vectors parameters : IEnumberable of Vectors
usages :
Console.WriteLine(Vectors.GetCenter(new List<Vector2>() { new Vector2(11, 1), new Vector2(33, 3) }));//we get {22,2}
Vectors GetDot method
return : returns dot of two given vectors parameters : vector1 - first vector vector2 second vector
usages :
Console.WriteLine(Vectors.GetDot(new Vector3(9,6,11.4f) , new Vector3(5.4f,3,6)));//we get {135}
Vectors GetNormalizedInBounds method
return : initial vector that was normalized between max and min parameters : Vector min - min threshold , Vector max - max threshold , float value - value must be clamped from 0 to 1
usages :
Console.WriteLine(Vectors.GetNormalizedInBounds(new Vector2(15,52) , new Vector2(45,56),0.5));//we get {30,54}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Dimension:
Package | Downloads |
---|---|
GraphFunctions
.Net Packege for creating and building Graphical Functions |
GitHub repositories
This package is not used by any popular GitHub repositories.
New : Vectors.GetNormalizedInBounds(), Vectors.GetDot() , IVector.HasEquals() ,IVector.HasGreater(), IVector.Clamp() , IVector, NormalizedInBounds() , IVector.Compare operators ;
Rewrote : IVector.GetNormalized() -> IVectors.GetNormalizedL1();
Fixed : IVector.GetMagnitude();