- Any oddities are the fault of Google Translator
- Everything in this post is just an idea, not a full and high-quality implementation
I agree with @socean’s answer. It’s just that there is also the possibility of implementing the faces of a polygon (polygon), through trigonometric functions.
To do this, you only need the formula for obtaining the coordinates of the n-gon
function Polygon:_get_points_of_polygon(n,r)
local points = {}
for i = 1,n do
table.insert(points,Vector2.new(r* math.cos(2 * math.pi * i / n),r *math.sin(2 * math.pi * i / n)))
end
return points
end
And of course safely paint it all
Of course, it is possible that I cheated a little with the painting of the polygon, but let it remain an idea, and an incomplete implementation
function Polygon:_create_line(Vec1,Vec2,Width,Lenght,Parent)
Width=Width or 0.2;Lenght=Lenght or 0.2
return _create({"Part",Parent= Parent or workspace,Anchored = true,Shape = Enum.PartType.Block,
Size = Vector3.new(Width,(Vec1-Vec2).Magnitude,Lenght),
["CFrame"] = CFrame.new(
Vec1:Lerp(Vec2,1/2),
Vec2
)*CFrame.Angles(math.pi/2,0,0),Name=Polygon.NAMES.LINE
})
end
Code example:
local _create=setmetatable({},{ __call = function(tab,props)
local Object = Instance.new(props[1] or props["ClassName"])
for Property,Value in pairs(props) do
if Property~= "ClassName" and Property~=1 then
Object[Property] = Value
end
end return Object end })
local Polygon = {} do
Polygon.NAMES = {
POLYGON = "PolygonModel",
LINE = "PolygonPart",
PRIMARYPART= "PolygonPrimaryPart",
CENTER = "Center"
}
Polygon.METAINDEXES = {
POSITION = "Position",
CFRAME = "CFrame",
}
function Polygon:_create_line(Vec1,Vec2,Width,Lenght,Parent)
Width=Width or 0.2;Lenght=Lenght or 0.2
return _create({"Part",Parent= Parent or workspace,Anchored = true,Shape = Enum.PartType.Block,
Size = Vector3.new(Width,(Vec1-Vec2).Magnitude,Lenght),
["CFrame"] = CFrame.new(
Vec1:Lerp(Vec2,1/2),
Vec2
)*CFrame.Angles(math.pi/2,0,0),Name=Polygon.NAMES.LINE
})
end
function Polygon:_get_points_of_polygon(n,r)
local points = {}
for i = 1,n do
table.insert(points,Vector2.new(r* math.cos(2 * math.pi * i / n),r *math.sin(2 * math.pi * i / n)))
end
return points
end
function Polygon.new(N,R,WidthLine,LenghtLine,Parent) -- N - Number of vertices; R - radius
----
Parent=Parent or workspace
---
local self = {
Model = _create({"Model",Name=Polygon.NAMES.POLYGON,Parent=Parent})
}
self.PrimaryPart = _create({"Part",Anchored = true,Shape=Enum.PartType.Block,
Size = Vector3.new(R,1,R),Transparency=1,
Position = Vector3.new(nil),Name=Polygon.NAMES.PRIMARYPART,Parent=self.Model});self.Model.PrimaryPart =self.PrimaryPart
self.AllLines = {}
self.AllPoints = Polygon:_get_points_of_polygon(N,R)
self.Center=nil
self.Suburbs ={}
for Number,_ in ipairs(self.AllPoints) do
if self.AllPoints[Number+1] then
table.insert(self.AllLines,Polygon:_create_line(Vector3.new(self.AllPoints[Number].X,0,self.AllPoints[Number].Y),Vector3.new(self.AllPoints[Number+1].X,0,self.AllPoints[Number+1].Y),WidthLine,LenghtLine,self.Model))
else
table.insert(self.AllLines,Polygon:_create_line(Vector3.new(self.AllPoints[1].X,0,self.AllPoints[1].Y),Vector3.new(self.AllPoints[Number].X,0,self.AllPoints[Number].Y),WidthLine,LenghtLine,self.Model ))
end
end
-------Center Polygon(Painting)
self.Center= _create({"Part",
Shape=Enum.PartType.Cylinder,
Anchored=true,
Orientation = Vector3.new(0,0,90),
Size=Vector3.new(0.2,R*(math.pi/2),R*(math.pi/2)),
Parent=self.Model,
Name = Polygon.NAMES.CENTER,
Position=self.PrimaryPart.Position
})
for _,obj in ipairs(self.AllLines) do
table.insert(self.Suburbs,_create({"Part",
Shape=Enum.PartType.Block,
Anchored=true,
Size=Vector3.new(R/2,obj.Size.Y,0.2),
Parent=self.Model,
Name = Polygon.NAMES.CENTER,
CFrame=CFrame.fromMatrix(obj.CFrame.Position/1.5,obj.CFrame.XVector,obj.CFrame.YVector,obj.CFrame.ZVector)
}))
end
-------
return setmetatable(self,{
__tostring=function()return "Polygon" end,
__newindex = function(t,k,v)
if k==Polygon.METAINDEXES.CFRAME then
t.Model:SetPrimaryPartCFrame(v)
end
if k==Polygon.METAINDEXES.POSITION then
t.Model:MoveTo(v)
end
end
})
end
end
Notes to the sample code:
To create a polygon model with n-vertices of r-radius, you can use the Polygon class constructor
Polygon.new(5,2) ---5 Vertexes with radius 2 studs
There are also additional arguments to this function
Polygon.new([[Number of vertices]],[[Radius]] ,[[Polygon Line Width]], [[Polygon line length]],[[Polygon model parents]])
An object of the Polygon class has attributes:
PrimaryPart -- Primary Part of the model
AllLines -- Contours of the model
AllPoints -- Vector 2 coordinates of points
Center -- Central cylinder
Suburbs -- The inner edges of the polygon (they cover the holes)