How would I draw a 2D Triangle

Hello, I am trying to draw a 2D Triangle in Roblox.
I am not sure how to do that though.

I managed to draw the edges of the triangle:

image

I don’t know how I will fill this though, I am really bad at math.

Here is the function that makes the lines:

function module:DrawLine(Points)
	local SettingsModule = require(MainModule.SettingsModule)
	
	local Distance = (Points[1] - Points[2]).Magnitude
	local Center = (Points[1] + Points[2]) / 2
	local Rotation = math.atan2(Points[1].Y - Points[2].Y, Points[1].X - Points[2].X)
	
	local NewLine = MainModule.LineModule.Line:Clone()
	
	NewLine.Position = UDim2.new(0, Center.X, 0, Center.Y)
	NewLine.Rotation = math.deg(Rotation)
	NewLine.Size = UDim2.new(0, Distance, 0, SettingsModule.Thickness)
	NewLine.Parent = MainModule.Parent.Polygons
end
2 Likes

Also I tried searching but I didn’t find an answer.

1 Like

I am a bit confused on what you mean, This is a 2D Gui Triangle btw.
Not 3D.

image

1 Like

I also want to draw the 2D Triangle with programming.

1 Like

Here is the function that draws the edges:

function module:DrawPolygon(Points)
	local LineModule = require(MainModule.LineModule)
	
	LineModule:DrawLine({Points[1], Points[2]})
	LineModule:DrawLine({Points[2], Points[3]})
	LineModule:DrawLine({Points[3], Points[1]})
end
1 Like
  1. You will need use right triangle image.
  2. IDK if this will be helpfull to you, but I experimented with mesh cutting in 3D space, and I have found this Triangle creation function. It will calculate CFrame and SIze of both Wedges.
local function CalculateTriangle(a,b,c)
	local ax,ay,az=a.x,a.y,a.z
	local bx,by,bz=b.x,b.y,b.z
	local cx,cy,cz=c.x,c.y,c.z
	local px,py,pz=ax,ay,az
	local tx,ty,tz=cx,cy,cz
	local v0x,v0y,v0z=bx-ax,by-ay,bz-az
	local v1x,v1y,v1z=cx-ax,cy-ay,cz-az
	local d01=v0x*v1x+v0y*v1y+v0z*v1z--In case of case 2 triangle. Speeds it up a lot
	local d=d01/(v0x*v0x+v0y*v0y+v0z*v0z)
	if d>0 then
		if d>1 then--Case 2 triangle
			tx,ty,tz=bx,by,bz
			v0x,v0y,v0z,v1x,v1y,v1z=v1x,v1y,v1z,v0x,v0y,v0z
			d=d01/(v0x*v0x+v0y*v0y+v0z*v0z)
			--		else Case 1 triangle			
		end
	else--Case 3 triangle. Don't get a case 3 triangle lol. Slowest of them all.
		px,py,pz=cx,cy,cz
		tx,ty,tz=ax,ay,az
		v0x,v0y,v0z=bx-cx,by-cy,bz-cz
		v1x,v1y,v1z=-v1x,-v1y,-v1z
		d=(v0x*v1x+v0y*v1y+v0z*v1z)/(v0x*v0x+v0y*v0y+v0z*v0z)
	end
	local y0x,y0y,y0z=tx-px-d*v0x,ty-py-d*v0y,tz-pz-d*v0z
	local l0,l1=(v0x*v0x+v0y*v0y+v0z*v0z)^0.5, (y0x*y0x+y0y*y0y+y0z*y0z)^0.5
	local hy0x,hy0y,hy0z,hd=y0x*0.5,y0y*0.5,y0z*0.5,d*0.5
	local tf=0.5+hd
	local oyx,oyy,oyz,ozx,ozy,ozz=y0x/l1,y0y/l1,y0z/l1,v0x/l0,v0y/l0,v0z/l0
	local oxx,oxy,oxz=oyy*ozz-oyz*ozy,oyz*ozx-oyx*ozz,oyx*ozy-oyy*ozx
	local Tris1CF = CFrame.new(v0x*hd+hy0x+px,v0y*hd+hy0y+py,v0z*hd+hy0z+pz, oxx,oyx,ozx,oxy,oyy,ozy,oxz,oyz,ozz)
	local Tris2CF = CFrame.new(v0x*tf+hy0x+px,v0y*tf+hy0y+py,v0z*tf+hy0z+pz,-oxx,oyx,-ozx,-oxy,oyy,-ozy,-oxz,oyz,-ozz)
	local Size1 = Vector3.new(0.001,l1,d*l0)
	local Size2 = Vector3.new(0.001,l1,(1-d)*l0)
	return Tris1CF, Size1, Tris2CF, Size2
end

If you understand anything here, you need to convert this into 2D from 3D
(Need to mention that this part of script isn’t mine, and due to fact that I found this piece of code 1 year ago, I don’t remember it’s creator)

1 Like

I am having a VERY hard time understanding ANYTHING here to be honest.

And I don’t think this can be converted into 2D.

1 Like

Only thing I can suggest you is either try to find triangulation on dev forum, or use above code with Z set to 0.

1 Like

I am really bad at math, I have tried searching, but I didn’t find any answer.
Also I don’t know how I will set the Z to zero because I can barely understand anything in this script.

a,b,c there represent three Vector3. Use instead Vector3.new(X, Y, 0).
(Also, I’m trying to find rn smth simpler)

I found one topic where you can find easier for understanding triangulation:

I am pretty sure this creates two wedges (Which is not an object that can be used in GUI)

I also found this: (He took some of the code from here.)

This works in 3d only because it uses Wedges.

I think I found a way to do this, But It’s not gonna work because it will be REALLY slow.

I think you are right, I can make a triangle image and use two of them to make the triangle.
I am gonna try it now, I hope it works.

Yes!
It worked!

Here is the answer:

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