How to find the center position from a table of positions?


#1

So, I got a table with like 20 vector3 positions. How do I get the midpoint of all those positions? I know the mathematical equation for 2 positions, but I am not able to find anything on x positions.


#2

Someone correct me if I’m wrong:

Find the minimum and maximum values for each axis, then just interpolate to the center of each. In the case of Vector3s, it could look like this:

local vectors = {...}

local minX, maxX = math.huge, -math.huge
local minY, maxY = math.huge, -math.huge
local minZ, maxZ = math.huge, -math.huge

for _,v in pairs(vectors) do
	if (v.X < minX) then minX = v.X end
	if (v.X > maxX) then maxX = v.X end
	if (v.Y < minY) then minY = v.Y end
	if (v.Y > maxY) then maxY = v.Y end
	if (v.Z < minZ) then minZ = v.Z end
	if (v.Z > maxZ) then maxZ = v.Z end
end

local minVector = Vector3.new(minX, minY, minZ)
local maxVector = Vector3.new(maxX, maxY, maxZ)

local center = minVector:Lerp(maxVector, 0.5)

I’m aware that there are like 100000 different ways to do the above operations. But I felt it was more clear to write it the way it is.


#3

Thank you. It works.