Finding the shortest distance

I have the script below to find the shortest distance between a frame that is currently being dragged and all these points (those lil white dots u see).

you cant see the dragging frame but that doesnt really matter in the first place. comments in the script explain whats going on

the white dots are the visuals of the points:

RobloxScreenShot20250205_132908040 (2)

local function GetClosestGapPoint(parentFrame, draggedFrame)
	-- Get all the gap positions (X coordinates)
	local gapPoints = GetAllFramePositions(parentFrame)

	-- Table to store the distances from the dragged frame to each gap
	local distances = {}

	-- Calculate the center of the dragged frame
	local draggedPosX = draggedFrame.AbsolutePosition.X + draggedFrame.AbsoluteSize.X / 2
	local draggedPosY = draggedFrame.AbsolutePosition.Y + draggedFrame.AbsoluteSize.Y / 2

	-- Loop through all the gap points and calculate the distance
	for index, gapX in pairs(gapPoints) do
		-- Calculate the 2D distance (Euclidean distance) from the dragged frame to each gap point
		local APlusB = math.pow(gapX - draggedPosX, 2) + math.pow((parentFrame.AbsolutePosition.Y + parentFrame.AbsoluteSize.Y / 2) - draggedPosY, 2)
		-- Store the calculated distance
		distances[index] = math.sqrt(APlusB)
	end

	-- Initialize variables to track the closest gap
	local minIndex = nil
	local minValue = math.huge  -- Start with a very large number for comparison

	-- Find the gap with the smallest distance
	for index, value in pairs(distances) do  -- Use pairs instead of ipairs
		if value < minValue then
			minValue = value   -- Update minimum distance
			minIndex = index   -- Store the index of the closest gap
		end
	end


	-- Debugging: Print out the index and value of the closest gap
	--print(distances, gapPoints)
	
	print(minValue, minIndex)

	-- Return the entire list of gap points and the closest gap point
	return gapPoints, gapPoints[minIndex]
end


The issue you is that it does not return any sensible values. as in it returns all points one after each other. which i cant wrap my head around. I asked chatgpt and there is only so much it can do. so any help would be appreciated

1 Like

You’re returning a tuple that consists of a table of all the points, plus a second return value that is one of the nearest points. Are you handling both return values, i.e.:
local allPoints, nearestPoint = GetClosestGapPoint()?

If you’re not concerned with returning all of the nearest points in the case of some being tied for the same minimum distance, that whole last loop could just be replaced with return gapPoints[minIndex], right?

1 Like

At the moment I’m printing them out so yeah

How did I not see that thanks

after some more debugging the out put just bounces between values. no matter where the frame is.

Are all of these gap points in a horizontal line? It’s unclear to me why the distance check is 2D at all, rather than a 1D x-only: math.abs(gapX - draggedPosX) You’d only need the Y component if the gaps have different Y positions, like if they are a 2D grid instead of a line. If it’s a matter of knowing if something is in range, like for drag and drop, that could be a separate check.

1 Like

That is exactly what it is. Sorry if I wasn’t clear

print(minValue, minIndex)

output:

  20:06:51.318  52.85412063599126 1  -  Client - Hotbar:113
  20:06:51.318  726.5645331101994 1  -  Client - Hotbar:113
  20:06:51.318  885.8808215406658 1  -  Client - Hotbar:113
  20:06:51.318  52.85412063599138 3  -  Client - Hotbar:113
  20:06:51.318  81.49071441612199 2  -  Client - Hotbar:113 -- right
  20:06:51.318  666.4251291619676 1  -  Client - Hotbar:113
  20:06:51.341  52.85412063599126 1  -  Client - Hotbar:113
  20:06:51.341  726.5645331101994 1  -  Client - Hotbar:113
  20:06:51.341  885.8808215406658 1  -  Client - Hotbar:113
  20:06:51.341  52.85412063599138 3  -  Client - Hotbar:113
  20:06:51.341  81.49071441612199 2  -  Client - Hotbar:113 -- right
  20:06:51.342  666.4251291619676 1  -  Client - Hotbar:113

the values marked as right are the only times the outputed value makes coherent sense. the rest dont even change when changing the drag position.

1 Like

bumping cause i still have no idea how to combat this,

only every 5 times is the output correct