Making random triangles in a square

Hi people, village idiot here with another inane and rare topic in scripting support.

I’ll cut the facts and cut to the chase:

Let’s say I have a random square, and at a specific and random point in that square I leave a dot there. How to I calculate triangles so the two vertices on the triangle’s base are also the points on the edge of the square and their Apex/Vortex (randomly generated triangles so we don’t know what it’ll be) is connecting to this specific random point?

Hope I explained well.

I’m just looking to be pointed in the right direction, or you could just launch api’s and lua at me (i’m trying to do this myself, as much as I would appreciate you creating a system at me I simply wouldn’t use it). Less convenient but ok.

Thanks for reading.

1 Like

What exactly do you mean by their base is touching the perimeter?
Did you mean that the two vertices on the triangle’s base are also the points on the edge of the square?

2 Likes

Yes I did. I’ll edit the post for some more clarity. Sorry about that!

1 Like

Okay, so now you can easily generate the point inside and these two points on the edge, what do you need to do with that next, because these three points define the triangle unambiguously already.

1 Like

I just need the triangles to fill the space in the already empty square while the Apex/Vortex is touching the given point.

1 Like

But what do you mean by ‘fill’? Is it GUI? Do you want to fill a space with parts? Because now it depends what you are willing to achieve.

1 Like

Parts as this is hopefully going to be some contraption which’ll I’ll never be proud of. If broader words, glass hopefully.

Edit: sorry for the inconsistent-in-time replies, currently doing a Technical Drawing assignment.

1 Like

Okay, so now I advice to find a way to generate a triangle from 3 vertices in 3D space.
This seems to be a good tutorial: https://github.com/EgoMoose/Articles/blob/master/3d%20triangles/3D%20triangles.md

And here someone has attached a script that you can check out too: 3D Triangle Creation - #5 by Fractality_alt

1 Like

It seems like you are looking for Polygon Triangulation maybe, if so there are a couple examples on the forum

Like This one, I also have some dualaney triangulation cde that’s based on another lua triangulation library that I can link if you need it

He is not looking for polygon triangulation but he wants to generate a triangle from 3 vertices.

1 Like

Here’s a script I wrote for building triangles out of parts from three position vectors:
(its old so there are probably ways it can be cleaned up and you might have to tweak some things to fit your use case)

function triangle(a, b, c, thickness)
	thickness = thickness or 1
	local ab = (a - b).Magnitude
	local bc = (b - c).Magnitude
	local ca = (c - a).Magnitude
	local area = ((b - a):Cross(c - a)).Magnitude / 2
	
	-- re-arrange the variables to make sure AB represents the longest side.
	if (bc > ab or ca > ab) then
		local olda = a
		local oldb = b
		local oldc = c
		if (ca > bc) then
			a = oldc
			b = olda
			c = oldb
		else
			a = oldb
			b = oldc
			c = olda
		end
		ab = (a - b).Magnitude
		bc = (b - c).Magnitude
		ca = (c - a).Magnitude
	end
	
	-- CFrame stuff
	-- h is the height
	-- ah and bh are the vertical distances from the base to where the parts must be positioned.
	-- hcf is a cframe representing the point where a perpendicular line from point C intersects the base.
	local h = 2 * area / ab
	local ah = math.sqrt((ca ^ 2) - (h ^ 2))
	local bh = ab - ah
	
	local up = CFrame.new((CFrame.new(a, b) * CFrame.new(0, 0, -ah)).Position, c).LookVector
	local forward = (b - a).Unit
	local right = forward:Cross(up)
	local up2 = right:Cross(forward)
	local hcf = CFrame.fromMatrix(a, right, up2) * CFrame.new(0, 0, -ah)
	
	local parts = Instance.new("Model")
	parts.Name = "Triangle"
	parts.Parent = workspace
	
	local part1 = Instance.new("WedgePart")
	part1.Size = Vector3.new(thickness, h, bh)
	part1.CFrame = hcf * CFrame.new(0, h / 2, bh / -2)
	part1.Name = "Part1"
	part1.Anchored = true
	part1.Parent = parts
	
	local part2 = Instance.new("WedgePart")
	part2.Size = Vector3.new(thickness, h, ah)
	part2.CFrame = hcf * CFrame.new(0, h / 2, ah / 2) * CFrame.Angles(0, math.pi, 0)
	part2.Name = "Part2"
	part2.Anchored = true
	part2.Parent = parts
	
	return parts
end


triangle(Vector3.new(0, 0, 0), Vector3.new(10, 0, 0), Vector3.new(10, 0, 10))

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

I wish I could mark both as solutions, I appreciate your code but I find the tutorial @TokoNanami has linked very useful.

I may tweak your code, thanks for the input from both of you :slight_smile:
and more importantly an award for being able to translate what I was trying to say

2 Likes