- Home /
get the average position of objects in array?
I have 4 objects in array, objects vectors:
1, 1, 1,
1, 2, 1,
1, 3, 1,
2, 4, 1,
How can I retrieve 1.25, 2,50, 1? all vectors from the arrays objects and use that to get a new vector3?
why not just go through the array, sum all x,y, z values and then divide each 1 with the element count ? something like ...
//just an example, not actual code Vector3[] array = {..} Vector3 vec = new Vector3(0.0f, 0.0f, 0.0f);
for(int i=0; i < array.length; i++){ vec.x += array[i].x vec.y += array[i].y vec.z += array[i].z } vec.x = vec.x / array.length vec.y = vec.y / array.length vec.z = vec.z / array.length
can't remember if "vec.z +=" should work or not, but you should get the general idea from this
Answer by alap soni · Oct 02, 2014 at 10:25 AM
Access all x values, add them and take average, repeat this for all y and z.
logic c#: Go through this link for logic of multidimensional array [1]: http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx
So you get 1.25,2.50 and 1. now make a new vector3 as Vector3 v3=new Vector3(1.25,2.50,1);
And if you want magnitude as one , just normalize it. v3.normalize();