Jump to content

Is rectangle within rectangle


klaw

Recommended Posts

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.

Link to comment
-- 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
Link to comment
  • Moderators
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
Link to comment
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, 

Link to comment
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.

Link to comment
  • Moderators

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
Link to comment

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