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:
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
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?
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.