Graph not creating proper lines

I have created a script that tries to create a graph between predetermined points on a GUI as a basis for a more advanced/modular graph.

The issue is that the rotation is off and doesn’t line up with the center of the points. The anchor points of the GUI objects is 0.5,0.5 so its centered. Here is some images and the code.

Example #1: https://gyazo.com/8f2c764c2e2a05d1f0b185980cc9f605
Example #2: https://gyazo.com/2d138e6362ff12fdaf11ce10b434ac89

Code:

local graph = script.Parent

for i,v in pairs(graph:GetChildren()) do
   if v.ClassName == "ImageLabel" then
   	local pointnum = tonumber(string.sub(v.Name,-1,-1)) -- get number of point "Point1"
   	if pointnum ~= #graph:GetChildren() - 1 then -- subtract 1 to get rid of the script
   		local line = Instance.new("Frame",graph)
   		line.Name = "Line"..pointnum.."-"..pointnum+1
   		local point1 = graph["Point"..pointnum]
   		local point2 = graph["Point"..pointnum+1]
   		local xyratio = graph.Size.X.Scale/graph.Size.Y.Scale -- found from Reddit post below
   		local x1 = point1.Position.X.Scale
   		local y1 = point1.Position.Y.Scale
   		local x2 = point2.Position.X.Scale
   		local y2 = point2.Position.Y.Scale
   		local rotation = math.atan2((y2-y1)/xyratio,(x2-x1))
   		local distance = math.sqrt(((x2-x1)^2)+(((y2-y1)/xyratio)^2))
   		local midpoint = Vector2.new(((x1+x2)-distance)/2,(y1+y2)/2)
   		
   		line.Position = UDim2.new(midpoint.X,0,midpoint.Y,0)
   		line.Size = UDim2.new(distance,0,0.05,0)
   		line.Rotation = math.deg(rotation/xyratio)
   		line.BackgroundColor3 = Color3.fromRGB(0,170,0)
   		line.BorderSizePixel = 0
   	end
   end
end

I had trouble with it before and found a Reddit post that I used in combination with my previous attempts. I tried messing around with the equations to no avail. Any help if possible would be appreciated.

1 Like

Looks like your lines do not have 0.5 as the vertical anchor point. I’m guessing you have 0,0 but want 0,0.5.

2 Likes

That seemed to do the fix the issue, thank you for your help!