Fit a part between 4 points?

Hey! How would I scale and position a part between 4 positions?

Example:

(the distance of the points to the origin can vary)

Get the average position (the center) of the positions:
pos1 (left), pos2 (right), pos3 (up), pos4 (down)

local avgPos = (pos1 + pos2 + pos3 + pos4) / 4

Now, we need the size of the part that fits:

local sizeX = pos1.X - pos2.X
local sizeZ = pos3.Z - pos4.Z

Lastly, create the part:

local Part = Instance.new("Part")
Part.Size = Vector3.new(sizeX, 1, sizeZ)
Part.Position = avgPos
2 Likes

Physically, or with a script?
Are all 4 points on the same plane?
If this is for a flat surface then you could use the Gapfill Plugin (I’m not on the site so I can’t give you the exact link, but ensure it’s made by the original maker.

For some reason my part is getting scaled to a very thin slice?

Here’s my code


local part = script.Parent
local origin = part.Position

local hitPositions = {}

local directions = {
	Vector3.new(-10,0,0); -- left
	Vector3.new(10,0,0); -- right
	Vector3.new(0,0,10); -- top
	Vector3.new(0,0,-10); -- bottom
}

for _,direction in ipairs(directions) do
	local result = workspace:Raycast(origin, direction*35)
	local hitPosition = (result and result.Position) or origin + direction*35
	table.insert(hitPositions,hitPosition)
end


local avgPos = (hitPositions[1] + hitPositions[2] + hitPositions[3] + hitPositions[4]) / 4

local sizeX = hitPositions[1].X - hitPositions[2].X
local sizeZ = hitPositions[3].Z - hitPositions[4].Z

part.Size = Vector3.new(sizeX, 1, sizeZ)
part.Position = avgPos

if the points make a square or a rectangle its pretty easy to fit
find the length of the edges
by (point1- point 2).magnitude
find the other edges length
by (point1- point 3).magnitude
scale ur x and z of the part to these

position is the midpoint of two diagnal points so
position = (Part3-Part2)/2

and then if u need orientation say because its a bit more chunky

or if i didnt pay full attention and u want it done so the points arent going to lie on the vertecies then you want it to be more like

part z size 1 (the horizontal side)= (point3-point1).Magnitude
part x size (the vertical side) = (point4-point2).Magnitude
part x position = (point4-point2)/2
part z position = (point3-point1)/2
part y position = any of the points position assuming they are all at the same height

(otherwise orientation because more chuncky as previously stated)

I’m trying to figure out positioning as size is working, heres my code for pos:

local x = (hitPositions[1] - hitPositions[2]).Magnitude
local z = (hitPositions[3] - hitPositions[4]).Magnitude

local pos = Vector3.new( (hitPositions[1]+hitPositions[2])/2 , 1 , (hitPositions[3]+hitPositions[4])/2 )

part.Position += pos

result:

I’m about to give up on this


@VegetationBush provided a valid solution for the position of the part.

It would be the average of the 4 points…

or if you want to do the x and y positions separately, then it would be the midpoint between the pair

x = (left.x + right.x) / 2
y = (top.y + bottom.y) / 2

I have size working, but why isnt position working?