C# vector libraryMilliseconds to Time string & Time string to MillisecondsExceptions or something else?Interface for fixed-size container supporting max, min, sumAchieving a rotated Vector magnitudePixelating black and white imagesProject Euler #11 Largest product in a gridTDD - Kata - String CalculatorGeneric matrices implementationBuilding unusual IComparer<T> from expressionsFiltering and Validating value in SETTER

Minimize taxes now that I earn more

What did the controller say during my approach to land (audio clip)?

How is underwater propagation of sound possible?

Why NASA publish all the results/data it gets?

I feel like most of my characters are the same, what can I do?

Is there any reason nowadays to use a neon indicator lamp instead of an LED?

How to manage expenditure when billing cycles and paycheck cycles are not aligned?

Nanomachines exist that enable Axolotl-levels of regeneration - So how can crippling injuries exist as well?

Where Does VDD+0.3V Input Limit Come From on IC chips?

Is this a Sherman, and if so what model?

Do liquid propellant rocket engines experience thrust oscillation?

How does one calculate the distribution of the Matt Colville way of rolling stats?

How to fix folder structure in Windows 7 and 10

C# vector library

Can planetary bodies have a second axis of rotation?

Can Northern Ireland's border issue be solved by repartition?

Did HaShem ever command a Navi (Prophet) to break a law?

Are actors contractually obligated to certain things like going nude/ Sensual Scenes/ Gory Scenes?

The 100 soldier problem

Can multiple wall timers turn lights on or off when required?

Cheap antenna for new HF HAM

Pandas aggregate with dynamic column names

Is Zack Morris's 'time stop' ability in "Saved By the Bell" a supernatural ability?

C# Fastest way to do Array Table Lookup with Integer Index



C# vector library


Milliseconds to Time string & Time string to MillisecondsExceptions or something else?Interface for fixed-size container supporting max, min, sumAchieving a rotated Vector magnitudePixelating black and white imagesProject Euler #11 Largest product in a gridTDD - Kata - String CalculatorGeneric matrices implementationBuilding unusual IComparer<T> from expressionsFiltering and Validating value in SETTER






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








6












$begingroup$


I have been working on a vector library for a little bit now. And at this point the basics are down and I am looking for some tips and advic - this may include:



  1. suggestions for things that might be missing and should be added

  2. improvements for readability and of-course efficiency

  3. if you have an important tip or advice that I didn't think of, please feel free to add it :)

See the code below for a Vector3 struct and a VectorX struct. I have left out the Vector2, 4 for now because those are build in the exact same way as Vector3. VectorX is a struct for vectors which have no predefined length. See code below:



Vector3:



using System;

namespace Development

public struct Vector3 : IEquatable<Vector3>

public float X get; set;
public float Y get; set;
public float Z get; set;
public int VectorCount get return 3;

public Vector3(float value)

X = value;
Y = value;
Z = value;


public Vector3(float x, float y, float z)

X = x;
Y = y;
Z = z;


public Vector3(Vector3 vector)

X = vector.X;
Y = vector.Y;
Z = vector.Z;


public Vector3(float[] arr)

X = arr[0];
Y = arr[1];
Z = arr[2];


public Vector3(float[] arr, int startIndex)

if (startIndex > arr.Length - 3) throw new Exception("start index doesnt leave enough space to fill all vector parameters");
else

X = arr[startIndex];
Y = arr[startIndex + 1];
Z = arr[startIndex + 2];




public override string ToString()

return "<" + X + ", " + Y + ", " + Z + ">";


public float[] ToArray()

return new float[] X, Y, Z ;


public Vector3 UpdateFromArray(float[] arr)

if (arr.Length < 3) throw new Exception("Array is too small to convert to vector");
else

X = arr[0];
Y = arr[1];
Z = arr[2];

return this;


public Vector3 UpdateFromArray(float[] arr, int startIndex)

if (startIndex + 2 > arr.Length - 1) throw new Exception("startindex is too high to fill vector");
else

X = arr[startIndex];
Y = arr[startIndex + 1];
Z = arr[startIndex + 2];

return this;


public bool Equals(Vector3 other) // not implemented

throw new NotImplementedException();


public Vector3 Abs()

X = (X < 0) ? X *= -1 : X;
Y = (Y < 0) ? Y *= -1 : Y;
Z = (Z < 0) ? Z *= -1 : Z;
return this;


public float Length()

return MathF.Sqrt(MathF.Pow(X, 2) + MathF.Pow(Y, 2) + MathF.Pow(Z, 2));


public float Dist(Vector3 other)

return MathF.Sqrt(MathF.Pow((this.X - other.X), 2) + MathF.Pow((this.Y - other.Y), 2) + MathF.Pow((this.Z - other.Z), 2));


public Vector3 Min(Vector3 min)

this.X = (this.X < min.X) ? this.X : min.X;
this.Y = (this.Y < min.Y) ? this.Y : min.Y;
this.Z = (this.Z < min.Z) ? this.Z : min.Z;
return this;


public Vector3 Max(Vector3 max)

this.X = (this.X > max.X) ? this.X : max.X;
this.Y = (this.Y > max.Y) ? this.Y : max.Y;
this.Z = (this.Z > max.Z) ? this.Z : max.Z;
return this;


public Vector3 Clamp(Vector3 min, Vector3 max)

this.X = (this.X < min.X) ? min.X : this.X = (this.X > max.X) ? max.X : this.X;
this.Y = (this.Y < min.Y) ? min.Y : this.Y = (this.Y > max.Y) ? max.Y : this.Y;
this.Z = (this.Z < min.Z) ? min.Z : this.Z = (this.Z > max.Z) ? max.Z : this.Z;
return this;


public Vector3 Lerp(Vector3 to, float amount)

amount = Math.Clamp(amount, 0, 1);
this.X = (this.X - to.X) * amount + to.X;
this.Y = (this.Y - to.Y) * amount + to.Y;
this.Z = (this.Z - to.Z) * amount + to.Z;
return this;


public Vector3 Normalize()

float scalar = 1 / MathF.Sqrt(MathF.Pow(X, 2) + MathF.Pow(Y, 2) + MathF.Pow(Z, 2));
X *= scalar;
Y *= scalar;
Z *= scalar;
return this;


public float Dot(Vector3 other)

return this.X * other.X + this.Y * other.Y + this.Z * other.Z;



public static Vector3 Abs(Vector3 value)

return value.Abs();


public static Vector3 Clamp(Vector3 value, Vector3 min, Vector3 max)

return value.Clamp(min, max);


public static Vector3 Min(Vector3 value, Vector3 min)

return value.Min(min);


public static Vector3 Max(Vector3 value, Vector3 max)

return value.Max(max);


public static float Dist(Vector3 value1, Vector3 value2)

return value1.Dist(value2);


public static float Dot(Vector3 value1, Vector3 value2)

return value1.Dot(value2);


public static float Length(Vector3 value)

return value.Length();


public static Vector3 Lerp(Vector3 from, Vector3 to, float amount)

return from.Lerp(to, amount);


public static Vector3 Normalize(Vector3 value)

return value.Normalize();



public static Vector3 Zero get return new Vector3(0);
public static Vector3 Unit get return new Vector3(1);
public static Vector3 UnitX get return new Vector3(1, 0, 0);
public static Vector3 UnitY get return new Vector3(0, 1, 0);
public static Vector3 UnitZ get return new Vector3(0, 0, 1);


public static Vector3 operator +(Vector3 left, float value)

left.X += value;
left.Y += value;
left.Z += value;
return left;


public static Vector3 operator +(Vector3 left, Vector3 right)

left.X += right.X;
left.Y += right.Y;
left.Z += right.Z;
return left;


public static Vector3 operator -(Vector3 left)

left.X = (-left.X);
left.Y = (-left.Y);
left.Z = (-left.Z);
return left;


public static Vector3 operator -(Vector3 left, float value)

left.X -= value;
left.Y -= value;
left.Z -= value;
return left;


public static Vector3 operator -(Vector3 left, Vector3 right)

left.X -= right.X;
left.Y -= right.Y;
left.Z -= right.Z;
return left;


public static Vector3 operator /(Vector3 left, float value)

left.X /= value;
left.Y /= value;
left.Z /= value;
return left;


public static Vector3 operator /(Vector3 left, Vector3 right)

left.X /= right.X;
left.Y /= right.Y;
left.Z /= right.Z;
return left;


public static Vector3 operator *(Vector3 left, float value)

left.X *= value;
left.Y *= value;
left.Z *= value;
return left;


public static Vector3 operator *(Vector3 left, Vector3 right)

left.X *= right.X;
left.Y *= right.Y;
left.Z *= right.Z;
return left;


// do these size comparisions make sense?
public static bool operator <(Vector3 left, Vector3 right)

return (left.Length() < right.Length()) ? true : false;


public static bool operator >(Vector3 left, Vector3 right)

return (left.Length() > right.Length()) ? true : false;


public static bool operator >=(Vector3 left, Vector3 right)

return (left.Length() >= right.Length()) ? true : false;


public static bool operator <=(Vector3 left, Vector3 right)

return (left.Length() <= right.Length()) ? true : false;

// end question

public static bool operator ==(Vector3 left, Vector3 right)

return (left.X == right.X && left.Y == right.Y && left.Z == right.Z) ? true : false;


public static bool operator !=(Vector3 left, Vector3 right)






VectorX:



using System;

namespace Development

public struct VectorX : IEquatable<VectorX>

// private float[] VectorValues;
public float[] VectorValues get; set;
public int VectorCount get; private set;

public VectorX(int vectorCount)

VectorValues = new float[vectorCount];
VectorCount = vectorCount;


public VectorX(int vectorCount, float defaultValue)

VectorCount = vectorCount;
VectorValues = new float[vectorCount];
for (int i = 0; i < vectorCount; i++)

VectorValues[i] = defaultValue;




public VectorX(params float[] values)

VectorValues = values;
VectorCount = values.Length;


public VectorX(float[] arr, int startIndex)

VectorCount = arr.Length - 1 - startIndex;
VectorValues = new float[VectorCount];
for (int i = startIndex; i < arr.Length; i++)

VectorValues[i - startIndex] = arr[i];



public VectorX(float[] arr, int startIndex, int endIndex)

VectorCount = endIndex - startIndex;
VectorValues = new float[VectorCount];
for (int i = startIndex; i < endIndex; i++)

VectorValues[i - startIndex] = arr[i];



public VectorX(VectorX vector)

VectorCount = vector.VectorCount;
VectorValues = vector.VectorValues;



public override string ToString()

string ret = "<";
for (int i = 0; i < VectorCount; i++)

if (i == VectorCount - 1) ret += VectorValues[i];
else ret += VectorValues[i] + ", ";

return ret += ">";


public float[] ToArray()

return VectorValues;


public VectorX UpdateFromArray(float[] arr)

if (arr.Length < VectorCount) throw new Exception("Array is too small to convert to vector");
else

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = arr[i];


return this;


public VectorX UpdateFromArray(float[] arr, int startIndex)

if (startIndex + VectorCount > arr.Length - 1) throw new Exception("startindex is too high to fill vector");
else

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = arr[startIndex + i];



return this;


public float GetAt(int pos)


public VectorX SetAt(int pos, float value)

if (pos >= VectorCount

public bool Equals(VectorX other) // not implemented

throw new NotImplementedException();


public float Length()

float total = 0;
for (int i = 0; i < VectorCount; i++)

total += MathF.Pow(VectorValues[i], 2);

return MathF.Sqrt(total);


public VectorX Normalize()

float scalar = 1 / this.Length();
for (int i = 0; i < VectorCount; i++)

VectorValues[i] *= scalar;

return this;


public VectorX Abs()

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = (VectorValues[i] < 0) ? VectorValues[i] *= -1 : VectorValues[i];

return this;


public float Dist(VectorX other)

if (other.VectorCount != this.VectorCount) throw new Exception("Vectors are not of same length");
else

float sqrtValue = 0;
for (int i = 0; i < this.VectorCount; i++)

sqrtValue += MathF.Pow((this.VectorValues[i] - other.GetAt(i)), 2);

return MathF.Sqrt(sqrtValue);



public VectorX Min(VectorX min)

if (min.VectorCount != this.VectorCount) throw new Exception("Vectors are not of same length");
else

for (int i = 0; i < this.VectorCount; i++)

this.VectorValues[i] = (this.VectorValues[i] > min.GetAt(i)) ? this.VectorValues[i] : min.GetAt(i);


return this;


public VectorX Max(VectorX max)

if (max.VectorCount != this.VectorCount) throw new Exception("Vectors are not of same length");
else

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = (VectorValues[i] < max.GetAt(i)) ? VectorValues[i] : max.GetAt(i);


return this;


public VectorX Clamp(VectorX min, VectorX max)


public VectorX Lerp(VectorX to, float amount)

if (to.VectorCount != VectorCount) throw new Exception("Vectors have different length");
else

amount = Math.Clamp(amount, 0, 1);
for (int i = 0; i < VectorCount; i++)

VectorValues[i] = (VectorValues[i] - to.GetAt(i)) * amount + to.GetAt(i);

return this;




public float Dot(VectorX other)

if(other.VectorCount != VectorCount) throw new Exception("Vectors are not the same length");
else
float d = 0;
for (int i = 0; i < VectorCount; i++)

d += VectorValues[i] * other.GetAt(i);

return d;





public static VectorX NewUnit(int size)

return new VectorX(size, 1);

public static VectorX NewUnitIndex(int size, int index)

VectorX vec = new VectorX(size);
vec.SetAt(index, 1);
return vec;



public static float Length(VectorX vector)

return vector.Length();


public static VectorX Normalize(VectorX vector)

return vector.Normalize();


public static VectorX Abs(VectorX vector)

return vector.Abs();


public static float Dist(VectorX vector1, VectorX vector2)

return vector1.Dist(vector2);


public static VectorX Min(VectorX vector, VectorX min)

return vector.Min(min);


public static VectorX Max(VectorX vector, VectorX min)

return vector.Max(min);


public static VectorX Clamp(VectorX vector, VectorX min, VectorX max)

return vector.Clamp(min, max);


public static VectorX Lerp(VectorX vector, VectorX to, float amount)

return vector.Lerp(to, amount);


public static float Dot(VectorX vector1, VectorX vector2)

return vector1.Dot(vector2);



public static VectorX operator +(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) + value);

return left;


public static VectorX operator +(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) + right.GetAt(i));

return left;



public static VectorX operator -(VectorX left)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) * -1);

return left;


public static VectorX operator -(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) - value);

return left;


public static VectorX operator -(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) - right.GetAt(i));

return left;



public static VectorX operator /(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) / value);

return left;


public static VectorX operator /(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) / right.GetAt(i));

return left;



public static VectorX operator *(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) * value);

return left;


public static VectorX operator *(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) * right.GetAt(i));

return left;



// do these size comparisions make sense?
public static bool operator <(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() < right.Length()) ? true : false;


public static bool operator >(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() > right.Length()) ? true : false;


public static bool operator >=(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() >= right.Length()) ? true : false;


public static bool operator <=(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() <= right.Length()) ? true : false;

// end question

public static bool operator ==(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else

for (int i = 0; i < left.VectorCount; i++)

if(left.GetAt(i) != right.GetAt(i)) return false;

return true;



public static bool operator !=(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else

for (int i = 0; i < left.VectorCount; i++)

if(left.GetAt(i) != right.GetAt(i)) return true;

return false;













share|improve this question









New contributor



FutureCake is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$













  • $begingroup$
    This seems alot like a javascript library (three.js) ported to C#.
    $endgroup$
    – dfhwze
    8 hours ago






  • 1




    $begingroup$
    Have you considered using Vector class from System.Numerics?
    $endgroup$
    – Rick Davin
    8 hours ago










  • $begingroup$
    @RickDavin yes i started out with System.Numerics but ran into some limitations, so now i am building my own to circumvent those. But for what i have atm it looks similar to System.Numerics, but i hope to change that soon :)
    $endgroup$
    – FutureCake
    7 hours ago

















6












$begingroup$


I have been working on a vector library for a little bit now. And at this point the basics are down and I am looking for some tips and advic - this may include:



  1. suggestions for things that might be missing and should be added

  2. improvements for readability and of-course efficiency

  3. if you have an important tip or advice that I didn't think of, please feel free to add it :)

See the code below for a Vector3 struct and a VectorX struct. I have left out the Vector2, 4 for now because those are build in the exact same way as Vector3. VectorX is a struct for vectors which have no predefined length. See code below:



Vector3:



using System;

namespace Development

public struct Vector3 : IEquatable<Vector3>

public float X get; set;
public float Y get; set;
public float Z get; set;
public int VectorCount get return 3;

public Vector3(float value)

X = value;
Y = value;
Z = value;


public Vector3(float x, float y, float z)

X = x;
Y = y;
Z = z;


public Vector3(Vector3 vector)

X = vector.X;
Y = vector.Y;
Z = vector.Z;


public Vector3(float[] arr)

X = arr[0];
Y = arr[1];
Z = arr[2];


public Vector3(float[] arr, int startIndex)

if (startIndex > arr.Length - 3) throw new Exception("start index doesnt leave enough space to fill all vector parameters");
else

X = arr[startIndex];
Y = arr[startIndex + 1];
Z = arr[startIndex + 2];




public override string ToString()

return "<" + X + ", " + Y + ", " + Z + ">";


public float[] ToArray()

return new float[] X, Y, Z ;


public Vector3 UpdateFromArray(float[] arr)

if (arr.Length < 3) throw new Exception("Array is too small to convert to vector");
else

X = arr[0];
Y = arr[1];
Z = arr[2];

return this;


public Vector3 UpdateFromArray(float[] arr, int startIndex)

if (startIndex + 2 > arr.Length - 1) throw new Exception("startindex is too high to fill vector");
else

X = arr[startIndex];
Y = arr[startIndex + 1];
Z = arr[startIndex + 2];

return this;


public bool Equals(Vector3 other) // not implemented

throw new NotImplementedException();


public Vector3 Abs()

X = (X < 0) ? X *= -1 : X;
Y = (Y < 0) ? Y *= -1 : Y;
Z = (Z < 0) ? Z *= -1 : Z;
return this;


public float Length()

return MathF.Sqrt(MathF.Pow(X, 2) + MathF.Pow(Y, 2) + MathF.Pow(Z, 2));


public float Dist(Vector3 other)

return MathF.Sqrt(MathF.Pow((this.X - other.X), 2) + MathF.Pow((this.Y - other.Y), 2) + MathF.Pow((this.Z - other.Z), 2));


public Vector3 Min(Vector3 min)

this.X = (this.X < min.X) ? this.X : min.X;
this.Y = (this.Y < min.Y) ? this.Y : min.Y;
this.Z = (this.Z < min.Z) ? this.Z : min.Z;
return this;


public Vector3 Max(Vector3 max)

this.X = (this.X > max.X) ? this.X : max.X;
this.Y = (this.Y > max.Y) ? this.Y : max.Y;
this.Z = (this.Z > max.Z) ? this.Z : max.Z;
return this;


public Vector3 Clamp(Vector3 min, Vector3 max)

this.X = (this.X < min.X) ? min.X : this.X = (this.X > max.X) ? max.X : this.X;
this.Y = (this.Y < min.Y) ? min.Y : this.Y = (this.Y > max.Y) ? max.Y : this.Y;
this.Z = (this.Z < min.Z) ? min.Z : this.Z = (this.Z > max.Z) ? max.Z : this.Z;
return this;


public Vector3 Lerp(Vector3 to, float amount)

amount = Math.Clamp(amount, 0, 1);
this.X = (this.X - to.X) * amount + to.X;
this.Y = (this.Y - to.Y) * amount + to.Y;
this.Z = (this.Z - to.Z) * amount + to.Z;
return this;


public Vector3 Normalize()

float scalar = 1 / MathF.Sqrt(MathF.Pow(X, 2) + MathF.Pow(Y, 2) + MathF.Pow(Z, 2));
X *= scalar;
Y *= scalar;
Z *= scalar;
return this;


public float Dot(Vector3 other)

return this.X * other.X + this.Y * other.Y + this.Z * other.Z;



public static Vector3 Abs(Vector3 value)

return value.Abs();


public static Vector3 Clamp(Vector3 value, Vector3 min, Vector3 max)

return value.Clamp(min, max);


public static Vector3 Min(Vector3 value, Vector3 min)

return value.Min(min);


public static Vector3 Max(Vector3 value, Vector3 max)

return value.Max(max);


public static float Dist(Vector3 value1, Vector3 value2)

return value1.Dist(value2);


public static float Dot(Vector3 value1, Vector3 value2)

return value1.Dot(value2);


public static float Length(Vector3 value)

return value.Length();


public static Vector3 Lerp(Vector3 from, Vector3 to, float amount)

return from.Lerp(to, amount);


public static Vector3 Normalize(Vector3 value)

return value.Normalize();



public static Vector3 Zero get return new Vector3(0);
public static Vector3 Unit get return new Vector3(1);
public static Vector3 UnitX get return new Vector3(1, 0, 0);
public static Vector3 UnitY get return new Vector3(0, 1, 0);
public static Vector3 UnitZ get return new Vector3(0, 0, 1);


public static Vector3 operator +(Vector3 left, float value)

left.X += value;
left.Y += value;
left.Z += value;
return left;


public static Vector3 operator +(Vector3 left, Vector3 right)

left.X += right.X;
left.Y += right.Y;
left.Z += right.Z;
return left;


public static Vector3 operator -(Vector3 left)

left.X = (-left.X);
left.Y = (-left.Y);
left.Z = (-left.Z);
return left;


public static Vector3 operator -(Vector3 left, float value)

left.X -= value;
left.Y -= value;
left.Z -= value;
return left;


public static Vector3 operator -(Vector3 left, Vector3 right)

left.X -= right.X;
left.Y -= right.Y;
left.Z -= right.Z;
return left;


public static Vector3 operator /(Vector3 left, float value)

left.X /= value;
left.Y /= value;
left.Z /= value;
return left;


public static Vector3 operator /(Vector3 left, Vector3 right)

left.X /= right.X;
left.Y /= right.Y;
left.Z /= right.Z;
return left;


public static Vector3 operator *(Vector3 left, float value)

left.X *= value;
left.Y *= value;
left.Z *= value;
return left;


public static Vector3 operator *(Vector3 left, Vector3 right)

left.X *= right.X;
left.Y *= right.Y;
left.Z *= right.Z;
return left;


// do these size comparisions make sense?
public static bool operator <(Vector3 left, Vector3 right)

return (left.Length() < right.Length()) ? true : false;


public static bool operator >(Vector3 left, Vector3 right)

return (left.Length() > right.Length()) ? true : false;


public static bool operator >=(Vector3 left, Vector3 right)

return (left.Length() >= right.Length()) ? true : false;


public static bool operator <=(Vector3 left, Vector3 right)

return (left.Length() <= right.Length()) ? true : false;

// end question

public static bool operator ==(Vector3 left, Vector3 right)

return (left.X == right.X && left.Y == right.Y && left.Z == right.Z) ? true : false;


public static bool operator !=(Vector3 left, Vector3 right)






VectorX:



using System;

namespace Development

public struct VectorX : IEquatable<VectorX>

// private float[] VectorValues;
public float[] VectorValues get; set;
public int VectorCount get; private set;

public VectorX(int vectorCount)

VectorValues = new float[vectorCount];
VectorCount = vectorCount;


public VectorX(int vectorCount, float defaultValue)

VectorCount = vectorCount;
VectorValues = new float[vectorCount];
for (int i = 0; i < vectorCount; i++)

VectorValues[i] = defaultValue;




public VectorX(params float[] values)

VectorValues = values;
VectorCount = values.Length;


public VectorX(float[] arr, int startIndex)

VectorCount = arr.Length - 1 - startIndex;
VectorValues = new float[VectorCount];
for (int i = startIndex; i < arr.Length; i++)

VectorValues[i - startIndex] = arr[i];



public VectorX(float[] arr, int startIndex, int endIndex)

VectorCount = endIndex - startIndex;
VectorValues = new float[VectorCount];
for (int i = startIndex; i < endIndex; i++)

VectorValues[i - startIndex] = arr[i];



public VectorX(VectorX vector)

VectorCount = vector.VectorCount;
VectorValues = vector.VectorValues;



public override string ToString()

string ret = "<";
for (int i = 0; i < VectorCount; i++)

if (i == VectorCount - 1) ret += VectorValues[i];
else ret += VectorValues[i] + ", ";

return ret += ">";


public float[] ToArray()

return VectorValues;


public VectorX UpdateFromArray(float[] arr)

if (arr.Length < VectorCount) throw new Exception("Array is too small to convert to vector");
else

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = arr[i];


return this;


public VectorX UpdateFromArray(float[] arr, int startIndex)

if (startIndex + VectorCount > arr.Length - 1) throw new Exception("startindex is too high to fill vector");
else

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = arr[startIndex + i];



return this;


public float GetAt(int pos)


public VectorX SetAt(int pos, float value)

if (pos >= VectorCount

public bool Equals(VectorX other) // not implemented

throw new NotImplementedException();


public float Length()

float total = 0;
for (int i = 0; i < VectorCount; i++)

total += MathF.Pow(VectorValues[i], 2);

return MathF.Sqrt(total);


public VectorX Normalize()

float scalar = 1 / this.Length();
for (int i = 0; i < VectorCount; i++)

VectorValues[i] *= scalar;

return this;


public VectorX Abs()

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = (VectorValues[i] < 0) ? VectorValues[i] *= -1 : VectorValues[i];

return this;


public float Dist(VectorX other)

if (other.VectorCount != this.VectorCount) throw new Exception("Vectors are not of same length");
else

float sqrtValue = 0;
for (int i = 0; i < this.VectorCount; i++)

sqrtValue += MathF.Pow((this.VectorValues[i] - other.GetAt(i)), 2);

return MathF.Sqrt(sqrtValue);



public VectorX Min(VectorX min)

if (min.VectorCount != this.VectorCount) throw new Exception("Vectors are not of same length");
else

for (int i = 0; i < this.VectorCount; i++)

this.VectorValues[i] = (this.VectorValues[i] > min.GetAt(i)) ? this.VectorValues[i] : min.GetAt(i);


return this;


public VectorX Max(VectorX max)

if (max.VectorCount != this.VectorCount) throw new Exception("Vectors are not of same length");
else

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = (VectorValues[i] < max.GetAt(i)) ? VectorValues[i] : max.GetAt(i);


return this;


public VectorX Clamp(VectorX min, VectorX max)


public VectorX Lerp(VectorX to, float amount)

if (to.VectorCount != VectorCount) throw new Exception("Vectors have different length");
else

amount = Math.Clamp(amount, 0, 1);
for (int i = 0; i < VectorCount; i++)

VectorValues[i] = (VectorValues[i] - to.GetAt(i)) * amount + to.GetAt(i);

return this;




public float Dot(VectorX other)

if(other.VectorCount != VectorCount) throw new Exception("Vectors are not the same length");
else
float d = 0;
for (int i = 0; i < VectorCount; i++)

d += VectorValues[i] * other.GetAt(i);

return d;





public static VectorX NewUnit(int size)

return new VectorX(size, 1);

public static VectorX NewUnitIndex(int size, int index)

VectorX vec = new VectorX(size);
vec.SetAt(index, 1);
return vec;



public static float Length(VectorX vector)

return vector.Length();


public static VectorX Normalize(VectorX vector)

return vector.Normalize();


public static VectorX Abs(VectorX vector)

return vector.Abs();


public static float Dist(VectorX vector1, VectorX vector2)

return vector1.Dist(vector2);


public static VectorX Min(VectorX vector, VectorX min)

return vector.Min(min);


public static VectorX Max(VectorX vector, VectorX min)

return vector.Max(min);


public static VectorX Clamp(VectorX vector, VectorX min, VectorX max)

return vector.Clamp(min, max);


public static VectorX Lerp(VectorX vector, VectorX to, float amount)

return vector.Lerp(to, amount);


public static float Dot(VectorX vector1, VectorX vector2)

return vector1.Dot(vector2);



public static VectorX operator +(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) + value);

return left;


public static VectorX operator +(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) + right.GetAt(i));

return left;



public static VectorX operator -(VectorX left)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) * -1);

return left;


public static VectorX operator -(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) - value);

return left;


public static VectorX operator -(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) - right.GetAt(i));

return left;



public static VectorX operator /(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) / value);

return left;


public static VectorX operator /(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) / right.GetAt(i));

return left;



public static VectorX operator *(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) * value);

return left;


public static VectorX operator *(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) * right.GetAt(i));

return left;



// do these size comparisions make sense?
public static bool operator <(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() < right.Length()) ? true : false;


public static bool operator >(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() > right.Length()) ? true : false;


public static bool operator >=(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() >= right.Length()) ? true : false;


public static bool operator <=(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() <= right.Length()) ? true : false;

// end question

public static bool operator ==(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else

for (int i = 0; i < left.VectorCount; i++)

if(left.GetAt(i) != right.GetAt(i)) return false;

return true;



public static bool operator !=(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else

for (int i = 0; i < left.VectorCount; i++)

if(left.GetAt(i) != right.GetAt(i)) return true;

return false;













share|improve this question









New contributor



FutureCake is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$













  • $begingroup$
    This seems alot like a javascript library (three.js) ported to C#.
    $endgroup$
    – dfhwze
    8 hours ago






  • 1




    $begingroup$
    Have you considered using Vector class from System.Numerics?
    $endgroup$
    – Rick Davin
    8 hours ago










  • $begingroup$
    @RickDavin yes i started out with System.Numerics but ran into some limitations, so now i am building my own to circumvent those. But for what i have atm it looks similar to System.Numerics, but i hope to change that soon :)
    $endgroup$
    – FutureCake
    7 hours ago













6












6








6





$begingroup$


I have been working on a vector library for a little bit now. And at this point the basics are down and I am looking for some tips and advic - this may include:



  1. suggestions for things that might be missing and should be added

  2. improvements for readability and of-course efficiency

  3. if you have an important tip or advice that I didn't think of, please feel free to add it :)

See the code below for a Vector3 struct and a VectorX struct. I have left out the Vector2, 4 for now because those are build in the exact same way as Vector3. VectorX is a struct for vectors which have no predefined length. See code below:



Vector3:



using System;

namespace Development

public struct Vector3 : IEquatable<Vector3>

public float X get; set;
public float Y get; set;
public float Z get; set;
public int VectorCount get return 3;

public Vector3(float value)

X = value;
Y = value;
Z = value;


public Vector3(float x, float y, float z)

X = x;
Y = y;
Z = z;


public Vector3(Vector3 vector)

X = vector.X;
Y = vector.Y;
Z = vector.Z;


public Vector3(float[] arr)

X = arr[0];
Y = arr[1];
Z = arr[2];


public Vector3(float[] arr, int startIndex)

if (startIndex > arr.Length - 3) throw new Exception("start index doesnt leave enough space to fill all vector parameters");
else

X = arr[startIndex];
Y = arr[startIndex + 1];
Z = arr[startIndex + 2];




public override string ToString()

return "<" + X + ", " + Y + ", " + Z + ">";


public float[] ToArray()

return new float[] X, Y, Z ;


public Vector3 UpdateFromArray(float[] arr)

if (arr.Length < 3) throw new Exception("Array is too small to convert to vector");
else

X = arr[0];
Y = arr[1];
Z = arr[2];

return this;


public Vector3 UpdateFromArray(float[] arr, int startIndex)

if (startIndex + 2 > arr.Length - 1) throw new Exception("startindex is too high to fill vector");
else

X = arr[startIndex];
Y = arr[startIndex + 1];
Z = arr[startIndex + 2];

return this;


public bool Equals(Vector3 other) // not implemented

throw new NotImplementedException();


public Vector3 Abs()

X = (X < 0) ? X *= -1 : X;
Y = (Y < 0) ? Y *= -1 : Y;
Z = (Z < 0) ? Z *= -1 : Z;
return this;


public float Length()

return MathF.Sqrt(MathF.Pow(X, 2) + MathF.Pow(Y, 2) + MathF.Pow(Z, 2));


public float Dist(Vector3 other)

return MathF.Sqrt(MathF.Pow((this.X - other.X), 2) + MathF.Pow((this.Y - other.Y), 2) + MathF.Pow((this.Z - other.Z), 2));


public Vector3 Min(Vector3 min)

this.X = (this.X < min.X) ? this.X : min.X;
this.Y = (this.Y < min.Y) ? this.Y : min.Y;
this.Z = (this.Z < min.Z) ? this.Z : min.Z;
return this;


public Vector3 Max(Vector3 max)

this.X = (this.X > max.X) ? this.X : max.X;
this.Y = (this.Y > max.Y) ? this.Y : max.Y;
this.Z = (this.Z > max.Z) ? this.Z : max.Z;
return this;


public Vector3 Clamp(Vector3 min, Vector3 max)

this.X = (this.X < min.X) ? min.X : this.X = (this.X > max.X) ? max.X : this.X;
this.Y = (this.Y < min.Y) ? min.Y : this.Y = (this.Y > max.Y) ? max.Y : this.Y;
this.Z = (this.Z < min.Z) ? min.Z : this.Z = (this.Z > max.Z) ? max.Z : this.Z;
return this;


public Vector3 Lerp(Vector3 to, float amount)

amount = Math.Clamp(amount, 0, 1);
this.X = (this.X - to.X) * amount + to.X;
this.Y = (this.Y - to.Y) * amount + to.Y;
this.Z = (this.Z - to.Z) * amount + to.Z;
return this;


public Vector3 Normalize()

float scalar = 1 / MathF.Sqrt(MathF.Pow(X, 2) + MathF.Pow(Y, 2) + MathF.Pow(Z, 2));
X *= scalar;
Y *= scalar;
Z *= scalar;
return this;


public float Dot(Vector3 other)

return this.X * other.X + this.Y * other.Y + this.Z * other.Z;



public static Vector3 Abs(Vector3 value)

return value.Abs();


public static Vector3 Clamp(Vector3 value, Vector3 min, Vector3 max)

return value.Clamp(min, max);


public static Vector3 Min(Vector3 value, Vector3 min)

return value.Min(min);


public static Vector3 Max(Vector3 value, Vector3 max)

return value.Max(max);


public static float Dist(Vector3 value1, Vector3 value2)

return value1.Dist(value2);


public static float Dot(Vector3 value1, Vector3 value2)

return value1.Dot(value2);


public static float Length(Vector3 value)

return value.Length();


public static Vector3 Lerp(Vector3 from, Vector3 to, float amount)

return from.Lerp(to, amount);


public static Vector3 Normalize(Vector3 value)

return value.Normalize();



public static Vector3 Zero get return new Vector3(0);
public static Vector3 Unit get return new Vector3(1);
public static Vector3 UnitX get return new Vector3(1, 0, 0);
public static Vector3 UnitY get return new Vector3(0, 1, 0);
public static Vector3 UnitZ get return new Vector3(0, 0, 1);


public static Vector3 operator +(Vector3 left, float value)

left.X += value;
left.Y += value;
left.Z += value;
return left;


public static Vector3 operator +(Vector3 left, Vector3 right)

left.X += right.X;
left.Y += right.Y;
left.Z += right.Z;
return left;


public static Vector3 operator -(Vector3 left)

left.X = (-left.X);
left.Y = (-left.Y);
left.Z = (-left.Z);
return left;


public static Vector3 operator -(Vector3 left, float value)

left.X -= value;
left.Y -= value;
left.Z -= value;
return left;


public static Vector3 operator -(Vector3 left, Vector3 right)

left.X -= right.X;
left.Y -= right.Y;
left.Z -= right.Z;
return left;


public static Vector3 operator /(Vector3 left, float value)

left.X /= value;
left.Y /= value;
left.Z /= value;
return left;


public static Vector3 operator /(Vector3 left, Vector3 right)

left.X /= right.X;
left.Y /= right.Y;
left.Z /= right.Z;
return left;


public static Vector3 operator *(Vector3 left, float value)

left.X *= value;
left.Y *= value;
left.Z *= value;
return left;


public static Vector3 operator *(Vector3 left, Vector3 right)

left.X *= right.X;
left.Y *= right.Y;
left.Z *= right.Z;
return left;


// do these size comparisions make sense?
public static bool operator <(Vector3 left, Vector3 right)

return (left.Length() < right.Length()) ? true : false;


public static bool operator >(Vector3 left, Vector3 right)

return (left.Length() > right.Length()) ? true : false;


public static bool operator >=(Vector3 left, Vector3 right)

return (left.Length() >= right.Length()) ? true : false;


public static bool operator <=(Vector3 left, Vector3 right)

return (left.Length() <= right.Length()) ? true : false;

// end question

public static bool operator ==(Vector3 left, Vector3 right)

return (left.X == right.X && left.Y == right.Y && left.Z == right.Z) ? true : false;


public static bool operator !=(Vector3 left, Vector3 right)






VectorX:



using System;

namespace Development

public struct VectorX : IEquatable<VectorX>

// private float[] VectorValues;
public float[] VectorValues get; set;
public int VectorCount get; private set;

public VectorX(int vectorCount)

VectorValues = new float[vectorCount];
VectorCount = vectorCount;


public VectorX(int vectorCount, float defaultValue)

VectorCount = vectorCount;
VectorValues = new float[vectorCount];
for (int i = 0; i < vectorCount; i++)

VectorValues[i] = defaultValue;




public VectorX(params float[] values)

VectorValues = values;
VectorCount = values.Length;


public VectorX(float[] arr, int startIndex)

VectorCount = arr.Length - 1 - startIndex;
VectorValues = new float[VectorCount];
for (int i = startIndex; i < arr.Length; i++)

VectorValues[i - startIndex] = arr[i];



public VectorX(float[] arr, int startIndex, int endIndex)

VectorCount = endIndex - startIndex;
VectorValues = new float[VectorCount];
for (int i = startIndex; i < endIndex; i++)

VectorValues[i - startIndex] = arr[i];



public VectorX(VectorX vector)

VectorCount = vector.VectorCount;
VectorValues = vector.VectorValues;



public override string ToString()

string ret = "<";
for (int i = 0; i < VectorCount; i++)

if (i == VectorCount - 1) ret += VectorValues[i];
else ret += VectorValues[i] + ", ";

return ret += ">";


public float[] ToArray()

return VectorValues;


public VectorX UpdateFromArray(float[] arr)

if (arr.Length < VectorCount) throw new Exception("Array is too small to convert to vector");
else

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = arr[i];


return this;


public VectorX UpdateFromArray(float[] arr, int startIndex)

if (startIndex + VectorCount > arr.Length - 1) throw new Exception("startindex is too high to fill vector");
else

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = arr[startIndex + i];



return this;


public float GetAt(int pos)


public VectorX SetAt(int pos, float value)

if (pos >= VectorCount

public bool Equals(VectorX other) // not implemented

throw new NotImplementedException();


public float Length()

float total = 0;
for (int i = 0; i < VectorCount; i++)

total += MathF.Pow(VectorValues[i], 2);

return MathF.Sqrt(total);


public VectorX Normalize()

float scalar = 1 / this.Length();
for (int i = 0; i < VectorCount; i++)

VectorValues[i] *= scalar;

return this;


public VectorX Abs()

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = (VectorValues[i] < 0) ? VectorValues[i] *= -1 : VectorValues[i];

return this;


public float Dist(VectorX other)

if (other.VectorCount != this.VectorCount) throw new Exception("Vectors are not of same length");
else

float sqrtValue = 0;
for (int i = 0; i < this.VectorCount; i++)

sqrtValue += MathF.Pow((this.VectorValues[i] - other.GetAt(i)), 2);

return MathF.Sqrt(sqrtValue);



public VectorX Min(VectorX min)

if (min.VectorCount != this.VectorCount) throw new Exception("Vectors are not of same length");
else

for (int i = 0; i < this.VectorCount; i++)

this.VectorValues[i] = (this.VectorValues[i] > min.GetAt(i)) ? this.VectorValues[i] : min.GetAt(i);


return this;


public VectorX Max(VectorX max)

if (max.VectorCount != this.VectorCount) throw new Exception("Vectors are not of same length");
else

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = (VectorValues[i] < max.GetAt(i)) ? VectorValues[i] : max.GetAt(i);


return this;


public VectorX Clamp(VectorX min, VectorX max)


public VectorX Lerp(VectorX to, float amount)

if (to.VectorCount != VectorCount) throw new Exception("Vectors have different length");
else

amount = Math.Clamp(amount, 0, 1);
for (int i = 0; i < VectorCount; i++)

VectorValues[i] = (VectorValues[i] - to.GetAt(i)) * amount + to.GetAt(i);

return this;




public float Dot(VectorX other)

if(other.VectorCount != VectorCount) throw new Exception("Vectors are not the same length");
else
float d = 0;
for (int i = 0; i < VectorCount; i++)

d += VectorValues[i] * other.GetAt(i);

return d;





public static VectorX NewUnit(int size)

return new VectorX(size, 1);

public static VectorX NewUnitIndex(int size, int index)

VectorX vec = new VectorX(size);
vec.SetAt(index, 1);
return vec;



public static float Length(VectorX vector)

return vector.Length();


public static VectorX Normalize(VectorX vector)

return vector.Normalize();


public static VectorX Abs(VectorX vector)

return vector.Abs();


public static float Dist(VectorX vector1, VectorX vector2)

return vector1.Dist(vector2);


public static VectorX Min(VectorX vector, VectorX min)

return vector.Min(min);


public static VectorX Max(VectorX vector, VectorX min)

return vector.Max(min);


public static VectorX Clamp(VectorX vector, VectorX min, VectorX max)

return vector.Clamp(min, max);


public static VectorX Lerp(VectorX vector, VectorX to, float amount)

return vector.Lerp(to, amount);


public static float Dot(VectorX vector1, VectorX vector2)

return vector1.Dot(vector2);



public static VectorX operator +(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) + value);

return left;


public static VectorX operator +(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) + right.GetAt(i));

return left;



public static VectorX operator -(VectorX left)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) * -1);

return left;


public static VectorX operator -(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) - value);

return left;


public static VectorX operator -(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) - right.GetAt(i));

return left;



public static VectorX operator /(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) / value);

return left;


public static VectorX operator /(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) / right.GetAt(i));

return left;



public static VectorX operator *(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) * value);

return left;


public static VectorX operator *(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) * right.GetAt(i));

return left;



// do these size comparisions make sense?
public static bool operator <(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() < right.Length()) ? true : false;


public static bool operator >(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() > right.Length()) ? true : false;


public static bool operator >=(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() >= right.Length()) ? true : false;


public static bool operator <=(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() <= right.Length()) ? true : false;

// end question

public static bool operator ==(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else

for (int i = 0; i < left.VectorCount; i++)

if(left.GetAt(i) != right.GetAt(i)) return false;

return true;



public static bool operator !=(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else

for (int i = 0; i < left.VectorCount; i++)

if(left.GetAt(i) != right.GetAt(i)) return true;

return false;













share|improve this question









New contributor



FutureCake is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$




I have been working on a vector library for a little bit now. And at this point the basics are down and I am looking for some tips and advic - this may include:



  1. suggestions for things that might be missing and should be added

  2. improvements for readability and of-course efficiency

  3. if you have an important tip or advice that I didn't think of, please feel free to add it :)

See the code below for a Vector3 struct and a VectorX struct. I have left out the Vector2, 4 for now because those are build in the exact same way as Vector3. VectorX is a struct for vectors which have no predefined length. See code below:



Vector3:



using System;

namespace Development

public struct Vector3 : IEquatable<Vector3>

public float X get; set;
public float Y get; set;
public float Z get; set;
public int VectorCount get return 3;

public Vector3(float value)

X = value;
Y = value;
Z = value;


public Vector3(float x, float y, float z)

X = x;
Y = y;
Z = z;


public Vector3(Vector3 vector)

X = vector.X;
Y = vector.Y;
Z = vector.Z;


public Vector3(float[] arr)

X = arr[0];
Y = arr[1];
Z = arr[2];


public Vector3(float[] arr, int startIndex)

if (startIndex > arr.Length - 3) throw new Exception("start index doesnt leave enough space to fill all vector parameters");
else

X = arr[startIndex];
Y = arr[startIndex + 1];
Z = arr[startIndex + 2];




public override string ToString()

return "<" + X + ", " + Y + ", " + Z + ">";


public float[] ToArray()

return new float[] X, Y, Z ;


public Vector3 UpdateFromArray(float[] arr)

if (arr.Length < 3) throw new Exception("Array is too small to convert to vector");
else

X = arr[0];
Y = arr[1];
Z = arr[2];

return this;


public Vector3 UpdateFromArray(float[] arr, int startIndex)

if (startIndex + 2 > arr.Length - 1) throw new Exception("startindex is too high to fill vector");
else

X = arr[startIndex];
Y = arr[startIndex + 1];
Z = arr[startIndex + 2];

return this;


public bool Equals(Vector3 other) // not implemented

throw new NotImplementedException();


public Vector3 Abs()

X = (X < 0) ? X *= -1 : X;
Y = (Y < 0) ? Y *= -1 : Y;
Z = (Z < 0) ? Z *= -1 : Z;
return this;


public float Length()

return MathF.Sqrt(MathF.Pow(X, 2) + MathF.Pow(Y, 2) + MathF.Pow(Z, 2));


public float Dist(Vector3 other)

return MathF.Sqrt(MathF.Pow((this.X - other.X), 2) + MathF.Pow((this.Y - other.Y), 2) + MathF.Pow((this.Z - other.Z), 2));


public Vector3 Min(Vector3 min)

this.X = (this.X < min.X) ? this.X : min.X;
this.Y = (this.Y < min.Y) ? this.Y : min.Y;
this.Z = (this.Z < min.Z) ? this.Z : min.Z;
return this;


public Vector3 Max(Vector3 max)

this.X = (this.X > max.X) ? this.X : max.X;
this.Y = (this.Y > max.Y) ? this.Y : max.Y;
this.Z = (this.Z > max.Z) ? this.Z : max.Z;
return this;


public Vector3 Clamp(Vector3 min, Vector3 max)

this.X = (this.X < min.X) ? min.X : this.X = (this.X > max.X) ? max.X : this.X;
this.Y = (this.Y < min.Y) ? min.Y : this.Y = (this.Y > max.Y) ? max.Y : this.Y;
this.Z = (this.Z < min.Z) ? min.Z : this.Z = (this.Z > max.Z) ? max.Z : this.Z;
return this;


public Vector3 Lerp(Vector3 to, float amount)

amount = Math.Clamp(amount, 0, 1);
this.X = (this.X - to.X) * amount + to.X;
this.Y = (this.Y - to.Y) * amount + to.Y;
this.Z = (this.Z - to.Z) * amount + to.Z;
return this;


public Vector3 Normalize()

float scalar = 1 / MathF.Sqrt(MathF.Pow(X, 2) + MathF.Pow(Y, 2) + MathF.Pow(Z, 2));
X *= scalar;
Y *= scalar;
Z *= scalar;
return this;


public float Dot(Vector3 other)

return this.X * other.X + this.Y * other.Y + this.Z * other.Z;



public static Vector3 Abs(Vector3 value)

return value.Abs();


public static Vector3 Clamp(Vector3 value, Vector3 min, Vector3 max)

return value.Clamp(min, max);


public static Vector3 Min(Vector3 value, Vector3 min)

return value.Min(min);


public static Vector3 Max(Vector3 value, Vector3 max)

return value.Max(max);


public static float Dist(Vector3 value1, Vector3 value2)

return value1.Dist(value2);


public static float Dot(Vector3 value1, Vector3 value2)

return value1.Dot(value2);


public static float Length(Vector3 value)

return value.Length();


public static Vector3 Lerp(Vector3 from, Vector3 to, float amount)

return from.Lerp(to, amount);


public static Vector3 Normalize(Vector3 value)

return value.Normalize();



public static Vector3 Zero get return new Vector3(0);
public static Vector3 Unit get return new Vector3(1);
public static Vector3 UnitX get return new Vector3(1, 0, 0);
public static Vector3 UnitY get return new Vector3(0, 1, 0);
public static Vector3 UnitZ get return new Vector3(0, 0, 1);


public static Vector3 operator +(Vector3 left, float value)

left.X += value;
left.Y += value;
left.Z += value;
return left;


public static Vector3 operator +(Vector3 left, Vector3 right)

left.X += right.X;
left.Y += right.Y;
left.Z += right.Z;
return left;


public static Vector3 operator -(Vector3 left)

left.X = (-left.X);
left.Y = (-left.Y);
left.Z = (-left.Z);
return left;


public static Vector3 operator -(Vector3 left, float value)

left.X -= value;
left.Y -= value;
left.Z -= value;
return left;


public static Vector3 operator -(Vector3 left, Vector3 right)

left.X -= right.X;
left.Y -= right.Y;
left.Z -= right.Z;
return left;


public static Vector3 operator /(Vector3 left, float value)

left.X /= value;
left.Y /= value;
left.Z /= value;
return left;


public static Vector3 operator /(Vector3 left, Vector3 right)

left.X /= right.X;
left.Y /= right.Y;
left.Z /= right.Z;
return left;


public static Vector3 operator *(Vector3 left, float value)

left.X *= value;
left.Y *= value;
left.Z *= value;
return left;


public static Vector3 operator *(Vector3 left, Vector3 right)

left.X *= right.X;
left.Y *= right.Y;
left.Z *= right.Z;
return left;


// do these size comparisions make sense?
public static bool operator <(Vector3 left, Vector3 right)

return (left.Length() < right.Length()) ? true : false;


public static bool operator >(Vector3 left, Vector3 right)

return (left.Length() > right.Length()) ? true : false;


public static bool operator >=(Vector3 left, Vector3 right)

return (left.Length() >= right.Length()) ? true : false;


public static bool operator <=(Vector3 left, Vector3 right)

return (left.Length() <= right.Length()) ? true : false;

// end question

public static bool operator ==(Vector3 left, Vector3 right)

return (left.X == right.X && left.Y == right.Y && left.Z == right.Z) ? true : false;


public static bool operator !=(Vector3 left, Vector3 right)






VectorX:



using System;

namespace Development

public struct VectorX : IEquatable<VectorX>

// private float[] VectorValues;
public float[] VectorValues get; set;
public int VectorCount get; private set;

public VectorX(int vectorCount)

VectorValues = new float[vectorCount];
VectorCount = vectorCount;


public VectorX(int vectorCount, float defaultValue)

VectorCount = vectorCount;
VectorValues = new float[vectorCount];
for (int i = 0; i < vectorCount; i++)

VectorValues[i] = defaultValue;




public VectorX(params float[] values)

VectorValues = values;
VectorCount = values.Length;


public VectorX(float[] arr, int startIndex)

VectorCount = arr.Length - 1 - startIndex;
VectorValues = new float[VectorCount];
for (int i = startIndex; i < arr.Length; i++)

VectorValues[i - startIndex] = arr[i];



public VectorX(float[] arr, int startIndex, int endIndex)

VectorCount = endIndex - startIndex;
VectorValues = new float[VectorCount];
for (int i = startIndex; i < endIndex; i++)

VectorValues[i - startIndex] = arr[i];



public VectorX(VectorX vector)

VectorCount = vector.VectorCount;
VectorValues = vector.VectorValues;



public override string ToString()

string ret = "<";
for (int i = 0; i < VectorCount; i++)

if (i == VectorCount - 1) ret += VectorValues[i];
else ret += VectorValues[i] + ", ";

return ret += ">";


public float[] ToArray()

return VectorValues;


public VectorX UpdateFromArray(float[] arr)

if (arr.Length < VectorCount) throw new Exception("Array is too small to convert to vector");
else

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = arr[i];


return this;


public VectorX UpdateFromArray(float[] arr, int startIndex)

if (startIndex + VectorCount > arr.Length - 1) throw new Exception("startindex is too high to fill vector");
else

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = arr[startIndex + i];



return this;


public float GetAt(int pos)


public VectorX SetAt(int pos, float value)

if (pos >= VectorCount

public bool Equals(VectorX other) // not implemented

throw new NotImplementedException();


public float Length()

float total = 0;
for (int i = 0; i < VectorCount; i++)

total += MathF.Pow(VectorValues[i], 2);

return MathF.Sqrt(total);


public VectorX Normalize()

float scalar = 1 / this.Length();
for (int i = 0; i < VectorCount; i++)

VectorValues[i] *= scalar;

return this;


public VectorX Abs()

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = (VectorValues[i] < 0) ? VectorValues[i] *= -1 : VectorValues[i];

return this;


public float Dist(VectorX other)

if (other.VectorCount != this.VectorCount) throw new Exception("Vectors are not of same length");
else

float sqrtValue = 0;
for (int i = 0; i < this.VectorCount; i++)

sqrtValue += MathF.Pow((this.VectorValues[i] - other.GetAt(i)), 2);

return MathF.Sqrt(sqrtValue);



public VectorX Min(VectorX min)

if (min.VectorCount != this.VectorCount) throw new Exception("Vectors are not of same length");
else

for (int i = 0; i < this.VectorCount; i++)

this.VectorValues[i] = (this.VectorValues[i] > min.GetAt(i)) ? this.VectorValues[i] : min.GetAt(i);


return this;


public VectorX Max(VectorX max)

if (max.VectorCount != this.VectorCount) throw new Exception("Vectors are not of same length");
else

for (int i = 0; i < VectorCount; i++)

VectorValues[i] = (VectorValues[i] < max.GetAt(i)) ? VectorValues[i] : max.GetAt(i);


return this;


public VectorX Clamp(VectorX min, VectorX max)


public VectorX Lerp(VectorX to, float amount)

if (to.VectorCount != VectorCount) throw new Exception("Vectors have different length");
else

amount = Math.Clamp(amount, 0, 1);
for (int i = 0; i < VectorCount; i++)

VectorValues[i] = (VectorValues[i] - to.GetAt(i)) * amount + to.GetAt(i);

return this;




public float Dot(VectorX other)

if(other.VectorCount != VectorCount) throw new Exception("Vectors are not the same length");
else
float d = 0;
for (int i = 0; i < VectorCount; i++)

d += VectorValues[i] * other.GetAt(i);

return d;





public static VectorX NewUnit(int size)

return new VectorX(size, 1);

public static VectorX NewUnitIndex(int size, int index)

VectorX vec = new VectorX(size);
vec.SetAt(index, 1);
return vec;



public static float Length(VectorX vector)

return vector.Length();


public static VectorX Normalize(VectorX vector)

return vector.Normalize();


public static VectorX Abs(VectorX vector)

return vector.Abs();


public static float Dist(VectorX vector1, VectorX vector2)

return vector1.Dist(vector2);


public static VectorX Min(VectorX vector, VectorX min)

return vector.Min(min);


public static VectorX Max(VectorX vector, VectorX min)

return vector.Max(min);


public static VectorX Clamp(VectorX vector, VectorX min, VectorX max)

return vector.Clamp(min, max);


public static VectorX Lerp(VectorX vector, VectorX to, float amount)

return vector.Lerp(to, amount);


public static float Dot(VectorX vector1, VectorX vector2)

return vector1.Dot(vector2);



public static VectorX operator +(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) + value);

return left;


public static VectorX operator +(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) + right.GetAt(i));

return left;



public static VectorX operator -(VectorX left)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) * -1);

return left;


public static VectorX operator -(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) - value);

return left;


public static VectorX operator -(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) - right.GetAt(i));

return left;



public static VectorX operator /(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) / value);

return left;


public static VectorX operator /(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) / right.GetAt(i));

return left;



public static VectorX operator *(VectorX left, float value)

for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) * value);

return left;


public static VectorX operator *(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else
for (int i = 0; i < left.VectorCount; i++)

left.SetAt(i, left.GetAt(i) * right.GetAt(i));

return left;



// do these size comparisions make sense?
public static bool operator <(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() < right.Length()) ? true : false;


public static bool operator >(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() > right.Length()) ? true : false;


public static bool operator >=(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() >= right.Length()) ? true : false;


public static bool operator <=(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else return (left.Length() <= right.Length()) ? true : false;

// end question

public static bool operator ==(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else

for (int i = 0; i < left.VectorCount; i++)

if(left.GetAt(i) != right.GetAt(i)) return false;

return true;



public static bool operator !=(VectorX left, VectorX right)

if(left.VectorCount != right.VectorCount) throw new Exception("Vectors not same length");
else

for (int i = 0; i < left.VectorCount; i++)

if(left.GetAt(i) != right.GetAt(i)) return true;

return false;










c# performance coordinate-system






share|improve this question









New contributor



FutureCake is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



FutureCake is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 8 hours ago









dfhwze

12.1k2 gold badges22 silver badges82 bronze badges




12.1k2 gold badges22 silver badges82 bronze badges






New contributor



FutureCake is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 9 hours ago









FutureCakeFutureCake

1314 bronze badges




1314 bronze badges




New contributor



FutureCake is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




FutureCake is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • $begingroup$
    This seems alot like a javascript library (three.js) ported to C#.
    $endgroup$
    – dfhwze
    8 hours ago






  • 1




    $begingroup$
    Have you considered using Vector class from System.Numerics?
    $endgroup$
    – Rick Davin
    8 hours ago










  • $begingroup$
    @RickDavin yes i started out with System.Numerics but ran into some limitations, so now i am building my own to circumvent those. But for what i have atm it looks similar to System.Numerics, but i hope to change that soon :)
    $endgroup$
    – FutureCake
    7 hours ago
















  • $begingroup$
    This seems alot like a javascript library (three.js) ported to C#.
    $endgroup$
    – dfhwze
    8 hours ago






  • 1




    $begingroup$
    Have you considered using Vector class from System.Numerics?
    $endgroup$
    – Rick Davin
    8 hours ago










  • $begingroup$
    @RickDavin yes i started out with System.Numerics but ran into some limitations, so now i am building my own to circumvent those. But for what i have atm it looks similar to System.Numerics, but i hope to change that soon :)
    $endgroup$
    – FutureCake
    7 hours ago















$begingroup$
This seems alot like a javascript library (three.js) ported to C#.
$endgroup$
– dfhwze
8 hours ago




$begingroup$
This seems alot like a javascript library (three.js) ported to C#.
$endgroup$
– dfhwze
8 hours ago




1




1




$begingroup$
Have you considered using Vector class from System.Numerics?
$endgroup$
– Rick Davin
8 hours ago




$begingroup$
Have you considered using Vector class from System.Numerics?
$endgroup$
– Rick Davin
8 hours ago












$begingroup$
@RickDavin yes i started out with System.Numerics but ran into some limitations, so now i am building my own to circumvent those. But for what i have atm it looks similar to System.Numerics, but i hope to change that soon :)
$endgroup$
– FutureCake
7 hours ago




$begingroup$
@RickDavin yes i started out with System.Numerics but ran into some limitations, so now i am building my own to circumvent those. But for what i have atm it looks similar to System.Numerics, but i hope to change that soon :)
$endgroup$
– FutureCake
7 hours ago










1 Answer
1






active

oldest

votes


















6














$begingroup$

You are inconsistent with your use of the this. 'suffix'. Nobody can agree on this, but you should try to be consistent without projects.




I would remove the return values from every method which modifies Vector3: you have 2 copies of Min, and one is very confusing. All of the methods which happen to produce a vector (e.g. Abs, Min, Max) happen to modifythisand then returnthis. This is, in my opinion, a terrible API, and inconsistent with operations (likeLength) which just happen to _not_ produce a vector. Anyone looking at the signature will assume it does not modify the vector, and you just open yourself to all the nightmares associated with mutablestruct`s. Do not mix an immutable/mutable API like you are now, because it will only infuriate your consumers.



Methods like UpdateFromArray are for some reason returning this as well.




You seem to be trying to provide a consistent API between VectorX (which I would just call Vector) and Vector3, so you might consider an IVector interface.



VectorCount is a mildly confusing name. It's the element count, or the length of the vector.




I would suggest not using Math.Pow(, 2) just to square something: even if it is optimised to detect this exact case (I don't know), it is just harder to read than X * X. I would rewrite Dist as (this - other).length for the sake of simplicity.



It's also common to provide a LengthSquared member which returns the sum before the squareroot, since often this is all that is needed and saves an expensive operation.




The parameter names for Min and Max are odd: other would be fine, but again I don't like the API.




I would expect public Vector3(float[] arr) to throw a nice exception if arr is null, or had a length other than 3.



It's good that most of your methods in VectorX are performing range checks (I didn't notice any that didn't). You don't need to put the 'non-exceptional' code in an else for these.




VectorX has many constructors which just provide 'defaults' for others. I would make these call directly the more general versions. E.g.



public VectorX(int vectorCount) : this(vectorCount, default(float))


public VectorX(params float[] values) : this(values, 0, values.Length)



This will significantly reduce redundancy and so improve maintainability.




Your ToString methods could be nicer: I would use string interpolation for Vector3 (i.e. $"<X, Y, Z>") and you should use a StringBuilder for VectorX (currently VectorX.ToString() is a quadratic memory operation when it should be linear).





// do these size comparisions make sense?




No, I would say no; though, it does atleast provide an ordering, so it could be much worse.



The comparisons are also performing 2 unnecessary square-roots (you could compare the LengthSquared).



All your Vector3 comparisions also include a completely redundant ternay clause, which will just get in the way of maintaince efforts and provide a greater surface overwhich bugs can will appear.



I would consider describing negatives in terms of the positives, e.g.



public static bool operator !=(Vector3 left, Vector3 right)

return !(left == right);




Why isn't VectorX.Equals(VectorX other) implemented? What is the point of declaring you implement IEquatable<VectorX> if you do not?




I don't like all your single-line ifs and elses. Even if you don't want to add braces, a line at the end of the condition or else helps significantly with readbility, and reduces the amount of code which is 'off side'.




All of your types and methods would benefit from inline documention (///). This would help to explain the confusing bits of the API, and clarify what methods like Length and Normalise mean.





VectorValues[i] = (VectorValues[i] < 0) ? VectorValues[i] *= -1 :
VectorValues[i];



What is wrong with Math.Abs(VectorValues[i]).





public int VectorCount get return 3;




This can be made a little more concise:



public int VectorCount => 3;


You could do the same with the static Unit and Zero members.




You have some odd line-spacing in places (e.g. around VectorX.NewUnit). I can see no reason for this, so it just looks untidy and makes the code harder to scan.




Consider using an indexer instead of the SetAt and GetAt methods. These could be part of the IVector interface also.




VectorX.VectorValues should not be mutable, and probably shouldn't be public. At the moment it is possible for someone to change VectorValues such that VectorCount will be wrong, or even to change it null and wreck everything. Hide it away so that people can't shoot themselves in the foot with your API.



You could re-write VectorX.VectorCount in terms of VectorValues to reduce redunancy and simplify the constructors.



public int VectorCount => VectorValues.Length;


Less redundancy again means there is less to go wrong, which makes the code easier to maintain.




The VectorX(VectorX) constructor is not good: all the other constructors duplicate the array they are given, but this just copies the reference. There is no point in this constructor (it's the same as a value-copy) and it will only create confusion. If instead it copied the array, then it would be fine.



public VectorX(VectorX vector) : this(vector.VectorValues)



ToArray would also imply a copy, and with VectorValues public does nothing useful. It should take a copy. This is as easy as VectorValues.ToArray() if you have using System.Linq at the top of your file. The fact that it does/doesn't copy should be in the documention.




I'm going to stop for now, but I may add to this answer when I have time, though I'm sure someone else will take up the slack long before then if there is more to be said.






share|improve this answer











$endgroup$














  • $begingroup$
    It will be hard writing a complement answer without having some kind of intersection with this answer :p
    $endgroup$
    – dfhwze
    8 hours ago










  • $begingroup$
    About the status of Pow(x, 2), it is not (yet) optimized by the main implementations, there is an open issue for coreclr, so maybe someday
    $endgroup$
    – harold
    7 hours ago










  • $begingroup$
    VisualMelon, Thank you so much for the extensive list of improvements!!! As you may have seen from my code i am no pro ;) But this sure as hell will help with my improvement of skills :) I will be working on this list in the next few days probably :) Please keep adding indeed if you feel like it :)
    $endgroup$
    – FutureCake
    7 hours ago










  • $begingroup$
    @FutureCake you're welcome. It's customary to hold off before marking any answer as 'the' answer for at least a day or two, so as not to discourage more answers. I didn't add it to my answer, because it's not something I have much experience with, but you may want to look into the SIMD support in .NET if you really want serious performance.
    $endgroup$
    – VisualMelon
    7 hours ago











  • $begingroup$
    @VisualMelon I unchecked the answer, i will get back to it in a few days then :) And i will go and check out what SIMD is! Thanks again!
    $endgroup$
    – FutureCake
    7 hours ago













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);







FutureCake is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f229256%2fc-vector-library%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














$begingroup$

You are inconsistent with your use of the this. 'suffix'. Nobody can agree on this, but you should try to be consistent without projects.




I would remove the return values from every method which modifies Vector3: you have 2 copies of Min, and one is very confusing. All of the methods which happen to produce a vector (e.g. Abs, Min, Max) happen to modifythisand then returnthis. This is, in my opinion, a terrible API, and inconsistent with operations (likeLength) which just happen to _not_ produce a vector. Anyone looking at the signature will assume it does not modify the vector, and you just open yourself to all the nightmares associated with mutablestruct`s. Do not mix an immutable/mutable API like you are now, because it will only infuriate your consumers.



Methods like UpdateFromArray are for some reason returning this as well.




You seem to be trying to provide a consistent API between VectorX (which I would just call Vector) and Vector3, so you might consider an IVector interface.



VectorCount is a mildly confusing name. It's the element count, or the length of the vector.




I would suggest not using Math.Pow(, 2) just to square something: even if it is optimised to detect this exact case (I don't know), it is just harder to read than X * X. I would rewrite Dist as (this - other).length for the sake of simplicity.



It's also common to provide a LengthSquared member which returns the sum before the squareroot, since often this is all that is needed and saves an expensive operation.




The parameter names for Min and Max are odd: other would be fine, but again I don't like the API.




I would expect public Vector3(float[] arr) to throw a nice exception if arr is null, or had a length other than 3.



It's good that most of your methods in VectorX are performing range checks (I didn't notice any that didn't). You don't need to put the 'non-exceptional' code in an else for these.




VectorX has many constructors which just provide 'defaults' for others. I would make these call directly the more general versions. E.g.



public VectorX(int vectorCount) : this(vectorCount, default(float))


public VectorX(params float[] values) : this(values, 0, values.Length)



This will significantly reduce redundancy and so improve maintainability.




Your ToString methods could be nicer: I would use string interpolation for Vector3 (i.e. $"<X, Y, Z>") and you should use a StringBuilder for VectorX (currently VectorX.ToString() is a quadratic memory operation when it should be linear).





// do these size comparisions make sense?




No, I would say no; though, it does atleast provide an ordering, so it could be much worse.



The comparisons are also performing 2 unnecessary square-roots (you could compare the LengthSquared).



All your Vector3 comparisions also include a completely redundant ternay clause, which will just get in the way of maintaince efforts and provide a greater surface overwhich bugs can will appear.



I would consider describing negatives in terms of the positives, e.g.



public static bool operator !=(Vector3 left, Vector3 right)

return !(left == right);




Why isn't VectorX.Equals(VectorX other) implemented? What is the point of declaring you implement IEquatable<VectorX> if you do not?




I don't like all your single-line ifs and elses. Even if you don't want to add braces, a line at the end of the condition or else helps significantly with readbility, and reduces the amount of code which is 'off side'.




All of your types and methods would benefit from inline documention (///). This would help to explain the confusing bits of the API, and clarify what methods like Length and Normalise mean.





VectorValues[i] = (VectorValues[i] < 0) ? VectorValues[i] *= -1 :
VectorValues[i];



What is wrong with Math.Abs(VectorValues[i]).





public int VectorCount get return 3;




This can be made a little more concise:



public int VectorCount => 3;


You could do the same with the static Unit and Zero members.




You have some odd line-spacing in places (e.g. around VectorX.NewUnit). I can see no reason for this, so it just looks untidy and makes the code harder to scan.




Consider using an indexer instead of the SetAt and GetAt methods. These could be part of the IVector interface also.




VectorX.VectorValues should not be mutable, and probably shouldn't be public. At the moment it is possible for someone to change VectorValues such that VectorCount will be wrong, or even to change it null and wreck everything. Hide it away so that people can't shoot themselves in the foot with your API.



You could re-write VectorX.VectorCount in terms of VectorValues to reduce redunancy and simplify the constructors.



public int VectorCount => VectorValues.Length;


Less redundancy again means there is less to go wrong, which makes the code easier to maintain.




The VectorX(VectorX) constructor is not good: all the other constructors duplicate the array they are given, but this just copies the reference. There is no point in this constructor (it's the same as a value-copy) and it will only create confusion. If instead it copied the array, then it would be fine.



public VectorX(VectorX vector) : this(vector.VectorValues)



ToArray would also imply a copy, and with VectorValues public does nothing useful. It should take a copy. This is as easy as VectorValues.ToArray() if you have using System.Linq at the top of your file. The fact that it does/doesn't copy should be in the documention.




I'm going to stop for now, but I may add to this answer when I have time, though I'm sure someone else will take up the slack long before then if there is more to be said.






share|improve this answer











$endgroup$














  • $begingroup$
    It will be hard writing a complement answer without having some kind of intersection with this answer :p
    $endgroup$
    – dfhwze
    8 hours ago










  • $begingroup$
    About the status of Pow(x, 2), it is not (yet) optimized by the main implementations, there is an open issue for coreclr, so maybe someday
    $endgroup$
    – harold
    7 hours ago










  • $begingroup$
    VisualMelon, Thank you so much for the extensive list of improvements!!! As you may have seen from my code i am no pro ;) But this sure as hell will help with my improvement of skills :) I will be working on this list in the next few days probably :) Please keep adding indeed if you feel like it :)
    $endgroup$
    – FutureCake
    7 hours ago










  • $begingroup$
    @FutureCake you're welcome. It's customary to hold off before marking any answer as 'the' answer for at least a day or two, so as not to discourage more answers. I didn't add it to my answer, because it's not something I have much experience with, but you may want to look into the SIMD support in .NET if you really want serious performance.
    $endgroup$
    – VisualMelon
    7 hours ago











  • $begingroup$
    @VisualMelon I unchecked the answer, i will get back to it in a few days then :) And i will go and check out what SIMD is! Thanks again!
    $endgroup$
    – FutureCake
    7 hours ago















6














$begingroup$

You are inconsistent with your use of the this. 'suffix'. Nobody can agree on this, but you should try to be consistent without projects.




I would remove the return values from every method which modifies Vector3: you have 2 copies of Min, and one is very confusing. All of the methods which happen to produce a vector (e.g. Abs, Min, Max) happen to modifythisand then returnthis. This is, in my opinion, a terrible API, and inconsistent with operations (likeLength) which just happen to _not_ produce a vector. Anyone looking at the signature will assume it does not modify the vector, and you just open yourself to all the nightmares associated with mutablestruct`s. Do not mix an immutable/mutable API like you are now, because it will only infuriate your consumers.



Methods like UpdateFromArray are for some reason returning this as well.




You seem to be trying to provide a consistent API between VectorX (which I would just call Vector) and Vector3, so you might consider an IVector interface.



VectorCount is a mildly confusing name. It's the element count, or the length of the vector.




I would suggest not using Math.Pow(, 2) just to square something: even if it is optimised to detect this exact case (I don't know), it is just harder to read than X * X. I would rewrite Dist as (this - other).length for the sake of simplicity.



It's also common to provide a LengthSquared member which returns the sum before the squareroot, since often this is all that is needed and saves an expensive operation.




The parameter names for Min and Max are odd: other would be fine, but again I don't like the API.




I would expect public Vector3(float[] arr) to throw a nice exception if arr is null, or had a length other than 3.



It's good that most of your methods in VectorX are performing range checks (I didn't notice any that didn't). You don't need to put the 'non-exceptional' code in an else for these.




VectorX has many constructors which just provide 'defaults' for others. I would make these call directly the more general versions. E.g.



public VectorX(int vectorCount) : this(vectorCount, default(float))


public VectorX(params float[] values) : this(values, 0, values.Length)



This will significantly reduce redundancy and so improve maintainability.




Your ToString methods could be nicer: I would use string interpolation for Vector3 (i.e. $"<X, Y, Z>") and you should use a StringBuilder for VectorX (currently VectorX.ToString() is a quadratic memory operation when it should be linear).





// do these size comparisions make sense?




No, I would say no; though, it does atleast provide an ordering, so it could be much worse.



The comparisons are also performing 2 unnecessary square-roots (you could compare the LengthSquared).



All your Vector3 comparisions also include a completely redundant ternay clause, which will just get in the way of maintaince efforts and provide a greater surface overwhich bugs can will appear.



I would consider describing negatives in terms of the positives, e.g.



public static bool operator !=(Vector3 left, Vector3 right)

return !(left == right);




Why isn't VectorX.Equals(VectorX other) implemented? What is the point of declaring you implement IEquatable<VectorX> if you do not?




I don't like all your single-line ifs and elses. Even if you don't want to add braces, a line at the end of the condition or else helps significantly with readbility, and reduces the amount of code which is 'off side'.




All of your types and methods would benefit from inline documention (///). This would help to explain the confusing bits of the API, and clarify what methods like Length and Normalise mean.





VectorValues[i] = (VectorValues[i] < 0) ? VectorValues[i] *= -1 :
VectorValues[i];



What is wrong with Math.Abs(VectorValues[i]).





public int VectorCount get return 3;




This can be made a little more concise:



public int VectorCount => 3;


You could do the same with the static Unit and Zero members.




You have some odd line-spacing in places (e.g. around VectorX.NewUnit). I can see no reason for this, so it just looks untidy and makes the code harder to scan.




Consider using an indexer instead of the SetAt and GetAt methods. These could be part of the IVector interface also.




VectorX.VectorValues should not be mutable, and probably shouldn't be public. At the moment it is possible for someone to change VectorValues such that VectorCount will be wrong, or even to change it null and wreck everything. Hide it away so that people can't shoot themselves in the foot with your API.



You could re-write VectorX.VectorCount in terms of VectorValues to reduce redunancy and simplify the constructors.



public int VectorCount => VectorValues.Length;


Less redundancy again means there is less to go wrong, which makes the code easier to maintain.




The VectorX(VectorX) constructor is not good: all the other constructors duplicate the array they are given, but this just copies the reference. There is no point in this constructor (it's the same as a value-copy) and it will only create confusion. If instead it copied the array, then it would be fine.



public VectorX(VectorX vector) : this(vector.VectorValues)



ToArray would also imply a copy, and with VectorValues public does nothing useful. It should take a copy. This is as easy as VectorValues.ToArray() if you have using System.Linq at the top of your file. The fact that it does/doesn't copy should be in the documention.




I'm going to stop for now, but I may add to this answer when I have time, though I'm sure someone else will take up the slack long before then if there is more to be said.






share|improve this answer











$endgroup$














  • $begingroup$
    It will be hard writing a complement answer without having some kind of intersection with this answer :p
    $endgroup$
    – dfhwze
    8 hours ago










  • $begingroup$
    About the status of Pow(x, 2), it is not (yet) optimized by the main implementations, there is an open issue for coreclr, so maybe someday
    $endgroup$
    – harold
    7 hours ago










  • $begingroup$
    VisualMelon, Thank you so much for the extensive list of improvements!!! As you may have seen from my code i am no pro ;) But this sure as hell will help with my improvement of skills :) I will be working on this list in the next few days probably :) Please keep adding indeed if you feel like it :)
    $endgroup$
    – FutureCake
    7 hours ago










  • $begingroup$
    @FutureCake you're welcome. It's customary to hold off before marking any answer as 'the' answer for at least a day or two, so as not to discourage more answers. I didn't add it to my answer, because it's not something I have much experience with, but you may want to look into the SIMD support in .NET if you really want serious performance.
    $endgroup$
    – VisualMelon
    7 hours ago











  • $begingroup$
    @VisualMelon I unchecked the answer, i will get back to it in a few days then :) And i will go and check out what SIMD is! Thanks again!
    $endgroup$
    – FutureCake
    7 hours ago













6














6










6







$begingroup$

You are inconsistent with your use of the this. 'suffix'. Nobody can agree on this, but you should try to be consistent without projects.




I would remove the return values from every method which modifies Vector3: you have 2 copies of Min, and one is very confusing. All of the methods which happen to produce a vector (e.g. Abs, Min, Max) happen to modifythisand then returnthis. This is, in my opinion, a terrible API, and inconsistent with operations (likeLength) which just happen to _not_ produce a vector. Anyone looking at the signature will assume it does not modify the vector, and you just open yourself to all the nightmares associated with mutablestruct`s. Do not mix an immutable/mutable API like you are now, because it will only infuriate your consumers.



Methods like UpdateFromArray are for some reason returning this as well.




You seem to be trying to provide a consistent API between VectorX (which I would just call Vector) and Vector3, so you might consider an IVector interface.



VectorCount is a mildly confusing name. It's the element count, or the length of the vector.




I would suggest not using Math.Pow(, 2) just to square something: even if it is optimised to detect this exact case (I don't know), it is just harder to read than X * X. I would rewrite Dist as (this - other).length for the sake of simplicity.



It's also common to provide a LengthSquared member which returns the sum before the squareroot, since often this is all that is needed and saves an expensive operation.




The parameter names for Min and Max are odd: other would be fine, but again I don't like the API.




I would expect public Vector3(float[] arr) to throw a nice exception if arr is null, or had a length other than 3.



It's good that most of your methods in VectorX are performing range checks (I didn't notice any that didn't). You don't need to put the 'non-exceptional' code in an else for these.




VectorX has many constructors which just provide 'defaults' for others. I would make these call directly the more general versions. E.g.



public VectorX(int vectorCount) : this(vectorCount, default(float))


public VectorX(params float[] values) : this(values, 0, values.Length)



This will significantly reduce redundancy and so improve maintainability.




Your ToString methods could be nicer: I would use string interpolation for Vector3 (i.e. $"<X, Y, Z>") and you should use a StringBuilder for VectorX (currently VectorX.ToString() is a quadratic memory operation when it should be linear).





// do these size comparisions make sense?




No, I would say no; though, it does atleast provide an ordering, so it could be much worse.



The comparisons are also performing 2 unnecessary square-roots (you could compare the LengthSquared).



All your Vector3 comparisions also include a completely redundant ternay clause, which will just get in the way of maintaince efforts and provide a greater surface overwhich bugs can will appear.



I would consider describing negatives in terms of the positives, e.g.



public static bool operator !=(Vector3 left, Vector3 right)

return !(left == right);




Why isn't VectorX.Equals(VectorX other) implemented? What is the point of declaring you implement IEquatable<VectorX> if you do not?




I don't like all your single-line ifs and elses. Even if you don't want to add braces, a line at the end of the condition or else helps significantly with readbility, and reduces the amount of code which is 'off side'.




All of your types and methods would benefit from inline documention (///). This would help to explain the confusing bits of the API, and clarify what methods like Length and Normalise mean.





VectorValues[i] = (VectorValues[i] < 0) ? VectorValues[i] *= -1 :
VectorValues[i];



What is wrong with Math.Abs(VectorValues[i]).





public int VectorCount get return 3;




This can be made a little more concise:



public int VectorCount => 3;


You could do the same with the static Unit and Zero members.




You have some odd line-spacing in places (e.g. around VectorX.NewUnit). I can see no reason for this, so it just looks untidy and makes the code harder to scan.




Consider using an indexer instead of the SetAt and GetAt methods. These could be part of the IVector interface also.




VectorX.VectorValues should not be mutable, and probably shouldn't be public. At the moment it is possible for someone to change VectorValues such that VectorCount will be wrong, or even to change it null and wreck everything. Hide it away so that people can't shoot themselves in the foot with your API.



You could re-write VectorX.VectorCount in terms of VectorValues to reduce redunancy and simplify the constructors.



public int VectorCount => VectorValues.Length;


Less redundancy again means there is less to go wrong, which makes the code easier to maintain.




The VectorX(VectorX) constructor is not good: all the other constructors duplicate the array they are given, but this just copies the reference. There is no point in this constructor (it's the same as a value-copy) and it will only create confusion. If instead it copied the array, then it would be fine.



public VectorX(VectorX vector) : this(vector.VectorValues)



ToArray would also imply a copy, and with VectorValues public does nothing useful. It should take a copy. This is as easy as VectorValues.ToArray() if you have using System.Linq at the top of your file. The fact that it does/doesn't copy should be in the documention.




I'm going to stop for now, but I may add to this answer when I have time, though I'm sure someone else will take up the slack long before then if there is more to be said.






share|improve this answer











$endgroup$



You are inconsistent with your use of the this. 'suffix'. Nobody can agree on this, but you should try to be consistent without projects.




I would remove the return values from every method which modifies Vector3: you have 2 copies of Min, and one is very confusing. All of the methods which happen to produce a vector (e.g. Abs, Min, Max) happen to modifythisand then returnthis. This is, in my opinion, a terrible API, and inconsistent with operations (likeLength) which just happen to _not_ produce a vector. Anyone looking at the signature will assume it does not modify the vector, and you just open yourself to all the nightmares associated with mutablestruct`s. Do not mix an immutable/mutable API like you are now, because it will only infuriate your consumers.



Methods like UpdateFromArray are for some reason returning this as well.




You seem to be trying to provide a consistent API between VectorX (which I would just call Vector) and Vector3, so you might consider an IVector interface.



VectorCount is a mildly confusing name. It's the element count, or the length of the vector.




I would suggest not using Math.Pow(, 2) just to square something: even if it is optimised to detect this exact case (I don't know), it is just harder to read than X * X. I would rewrite Dist as (this - other).length for the sake of simplicity.



It's also common to provide a LengthSquared member which returns the sum before the squareroot, since often this is all that is needed and saves an expensive operation.




The parameter names for Min and Max are odd: other would be fine, but again I don't like the API.




I would expect public Vector3(float[] arr) to throw a nice exception if arr is null, or had a length other than 3.



It's good that most of your methods in VectorX are performing range checks (I didn't notice any that didn't). You don't need to put the 'non-exceptional' code in an else for these.




VectorX has many constructors which just provide 'defaults' for others. I would make these call directly the more general versions. E.g.



public VectorX(int vectorCount) : this(vectorCount, default(float))


public VectorX(params float[] values) : this(values, 0, values.Length)



This will significantly reduce redundancy and so improve maintainability.




Your ToString methods could be nicer: I would use string interpolation for Vector3 (i.e. $"<X, Y, Z>") and you should use a StringBuilder for VectorX (currently VectorX.ToString() is a quadratic memory operation when it should be linear).





// do these size comparisions make sense?




No, I would say no; though, it does atleast provide an ordering, so it could be much worse.



The comparisons are also performing 2 unnecessary square-roots (you could compare the LengthSquared).



All your Vector3 comparisions also include a completely redundant ternay clause, which will just get in the way of maintaince efforts and provide a greater surface overwhich bugs can will appear.



I would consider describing negatives in terms of the positives, e.g.



public static bool operator !=(Vector3 left, Vector3 right)

return !(left == right);




Why isn't VectorX.Equals(VectorX other) implemented? What is the point of declaring you implement IEquatable<VectorX> if you do not?




I don't like all your single-line ifs and elses. Even if you don't want to add braces, a line at the end of the condition or else helps significantly with readbility, and reduces the amount of code which is 'off side'.




All of your types and methods would benefit from inline documention (///). This would help to explain the confusing bits of the API, and clarify what methods like Length and Normalise mean.





VectorValues[i] = (VectorValues[i] < 0) ? VectorValues[i] *= -1 :
VectorValues[i];



What is wrong with Math.Abs(VectorValues[i]).





public int VectorCount get return 3;




This can be made a little more concise:



public int VectorCount => 3;


You could do the same with the static Unit and Zero members.




You have some odd line-spacing in places (e.g. around VectorX.NewUnit). I can see no reason for this, so it just looks untidy and makes the code harder to scan.




Consider using an indexer instead of the SetAt and GetAt methods. These could be part of the IVector interface also.




VectorX.VectorValues should not be mutable, and probably shouldn't be public. At the moment it is possible for someone to change VectorValues such that VectorCount will be wrong, or even to change it null and wreck everything. Hide it away so that people can't shoot themselves in the foot with your API.



You could re-write VectorX.VectorCount in terms of VectorValues to reduce redunancy and simplify the constructors.



public int VectorCount => VectorValues.Length;


Less redundancy again means there is less to go wrong, which makes the code easier to maintain.




The VectorX(VectorX) constructor is not good: all the other constructors duplicate the array they are given, but this just copies the reference. There is no point in this constructor (it's the same as a value-copy) and it will only create confusion. If instead it copied the array, then it would be fine.



public VectorX(VectorX vector) : this(vector.VectorValues)



ToArray would also imply a copy, and with VectorValues public does nothing useful. It should take a copy. This is as easy as VectorValues.ToArray() if you have using System.Linq at the top of your file. The fact that it does/doesn't copy should be in the documention.




I'm going to stop for now, but I may add to this answer when I have time, though I'm sure someone else will take up the slack long before then if there is more to be said.







share|improve this answer














share|improve this answer



share|improve this answer








edited 8 hours ago









Henrik Hansen

12.3k1 gold badge17 silver badges42 bronze badges




12.3k1 gold badge17 silver badges42 bronze badges










answered 8 hours ago









VisualMelonVisualMelon

6,22714 silver badges41 bronze badges




6,22714 silver badges41 bronze badges














  • $begingroup$
    It will be hard writing a complement answer without having some kind of intersection with this answer :p
    $endgroup$
    – dfhwze
    8 hours ago










  • $begingroup$
    About the status of Pow(x, 2), it is not (yet) optimized by the main implementations, there is an open issue for coreclr, so maybe someday
    $endgroup$
    – harold
    7 hours ago










  • $begingroup$
    VisualMelon, Thank you so much for the extensive list of improvements!!! As you may have seen from my code i am no pro ;) But this sure as hell will help with my improvement of skills :) I will be working on this list in the next few days probably :) Please keep adding indeed if you feel like it :)
    $endgroup$
    – FutureCake
    7 hours ago










  • $begingroup$
    @FutureCake you're welcome. It's customary to hold off before marking any answer as 'the' answer for at least a day or two, so as not to discourage more answers. I didn't add it to my answer, because it's not something I have much experience with, but you may want to look into the SIMD support in .NET if you really want serious performance.
    $endgroup$
    – VisualMelon
    7 hours ago











  • $begingroup$
    @VisualMelon I unchecked the answer, i will get back to it in a few days then :) And i will go and check out what SIMD is! Thanks again!
    $endgroup$
    – FutureCake
    7 hours ago
















  • $begingroup$
    It will be hard writing a complement answer without having some kind of intersection with this answer :p
    $endgroup$
    – dfhwze
    8 hours ago










  • $begingroup$
    About the status of Pow(x, 2), it is not (yet) optimized by the main implementations, there is an open issue for coreclr, so maybe someday
    $endgroup$
    – harold
    7 hours ago










  • $begingroup$
    VisualMelon, Thank you so much for the extensive list of improvements!!! As you may have seen from my code i am no pro ;) But this sure as hell will help with my improvement of skills :) I will be working on this list in the next few days probably :) Please keep adding indeed if you feel like it :)
    $endgroup$
    – FutureCake
    7 hours ago










  • $begingroup$
    @FutureCake you're welcome. It's customary to hold off before marking any answer as 'the' answer for at least a day or two, so as not to discourage more answers. I didn't add it to my answer, because it's not something I have much experience with, but you may want to look into the SIMD support in .NET if you really want serious performance.
    $endgroup$
    – VisualMelon
    7 hours ago











  • $begingroup$
    @VisualMelon I unchecked the answer, i will get back to it in a few days then :) And i will go and check out what SIMD is! Thanks again!
    $endgroup$
    – FutureCake
    7 hours ago















$begingroup$
It will be hard writing a complement answer without having some kind of intersection with this answer :p
$endgroup$
– dfhwze
8 hours ago




$begingroup$
It will be hard writing a complement answer without having some kind of intersection with this answer :p
$endgroup$
– dfhwze
8 hours ago












$begingroup$
About the status of Pow(x, 2), it is not (yet) optimized by the main implementations, there is an open issue for coreclr, so maybe someday
$endgroup$
– harold
7 hours ago




$begingroup$
About the status of Pow(x, 2), it is not (yet) optimized by the main implementations, there is an open issue for coreclr, so maybe someday
$endgroup$
– harold
7 hours ago












$begingroup$
VisualMelon, Thank you so much for the extensive list of improvements!!! As you may have seen from my code i am no pro ;) But this sure as hell will help with my improvement of skills :) I will be working on this list in the next few days probably :) Please keep adding indeed if you feel like it :)
$endgroup$
– FutureCake
7 hours ago




$begingroup$
VisualMelon, Thank you so much for the extensive list of improvements!!! As you may have seen from my code i am no pro ;) But this sure as hell will help with my improvement of skills :) I will be working on this list in the next few days probably :) Please keep adding indeed if you feel like it :)
$endgroup$
– FutureCake
7 hours ago












$begingroup$
@FutureCake you're welcome. It's customary to hold off before marking any answer as 'the' answer for at least a day or two, so as not to discourage more answers. I didn't add it to my answer, because it's not something I have much experience with, but you may want to look into the SIMD support in .NET if you really want serious performance.
$endgroup$
– VisualMelon
7 hours ago





$begingroup$
@FutureCake you're welcome. It's customary to hold off before marking any answer as 'the' answer for at least a day or two, so as not to discourage more answers. I didn't add it to my answer, because it's not something I have much experience with, but you may want to look into the SIMD support in .NET if you really want serious performance.
$endgroup$
– VisualMelon
7 hours ago













$begingroup$
@VisualMelon I unchecked the answer, i will get back to it in a few days then :) And i will go and check out what SIMD is! Thanks again!
$endgroup$
– FutureCake
7 hours ago




$begingroup$
@VisualMelon I unchecked the answer, i will get back to it in a few days then :) And i will go and check out what SIMD is! Thanks again!
$endgroup$
– FutureCake
7 hours ago











FutureCake is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded

















FutureCake is a new contributor. Be nice, and check out our Code of Conduct.












FutureCake is a new contributor. Be nice, and check out our Code of Conduct.











FutureCake is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f229256%2fc-vector-library%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу

Israel Cuprins Etimologie | Istorie | Geografie | Politică | Demografie | Educație | Economie | Cultură | Note explicative | Note bibliografice | Bibliografie | Legături externe | Meniu de navigaresite web oficialfacebooktweeterGoogle+Instagramcanal YouTubeInstagramtextmodificaremodificarewww.technion.ac.ilnew.huji.ac.ilwww.weizmann.ac.ilwww1.biu.ac.ilenglish.tau.ac.ilwww.haifa.ac.ilin.bgu.ac.ilwww.openu.ac.ilwww.ariel.ac.ilCIA FactbookHarta Israelului"Negotiating Jerusalem," Palestine–Israel JournalThe Schizoid Nature of Modern Hebrew: A Slavic Language in Search of a Semitic Past„Arabic in Israel: an official language and a cultural bridge”„Latest Population Statistics for Israel”„Israel Population”„Tables”„Report for Selected Countries and Subjects”Human Development Report 2016: Human Development for Everyone„Distribution of family income - Gini index”The World FactbookJerusalem Law„Israel”„Israel”„Zionist Leaders: David Ben-Gurion 1886–1973”„The status of Jerusalem”„Analysis: Kadima's big plans”„Israel's Hard-Learned Lessons”„The Legacy of Undefined Borders, Tel Aviv Notes No. 40, 5 iunie 2002”„Israel Journal: A Land Without Borders”„Population”„Israel closes decade with population of 7.5 million”Time Series-DataBank„Selected Statistics on Jerusalem Day 2007 (Hebrew)”Golan belongs to Syria, Druze protestGlobal Survey 2006: Middle East Progress Amid Global Gains in FreedomWHO: Life expectancy in Israel among highest in the worldInternational Monetary Fund, World Economic Outlook Database, April 2011: Nominal GDP list of countries. Data for the year 2010.„Israel's accession to the OECD”Popular Opinion„On the Move”Hosea 12:5„Walking the Bible Timeline”„Palestine: History”„Return to Zion”An invention called 'the Jewish people' – Haaretz – Israel NewsoriginalJewish and Non-Jewish Population of Palestine-Israel (1517–2004)ImmigrationJewishvirtuallibrary.orgChapter One: The Heralders of Zionism„The birth of modern Israel: A scrap of paper that changed history”„League of Nations: The Mandate for Palestine, 24 iulie 1922”The Population of Palestine Prior to 1948originalBackground Paper No. 47 (ST/DPI/SER.A/47)History: Foreign DominationTwo Hundred and Seventh Plenary Meeting„Israel (Labor Zionism)”Population, by Religion and Population GroupThe Suez CrisisAdolf EichmannJustice Ministry Reply to Amnesty International Report„The Interregnum”Israel Ministry of Foreign Affairs – The Palestinian National Covenant- July 1968Research on terrorism: trends, achievements & failuresThe Routledge Atlas of the Arab–Israeli conflict: The Complete History of the Struggle and the Efforts to Resolve It"George Habash, Palestinian Terrorism Tactician, Dies at 82."„1973: Arab states attack Israeli forces”Agranat Commission„Has Israel Annexed East Jerusalem?”original„After 4 Years, Intifada Still Smolders”From the End of the Cold War to 2001originalThe Oslo Accords, 1993Israel-PLO Recognition – Exchange of Letters between PM Rabin and Chairman Arafat – Sept 9- 1993Foundation for Middle East PeaceSources of Population Growth: Total Israeli Population and Settler Population, 1991–2003original„Israel marks Rabin assassination”The Wye River Memorandumoriginal„West Bank barrier route disputed, Israeli missile kills 2”"Permanent Ceasefire to Be Based on Creation Of Buffer Zone Free of Armed Personnel Other than UN, Lebanese Forces"„Hezbollah kills 8 soldiers, kidnaps two in offensive on northern border”„Olmert confirms peace talks with Syria”„Battleground Gaza: Israeli ground forces invade the strip”„IDF begins Gaza troop withdrawal, hours after ending 3-week offensive”„THE LAND: Geography and Climate”„Area of districts, sub-districts, natural regions and lakes”„Israel - Geography”„Makhteshim Country”Israel and the Palestinian Territories„Makhtesh Ramon”„The Living Dead Sea”„Temperatures reach record high in Pakistan”„Climate Extremes In Israel”Israel in figures„Deuteronom”„JNF: 240 million trees planted since 1901”„Vegetation of Israel and Neighboring Countries”Environmental Law in Israel„Executive branch”„Israel's election process explained”„The Electoral System in Israel”„Constitution for Israel”„All 120 incoming Knesset members”„Statul ISRAEL”„The Judiciary: The Court System”„Israel's high court unique in region”„Israel and the International Criminal Court: A Legal Battlefield”„Localities and population, by population group, district, sub-district and natural region”„Israel: Districts, Major Cities, Urban Localities & Metropolitan Areas”„Israel-Egypt Relations: Background & Overview of Peace Treaty”„Solana to Haaretz: New Rules of War Needed for Age of Terror”„Israel's Announcement Regarding Settlements”„United Nations Security Council Resolution 497”„Security Council resolution 478 (1980) on the status of Jerusalem”„Arabs will ask U.N. to seek razing of Israeli wall”„Olmert: Willing to trade land for peace”„Mapping Peace between Syria and Israel”„Egypt: Israel must accept the land-for-peace formula”„Israel: Age structure from 2005 to 2015”„Global, regional, and national disability-adjusted life years (DALYs) for 306 diseases and injuries and healthy life expectancy (HALE) for 188 countries, 1990–2013: quantifying the epidemiological transition”10.1016/S0140-6736(15)61340-X„World Health Statistics 2014”„Life expectancy for Israeli men world's 4th highest”„Family Structure and Well-Being Across Israel's Diverse Population”„Fertility among Jewish and Muslim Women in Israel, by Level of Religiosity, 1979-2009”„Israel leaders in birth rate, but poverty major challenge”„Ethnic Groups”„Israel's population: Over 8.5 million”„Israel - Ethnic groups”„Jews, by country of origin and age”„Minority Communities in Israel: Background & Overview”„Israel”„Language in Israel”„Selected Data from the 2011 Social Survey on Mastery of the Hebrew Language and Usage of Languages”„Religions”„5 facts about Israeli Druze, a unique religious and ethnic group”„Israël”Israel Country Study Guide„Haredi city in Negev – blessing or curse?”„New town Harish harbors hopes of being more than another Pleasantville”„List of localities, in alphabetical order”„Muncitorii români, doriți în Israel”„Prietenia româno-israeliană la nevoie se cunoaște”„The Higher Education System in Israel”„Middle East”„Academic Ranking of World Universities 2016”„Israel”„Israel”„Jewish Nobel Prize Winners”„All Nobel Prizes in Literature”„All Nobel Peace Prizes”„All Prizes in Economic Sciences”„All Nobel Prizes in Chemistry”„List of Fields Medallists”„Sakharov Prize”„Țara care și-a sfidat "destinul" și se bate umăr la umăr cu Silicon Valley”„Apple's R&D center in Israel grew to about 800 employees”„Tim Cook: Apple's Herzliya R&D center second-largest in world”„Lecții de economie de la Israel”„Land use”Israel Investment and Business GuideA Country Study: IsraelCentral Bureau of StatisticsFlorin Diaconu, „Kadima: Flexibilitate și pragmatism, dar nici un compromis în chestiuni vitale", în Revista Institutului Diplomatic Român, anul I, numărul I, semestrul I, 2006, pp. 71-72Florin Diaconu, „Likud: Dreapta israeliană constant opusă retrocedării teritoriilor cureite prin luptă în 1967", în Revista Institutului Diplomatic Român, anul I, numărul I, semestrul I, 2006, pp. 73-74MassadaIsraelul a crescut in 50 de ani cât alte state intr-un mileniuIsrael Government PortalIsraelIsraelIsraelmmmmmXX451232cb118646298(data)4027808-634110000 0004 0372 0767n7900328503691455-bb46-37e3-91d2-cb064a35ffcc1003570400564274ge1294033523775214929302638955X146498911146498911

Кастелфранко ди Сопра Становништво Референце Спољашње везе Мени за навигацију43°37′18″ СГШ; 11°33′32″ ИГД / 43.62156° СГШ; 11.55885° ИГД / 43.62156; 11.5588543°37′18″ СГШ; 11°33′32″ ИГД / 43.62156° СГШ; 11.55885° ИГД / 43.62156; 11.558853179688„The GeoNames geographical database”„Istituto Nazionale di Statistica”проширитиууWorldCat156923403n850174324558639-1cb14643287r(подаци)