Dimension 1.0.5
See the version list below for details.
dotnet add package Dimension --version 1.0.5
NuGet\Install-Package Dimension -Version 1.0.5
<PackageReference Include="Dimension" Version="1.0.5" />
<PackageVersion Include="Dimension" Version="1.0.5" />
<PackageReference Include="Dimension" />
paket add Dimension --version 1.0.5
#r "nuget: Dimension, 1.0.5"
#:package Dimension@1.0.5
#addin nuget:?package=Dimension&version=1.0.5
#tool nuget:?package=Dimension&version=1.0.5
Dimension Library
Content :
- Manual
- interface IVector
- IVector.GetSum()
- IVector.GetAbsoluteSum()
- IVector.GetMagnitude()
- IVector.GetSquared()
- IVector.GetAbsolute()
- IVector.GetNormilizedL1()
- IVector.GetNormilizeInBounds()
- IVector.GetSqrt()
- IVector.HasZero()
- IVector.HasNegative()
- IVector.HasEquals()
- IVector.HasGreater()
- IVector.Clamp()
- IVector.ToString()
- IVector.FromString()
- IVector.GetRandom()
- IVector.GetWithEquals()
- IVector.ToArray()
- IVector.FromArray()
- static class Vectors
- interface IVector
IVector
Start from the interface IVector for Vector2 (2D) & Vector3 (3D)
public interface IVector<T> where T : IVector<T>
{
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);
public string ToString(char first_bracket, char second_bracket, char separator = ';');
public abstract static T FromString(string str, char first_bracket, char second_bracket, char separator = ';');
public float[] ToArray();
public abstract static T FromArray(float[] array);
public abstract static T GetRandom(T min , T max);
public abstract static T GetRandom(float min , float max);
public abstract static T GetWithEquals(float value);
}
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}
IVector ToString method
Returns string converted from vector parameters : char beginning bracket , ending bracket and char separator
Implementation for Vector2
public readonly string ToString(char first_bracket, char second_bracket, char separator = ';') => first_bracket + " " + X + separator + " " + Y + second_bracket;
Basic usage
ToIbxVector(Vector2 vector)
{
return vector.ToString('(' ,')');
}
Console.WriteLine(new Vector2(-2.445, 53.41)); // we get (-2.445, 53.41)
Implementation for Vector3
public readonly string ToString(char first_bracket, char second_bracket, char divider = ',') => first_bracket + " " + X + divider + " " + Y + divider + " " + Z + second_bracket;
Basic usage
ToIbxVector(Vector3 vector)
{
return vector.ToString('(' ,')');
}
Console.WriteLine(new Vector2(-1; 0,4; 32,43)); // we get (-1; 0,4; 32,43)
IVector FromString method
Returns vector converted from string parameters : char beginning bracket , ending bracket and char separator
Implementation for Vector2
public static Vector2 FromString(string str, char first_bracket, char second_bracket, char separator = ';')
{
str = str.Trim(first_bracket, second_bracket);
string[] splited = str.Split(separator);
return FromArray(str.Trim(first_bracket, second_bracket).Split(separator).Select(float.Parse).ToArray());
}
Basic usage
Console.WriteLine(Vector2.FromString("(-14,23;1,54)" , '(' ,')')); // we get Vector2 {-14,23;1,54}
Implementation for Vector3
public static Vector3 FromString(string str, char first_bracket, char second_bracket, char separator = ';')
{
str = str.Trim(first_bracket, second_bracket);
string[] splited = str.Split(separator);
return FromArray(str.Trim(first_bracket, second_bracket).Split(separator).Select(float.Parse).ToArray());
}
Basic usage
Console.WriteLine(Vector2.FromString("(543,543;-71,61;0,44)" , '(' ,')' )); // we get Vector2 {543,543;-71,61;0,44}
IVector GetRandom method
Returns random vector between min and max
parameters of first reloading : Vector min - every value is min bound of value in output vector , Vector max - every value is max bound of value in output vector
parameters of second reloading : flaot min , float max - one bound for any of vector
Implementation for Vector2
public static Vector2 GetRandom(float min, float max) => GetRandom(GetWithEquals(min), GetWithEquals(max));
public static Vector2 GetRandom(Vector2 min, Vector2 max)
{
Random random = new Random();
return new(random.Next((int)max.X, (int)min.X) + (float)random.NextDouble(), random.Next((int)max.Y, (int)min.Y) + (float)random.NextDouble());
}
Basic usage
Console.WriteLine(Vector2.GetRandom(new Vector2(4,7) , new Vector2(7 , 10))); // we got sort of Vector2{2,985453 ; 9,314}
Implementation for Vector3
public static Vector3 GetRandom(float min, float max) => GetRandom(GetWithEquals(min), GetWithEquals(max));
public static Vector3 GetRandom(Vector3 min, Vector3 max)
{
Random random = new Random();
return new(random.Next((int)max.X, (int)min.X) + (float)random.NextDouble(), random.Next((int)max.Y, (int)min.Y) + (float)random.NextDouble(), random.Next((int)max.Z, (int)min.Z) + (float)random.NextDouble());
}
Basic usage
Console.WriteLine(Vector3.GetRandom(5, 10)); // we got sort of Vector3{5,4361 ; 6,51234}
IVector ToArray method
Returns array converted from vector parameters : -
Implementation for Vector2
public readonly float[] ToArray()
{
return [X, Y];
}
Basic usage
Debug.WriteLine(new Vector2(2.2f , 54.3f).ToArray()); // we got new Array[2,2f; 54,3f]
Implementation for Vector3
public readonly float[] ToArray()
{
return [X, Y, Z];
}
Basic usage
ToIbxVector(Vector3 vector)
{
return vector.ToString('(' ,')');
}
Console.WriteLine(new Vector2( 0.4, 32.43 , -13.232f).ToArray()); // we get new Array[0,4f;32,43f; -13.232f]
IVector FromArray method
Returns vector converted from array parameters : float array
Implementation for Vector2
public static Vector2 FromArray(float[] array)
{
if (array.Length < 2) throw new ArgumentException(Vectors.ArrayShorterThenVectorValues + typeof(Vector2));
return new(array[0], array[1]);
}
Basic usage
Vector2[] vectors = [46534.34f,55689.25f]
Console.WriteLine(Vector2.FromArray(vectors)); // we got Vector2 {46534,34f;55689,25f}
Implementation for Vector3
public static Vector2 FromArray(float[] array)
{
if (array.Length < 2) throw new ArgumentException(Vectors.ArrayShorterThenVectorValues + typeof(Vector2));
return new(array[0], array[1] , array[2]);
}
Basic usage
Vector3[] vectors = [52.3f,55.3f,-324.4f]
Console.WriteLine(Vector3.FromArray(vectors)); // we got Vector3 {52,3f;55,3f;-324,4f}
IVector GetWithEquals method
Returns vector with the same values parameters : float that gonna be value
Implementation for Vector2
public static Vector2 GetWithEquals(float value) => new(value, value);
Basic usage
Debug.Write(Vector2.GetWithEquals(-0.3f)); // we got Vector2{-0.3f;-0.3f}
Implementation for Vector3
public static Vector3 GetWithEquals(float value) => new(value, value, value);
Basic usage
Debug.Write(Vector3.GetWithEquals(52f)); // we got Vector3{ 52; 52; 52}
Vectors
static class to comfortablier use Vectors, has very useful Methods
public static class Vectors
{
public const string CANNOT_EXTRACT_ROOT_FROM_NEGATIVE_ERROR_MESSAGE = "Cannot extract root from negative number";
public const string MIN_GREATER_THEN_MAX_ERROR_MESSAGE = "Some of min's values is greater then some of max's values";
public const string VECTORS_HAS_EQUALS_NUMBER_ERROR_MESSAGE = "Cannot find normalized in bounds vector becuase vecors has equals numbers";
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;
}
public static T GetNormalized<T>(T first, T second) where T : IVector<T>
{
if (second.Equals(first)) return default;
return (first - second).GetAbsolute()/(first - second).GetMagnitude();
}
}
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}
Vectors GetNormalized method
return : normalized vector that is unit vector
parameters : first and second vectors
usages :
Console.WriteLine(Vectors.GetNormalized(new Vector3(3,0 ,4) , new Vector3(0, 0 ,0);//we get {0.6 , 0 , 0.8}
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 : IVector.GetWithEquals(), IVector.FromString() , IVector.FromArray() ,IVector.ToArray() , IVector.GetRandom()