Hello Robloxians! This is my first post on the devforum.
I need help fixing some issues with my straight line drawing tool as demonstrated in the video. The purpose of the tool is to allow the drawer to draw perfectly straight lines whether they are vertical, horizontal, or diagonal.
The way the tool works is it generates ImageButtons when input has began on the white board. In this case, the ImageButton is the diagonal line.
The way I’m currently thinking about achieving this is determining which direction the mouse is moving in. For example, if both the X and Y directions of the mouse increase (with respect to the top left corner of the screen), a diagonal line would be drawn from its starting position to the position where the mouse movement ended (down and to the right). The same would apply to all respective scenarios. I’m thinking of this as four quadrants.
I’m currently experiencing difficulty maintaining the starting position form where the line was first drawn, some weird glitching that causes the direction of the line to change randomly, and the angle in which the line is drawn.
I have tried implementing my four quadrant method mentioned above but can’t seem to find the most optimal way to make it work.
It’s kind of difficult for me to explain exactly what I want the code to achieve as I only have two years of experience, but I’ll give it a shot. Here is the code I’m working with:
--arg4 is the X direction of the mouse. (mouse.X/board.AbsoluteSize.X)
--arg5 is the Y direction of the mouse. (mouse.Y/board.AbsoluteSize.Y)
--arg6 is the thickness of the line
--startLineDragX is the starting X position of where the line was being dragged from
--startLineDragY is the starting Y position of where the lines was being dragged from
local prevX = 0
local prevY = 0
if arg4 > prevX and arg5 == prevY then
--this would result in a perfectly straight X line to the right
prevX = arg4
lineFrame.Size = UDim2.new(-(startLineDragX-arg4),0,arg6,0)
elseif arg4 < prevX and arg5 == prevY then
--this would result in a perfectly straight X line to the left
prevX = mouse.X/board.AbsoluteSize.X
lineFrame.Size = UDim2.new((arg4-startLineDragX),0,arg6,0)
elseif arg4 > prevX and arg5 > prevY then
prevX = arg4
prevY = arg5
lineFrame.Rotation = math.deg(arg5/arg4)
lineFrame.Size = UDim2.new(-(startLineDragX-arg4),0,arg6,0)
elseif arg4 < prevX and arg5 < prevY then
prevX = mouse.X/board.AbsoluteSize.X
prevY = mouse.Y/board.AbsoluteSize.Y
lineFrame.Rotation = math.deg(arg5/arg4)
lineFrame.Size = UDim2.new(-(startLineDragX-arg4),0,arg6,0)
elseif arg4 > prevX and arg5 < prevY then
prevX = arg4
prevY = mouse.Y/board.AbsoluteSize.Y
lineFrame.Rotation = math.deg(arg5/arg4)
lineFrame.Size = UDim2.new(-(startLineDragX-arg4),0,arg6,0)
elseif arg4 < prevX and arg5 > prevY then
prevX = mouse.X/board.AbsoluteSize.X
prevY = arg5
lineFrame.Rotation = math.deg(arg5/arg4)
lineFrame.Size = UDim2.new(-(startLineDragX-arg4),0,arg6,0)
elseif arg5 > prevY and arg4 == prevX then
--this would result in a perfectly straight Y line down
prevY = arg5
lineFrame.Size = UDim2.new(arg2,0,-(startLineDragY-arg5),0)
elseif arg5 < prevY and arg4 == prevX then
--this would result in a perfectly straight Y line up
prevY = mouse.Y/board.AbsoluteSize.Y
lineFrame.Size = UDim2.new(arg2,0,(arg5-startLineDragY),0)
end