# Draw lines in frames!

I made function to draw line from (x1, y1) to (x2, y2):

local function drawLine(parent: Instance, x1: number, y1: number, x2: number, y2: number, color: Color3, thickness: number?): Frame
local centerX = (x1 + x2) / 2
local centerY = (y1 + y2) / 2
local deltaX = math.abs(x1 - x2) ^ 2
local deltaY = math.abs(y1 - y2) ^ 2
local distance = math.sqrt(deltaX + deltaY)
local rotation = math.deg(math.atan2(y1 - y2, x1 - x2))
local frame = Instance.new("Frame", parent)
frame.AnchorPoint = Vector2.new(.5, .5)
frame.Position = UDim2.fromOffset(centerX, centerY)
frame.Size = UDim2.fromOffset(distance, thickness or 1)
frame.Rotation = rotation
frame.BackgroundColor3 = color
frame.BorderSizePixel = 0
return frame
end

It creates a frame in parent and calculates its position, size and rotation.

9 Likes

You should make this a plug-in, and then sorry, but I donâ€™t see any use for it, i mean, canâ€™t I just add a frame instead of adding script as a child of the block copying the script to add a frame? Sorry if it seemed mean, but I didnâ€™t know how to say it in any other way, but I like the effort youâ€™ve put into it

If you just create a frame you can only draw a vertical or horizontal line. But using this you can create line from one point to another with any angle and distance.

Oh ok, now thatâ€™s useful, sorry if I did not understand, I still think you should make this a plug-in if you can, keep up the good work

1 Like

It makes math calculations to reduce amount of code when drawing lines from script. So if you want to create frame not from script but using Roblox Studio you can just make the calculations manually.

Ok, I like it
Character limit

1 Like

I have been searching for this specific code for the past two years and I am finally happy to have found it. Although I have not had the chance to test it yet, I have a positive feeling that it will work as intended. As someone who that mainly focuses on Roblox for a while, I can attest to the fact that 2D elements can often be a real pain. It can be especially challenging to find the right solution, but I am hopeful that this code will help the community overcome those difficulties.

1 Like