?
Loading
?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 22, 2023, 10:39 PM EST
To Average vector, you can do like below.
var newVector = new Vector3( list.Average(x=>x.X), list.Average(x=>x.Y), list.Average(x=>x.Z));list.Average(x=>x.Z) will return average value of Z from all elements from listFor Vector Sum (double) type :
double vectorSum = Vector.Dot(yourDoubleVector, Vector.One);The
Dotmethod calculates the dot product of the two vectors, which is defined for two vectorsAandBof sizenasA1 * B1 + A2 * B2 + ... + An * BnReference
https://stackoverflow.com/questions/33170643/finding-the-average-of-vectors-in-a-list
https://stackoverflow.com/questions/35514685/sum-of-elements-in-system-numerics-vectort-in-net-4-6
Thanks
Amit MohantyRank #112 · 20K reputationPosted Jan 22, 2023, 11:35 PM EST
You can create a custom extension method that will allow you to calculate the sum and average of a Vector3 in a more readable way and use it as you would use the built-in Sum and Average methods.
public static class Vector3Extensions { public static float Sum(this Vector3 vector) { return vector.X + vector.Y + vector.Z; } public static float Average(this Vector3 vector) { return vector.Sum() / 3; } }Then you can use it as
Vector3 vector = new Vector3(10, 20, 30); Console.WriteLine("The sum of the vector is: " + vector.Sum()); Console.WriteLine("The average of the vector is: " + vector.Average());