I’m trying to make a simple diagonal line on a rectangle. Something like this: Imgur: The magic of the Internet, but where the ends of the diagonal actually meet up with the corner of the rectangle.
Dimensions of the Rectangle: (LxWxT) 7x5x2
Is there a formula for this that I can’t find? Or is this simply impossible?
When you say diagonal, do you mean diagonal in proportion to the object itself or diagonal to the world? (think 45 degrees from the baseplate or x degrees from the object)
You can use the Pythagoras theorem wich states that the sum of the squares of the legs is equal to the square of the hypotenuse: h2 = a2 + b2. Clearing the equation gives you: h = √(a2 + b2). then you have the length of the diagonal as h, and its position is the same of the part.
Use the law of cosines to calculate the angle you are looking for. c2 = a2 + b2 − 2ab cos{c}. Do it twice then find the difference between the sum of both angles and 180 and youll find your last angle.
A simpler way is just doing: cos(angle_adjacent_leg / hypotenuse) and converting to degree, or using an online calculator such as Triangle Calculator (Edit: use an online calculator if you can’t calculate yourself), else you could set the starting and ending vertex position of the diagonal to those of the rectangle
This is very simple if you think in terms of vectors and use a sneaky CFrame constructor.
You can get the vector representing corner1 to corner2 by doing corner2 - corner1. This is just taking the difference of the two positions.
Using that vector, you can find the midpoint of the plane the two corners define by offsetting corner1 by half of that vector’s distance: corner1 + vector/2.
Once you have this midpoint, you can use CFrame.new(position, lookat) to create a CFrame for the line part. Simply give it the midpoint for its position, and one of the corners for its lookat. Make the line part as long as the vector’s magnitude and you’re done.
This works for any two points in 3D space.
local part = workspace.Part
------------------------------------------
local function diagonal(pos1, pos2)
local vec = pos2 - pos1 -- Get vector about origin from pos1 to pos2
local mid = pos1 + vec/2 -- Using vector, get point half of its distance away from pos1 (towards pos2)
local line = Instance.new("Part")
line.Size = Vector3.new(0.1,0.1,vec.magnitude) -- Part is as long as the distance between pos1 and pos2
line.CFrame = CFrame.new(mid, pos1) -- From midpoint, look at pos1
line.Anchored = true
line.Color = Color3.new(1,1,0)
line.Parent = workspace
return line
end
------------------------------------------
local corner1 = part.CFrame * (part.Size/2)
local corner2 = part.CFrame * Vector3.new(-part.Size.x/2, part.Size.y/2, -part.Size.z/2) -- Adjust y to be on top of part along with corner1
diagonal(corner1, corner2)
You can edit this first line of this script and paste it into the command bar to generate a line from corner to corner on the top face of a part. You could also just feed the diagonal() function two Vector3s you define yourself to draw a line anywhere.