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.
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?
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.
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))