Generating filled N-Gon using a script

How could I generate a filled n-gon with a set diameter or radius using a script? General format would be the following…

function n_gon(Sides : number, Radius : number)
  return
end
1 Like

Hi, Im not really sure what you mean by generate filled N-Gon. Should it be made up of parts or should it be a 2d texture/ui element

You can use EditableMesh to create custom meshes, and you can search online for an n-gon generation algorithm. And then set the EditableMesh’s verticies to match the verticies of the generated polygon.

EditableMesh tutorial by Catargo:

EditableMesh / EditableImage DevForum post:

The editable mesh approach mentioned in the reply above is an easier and more efficient approach than using parts but it cannot be used in live games at the moment.
If you want to create the n-gon with parts, you can use this function. You’ll need to set the parent of the returned model.

local function createNGon(sides: number, radius: number, thickness: number, center: Vector3): Model
	local partParent = Instance.new("Model")

	local angleIncrement = 2 * math.pi / sides

	for isoscelesTriangleIndex = 0, sides - 1 do
		local rotationAngle = isoscelesTriangleIndex * angleIncrement
		local side1Angle, side2Angle = rotationAngle - angleIncrement / 2, rotationAngle + angleIncrement / 2
		local pos1, pos2 = center + .5 * radius * Vector3.new(math.cos(side1Angle), 0, math.sin(side1Angle)), center + .5 * radius * Vector3.new(math.cos(side2Angle), 0, math.sin(side2Angle))
		local cf1, cf2 = CFrame.fromMatrix(pos1, Vector3.yAxis, -Vector3.new(math.cos(rotationAngle), 0, math.sin(rotationAngle))), CFrame.fromMatrix(pos2, -Vector3.yAxis, -Vector3.new(math.cos(rotationAngle), 0, math.sin(rotationAngle)))
		local size = Vector3.new(thickness, math.cos(angleIncrement / 2) * radius, math.sin(angleIncrement / 2) * radius)

		local part1, part2 = Instance.new("Part"), Instance.new("Part")
		part1.Shape, part2.Shape = Enum.PartType.Wedge, Enum.PartType.Wedge
		part1.Anchored, part2.Anchored = true, true
		part1.CFrame, part2.CFrame = cf1, cf2
		part1.Size, part2.Size = size, size
		part1.Parent, part2.Parent = partParent, partParent
	end

	return partParent
end

Thanks for this! If I run into any errors i’ll be sure to reach out.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.