Hi!
Note: I’m not able to use already existing Roblox functions for this as everything relating to editing the drawing is happening on a ‘virtual’ UV grid.
I’m currently making a drawing game and I decided to add an eraser to it. Because the drawing may have a lot of parts, I wouldn’t be able to simply check where each parts of the drawing overlaps with the erased area so I decided to firstly do a simple rotated bound check so reduce significantly the number of complex checks needed.
So I’ve got this code that checks for overlaps between two rectangles but it seems that it could be optimized further as originally, it was made to handle pretty much any polygones with 4 points and not just only rotated rectangles.
function checkRectangleOverlap(rect1, rect2)
-- Get the axes of the rectangles
local rect1Axes = {
{rect1[2][1] - rect1[1][1], rect1[2][2] - rect1[1][2]},
{rect1[3][1] - rect1[2][1], rect1[3][2] - rect1[2][2]},
}
local rect2Axes = {
{rect2[2][1] - rect2[1][1], rect2[2][2] - rect2[1][2]},
{rect2[3][1] - rect2[2][1], rect2[3][2] - rect2[2][2]},
}
--print(rect1Axes, rect2Axes)
-- Check for overlap along each axis
for i = 1, 2 do
-- Get the projection of each rectangle onto the current axis
local rect1Min, rect1Max = math.huge, -math.huge--
local rect2Min, rect2Max = math.huge, -math.huge--
for j = 1, 4 do--
local dot = rect1[j][1] * rect1Axes[i][1] + rect1[j][2] * rect1Axes[i][2]
--print(dot)
rect1Min = math.min(rect1Min, dot)
rect1Max = math.max(rect1Max, dot)
end--
for j = 1, 4 do--
local dot = rect2[j][1] * rect1Axes[i][1] + rect2[j][2] * rect1Axes[i][2]
rect2Min = math.min(rect2Min, dot)
rect2Max = math.max(rect2Max, dot)
end--
-- Check if the projections overlap
if rect1Max < rect2Min or rect2Max < rect1Min then
return false
end
end
for i = 1, 2 do
-- Get the projection of each rectangle onto the current axis
local rect1Min, rect1Max = math.huge, -math.huge--
local rect2Min, rect2Max = math.huge, -math.huge--
for j = 1, 4 do--
local dot = rect1[j][1] * rect2Axes[i][1] + rect1[j][2] * rect2Axes[i][2]
rect1Min = math.min(rect1Min, dot)
rect1Max = math.max(rect1Max, dot)
end--
for j = 1, 4 do--
local dot = rect2[j][1] * rect2Axes[i][1] + rect2[j][2] * rect2Axes[i][2]
rect2Min = math.min(rect2Min, dot)
rect2Max = math.max(rect2Max, dot)
end--
-- Check if the projections overlap
if rect1Max < rect2Min or rect2Max < rect1Min then
return false
end
end
-- If all checks passed, the rectangles overlap
return true
end
This code isn’t too slow but at really high number of parts it will cause lag. Further optimizations will also be implemented to reduce the number of bound checks but I still want to be safe by having this part as optimized as possible (50K checks take ~0.1s)
Thanks!