Jump to content

Is rectangle within rectangle


klaw

Recommended Posts

Posted

Does anybody know how I can check if a rectangle (x, y, width, height) is touching another rectangle (x, y, width, height) on the screen? I have two DX rendered rectangles and want to check if they're touching, so I can change the colour of them if they are.

Posted
-- if doesCollide(firstRectangleX, firstRectangleY, firstRectangleWidth, firstRectangleHeight, secondRectangleX,secondRectangleY, secondRectangleWidth, secondRectangleHeight) then ...

function doesCollide(x1, y1, w1, h1, x2, y2, w2, h2)
	local horizontal = (x1 < x2) ~= (x1 < x2 + w2) or (x1 + w1 < x2) ~= (x1 + w1 < x2 + w2) or (x1 < x2) ~= (x1 + w1 < x2) or (x1 < x2 + w2) ~= (x1 + w1 < x2 + w2)
	local vertical = (y1 < y2) ~= (y1 < y2 + h2) or (y1 + h1 < y2) ~= (y1 + h1 < y2 + h2) or (y1 < y2) ~= (y1 + h1 < y2) or (y1 < y2 + h2) ~= (y1 + h1 < y2 + h2)
	
	return (horizontal and vertical)
end

 

  • Like 1
  • Moderators
Posted (edited)
local rectangleX1, rectangleY1 = 20, -48
local rectangleX2, rectangleY2 = 60, -5
local rectangleSizeX, rectangleSizeY = 50, 50


function isRectangleInRectangle (X1, Y1, sizeX1, sizeY1, X2, Y2, sizeX2, sizeY2)
	if (X1 > X2 and Y1 > Y2 and X1 < X2 + sizeX2 and Y1 < Y2 + sizeY2) 
		or (X1 + sizeX1 < X2 + sizeX2 and Y1 + sizeY1 < Y2 + sizeY2 and X1 + sizeX1 > X2 and Y1 + sizeY1 > Y2) then
    	return true
    end
	return false
end

iprint(tostring(isRectangleInRectangle(rectangleX1, rectangleY1, rectangleSizeX, rectangleSizeY, rectangleX2, rectangleY2, rectangleSizeX, rectangleSizeY)))

try this, untested.

 

It will check if the TOP+LEFT or BOTTOM+RIGHT is inside the other rectangle.

(This doesn't work when you rotate the rectangle)

Edited by IIYAMA
Posted
3 minutes ago, IIYAMA said:

local rectangleX1, rectangleY1 = 20, -48
local rectangleX2, rectangleY2 = 60, -5
local rectangleSizeX, rectangleSizeY = 50, 50


function isRectangleInRectangle (X1, Y1, sizeX1, sizeY1, X2, Y2, sizeX2, sizeY2)
	if (X1 > X2 and Y1 > Y2 and X1 < X2 + sizeX2 and Y1 < Y2 + sizeY2) 
		or (X1 + sizeX1 < X2 + sizeX2 and Y1 + sizeY1 < Y2 + sizeY2 and X1 + sizeX1 > X2 and Y1 + sizeY1 > Y2) then
    	return true
    end
	return false
end

iprint(tostring(isRectangleInRectangle(rectangleX1, rectangleY1, rectangleSizeX, rectangleSizeY, rectangleX2, rectangleY2, rectangleSizeX, rectangleSizeY)))

try this, untested.

 

It will check if the TOP+LEFT or BOTTOM+RIGHT is inside the other rectangle.

(This doesn't work when you rotate the rectangle)

can you tell me how can i rotate a rectangle ? " not talking about dxDrawImage " but dxDrawRectangle, 

Posted
58 minutes ago, IIYAMA said:

local rectangleX1, rectangleY1 = 20, -48
local rectangleX2, rectangleY2 = 60, -5
local rectangleSizeX, rectangleSizeY = 50, 50


function isRectangleInRectangle (X1, Y1, sizeX1, sizeY1, X2, Y2, sizeX2, sizeY2)
	if (X1 > X2 and Y1 > Y2 and X1 < X2 + sizeX2 and Y1 < Y2 + sizeY2) 
		or (X1 + sizeX1 < X2 + sizeX2 and Y1 + sizeY1 < Y2 + sizeY2 and X1 + sizeX1 > X2 and Y1 + sizeY1 > Y2) then
    	return true
    end
	return false
end

iprint(tostring(isRectangleInRectangle(rectangleX1, rectangleY1, rectangleSizeX, rectangleSizeY, rectangleX2, rectangleY2, rectangleSizeX, rectangleSizeY)))

try this, untested.

 

It will check if the TOP+LEFT or BOTTOM+RIGHT is inside the other rectangle.

(This doesn't work when you rotate the rectangle)

The one Jayceon posted didn't work. Also, I've tried top, left, bottom and right but it doesn't do what I want it to do. I need the sides too.

  • Moderators
Posted

sides should be included afaik, my bad for forgetting to mention that.

 

It is untested, so test it with all possible combinations you can come up with.

 

  • Moderators
Posted

nvm you are right. I think I am a bit tired, that must be the problem.

I will solve it for you tomorrow if nobody already solved it for you.

  • Moderators
Posted (edited)

I came up with an easier concept for it. This will compare the centres of the rectangles with each other, which only requires 4 conditions.

local rectangleX1, rectangleY1 = 25, 0
local rectangleX2, rectangleY2 = -25, 10
local rectangleSizeX, rectangleSizeY = 50, 50


function isRectangleInRectangle(X1, Y1, sizeX1, sizeY1, X2, Y2, sizeX2, sizeY2)
	local centreX1, centreY1 = X1 + sizeX1 / 2, Y1 + sizeY1 / 2
	local centreX2, centreY2 = X2 + sizeX2 / 2, Y2 + sizeY2 / 2
	
	local distanceX = centreX1 - centreX2
	local distanceY = centreY1 - centreY2 
	
	if distanceX < 0 then
		distanceX = -distanceX
	end
	if distanceY < 0 then
		distanceY = -distanceY
	end
	
	if distanceX <= sizeX1 / 2 + sizeX2 / 2 and distanceY <= sizeY1 / 2 + sizeY2 / 2 then
		return true
	end
	return false
end

iprint(tostring(isRectangleInRectangle(rectangleX1, rectangleY1, rectangleSizeX, rectangleSizeY, rectangleX2, rectangleY2, rectangleSizeX, rectangleSizeY)))

 

Edited by IIYAMA
Posted
On 2017-5-25 at 16:04, IIYAMA said:

You might want to reply @klaw , it is a bit rude if you do not.

Sorry, yes. I've figured it out.

Apologies to @Jayceon, your code was perfect, I just copied it incorrectly. Thank you.

  • Like 2
Posted

It seems that using all 4 corners doesn't accomplish what I need, @Jayceon, because there are some occurrences when the two rectangles are touching but none of the corners are conflicting.

For example: https://gyazo.com/981ce6b1691e68c833ba4934a3216979

As you can see in the diagram, the two rectangles are touching but none of the corners are within each other. Appreciate any further help.

 

  • Moderators
Posted

My last code post will help you out with that problem, since it isn't corner based but distance based.

 

  • Like 1
Posted
3 hours ago, IIYAMA said:

My last code post will help you out with that problem, since it isn't corner based but distance based.

 

Oh! Didn't see that, sorry! But thanks, it works perfect.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...