Draw Rectangle from 2 points

Basically, I have 2 parts (we’ll call them points here)

I need to make a rectangle with these 2 points but I’m not sure how it’s done programmatically. This will basically place 2 additional parts on the missing corners and then draw the rectangle with the 4 points.

I’ll be using this to place parts in this rectangle similar to Minecraft block printers.

Visual Reference

Video Reference for Minecraft block printer : click

local Workspace = workspace

local function create3DQuadrilateral(pos1, pos2)
	local coords = {{}, {}, {}}
	for index, axis in ipairs({"X", "Y", "Z"}) do
		table.insert(coords[index], pos1[axis])
		table.insert(coords[index], pos2[axis])
	end
	for _, coord in ipairs(coords) do
		table.sort(coord)
	end
	
	local Part = Instance.new("Part")
	Part.Anchored = true
	Part.Position = Vector3.new(coords[1][1], coords[2][1], coords[3][1])
	Part.Size = Vector3.new(coords[1][2] - coords[1][1], coords[2][2] - coords[2][1], coords[3][2] - coords[3][1])
	Part.Parent = Workspace
	return Part
end

local Part = create3DQuadrilateral(Vector3.new(-2, 7, 3), Vector3.new(10, 4, -5))
print(Part.Position) --(-2, 4, -5)
print(Part.Size) --(12, 3, 8)

Quadrilateral.rbxl (29.3 KB)

1 Like