Check if point is inside a rotated rectangle

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
RemiM
Prole
Posts: 8
Joined: Wed Oct 31, 2012 4:39 pm

Check if point is inside a rotated rectangle

Post by RemiM »

I need a way to check if an x,y coordinate is within a rotated rectangle.
The available data I have available of the rectangle is:
-x,y of the centre of the rectangle
-angle
-width and height

Now, I think that if I'm able to rotate the x,y coords I need to check and the rectangle, then I'll be able to find it out.
But how do I rotate these points by the angle?
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Check if point is inside a rotated rectangle

Post by micha »

I have no finished code for your problem, but I hope the following can help.

If you want to calculate positions from a given angle you always need sine and cosine (math.sin(x) and math.cos(x))
Look at the vector (1,0) that is the vector pointing to the right, with length 1. If you want to rotate it by the angle a, it becomes (cos(a),sin(a)).
The vector (0,1) (pointing down) becomes (-sin(a),cos(a)).

That means a point (x,y) becomes after rotation (x*cos(a)-y(sin(a)), x*sin(a)+y*cos(a))

To find if a point is inside your rectangle, take the distance-vector from the rectangle center to this point and rotate it backward (by the angle -a). Then check if it is inside the corresponding unrotated rectangle.
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Check if point is inside a rotated rectangle

Post by Ref »

May be has something in it you can use but kind of shy after have been hammered on else were in the forum.
Might get some ideas.
Best
Attachments
IntersectingLines.love
simple object contact
(2.08 KiB) Downloaded 227 times
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Check if point is inside a rotated rectangle

Post by Roland_Yonaba »

Hi,

Using a rotation maxtrix, you can workout the new coordinates x',y' of a point x,y, rotated around point ox,oy by an angle a:

Code: Select all

function rotate(x,y,ox,oy,a)
  return (x-ox)*math.cos(a) - (y-oy)*math.sin(a) + ox,
             (x-ox)*math.sin(a) + (y-oy)*math.cos(a) + oy
end
Apply this on all the four vertices to get the rotated rectangle vertices.
Then to test the collision with the point xp,yp, I think you can rotate this point by the same angle and perform a simple bound box collision check with the rotated rectangle.
Or, leave the point xp,yp as it is, and see this.

Hope it helps!
User avatar
Codex
Party member
Posts: 106
Joined: Tue Mar 06, 2012 6:49 am

Re: Check if point is inside a rotated rectangle

Post by Codex »

You might want to check this thread,

viewtopic.php?f=4&t=8841

Very similar stuff.
RemiM
Prole
Posts: 8
Joined: Wed Oct 31, 2012 4:39 pm

Re: Check if point is inside a rotated rectangle

Post by RemiM »

Thanks for all the help, I'll take a further look at it tonight and try to figure it out
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Check if point is inside a rotated rectangle

Post by Ref »

This is what I've used.
The test point was determined by the mouse position.
Just a leg up.
Best

Code: Select all

--   /////////////////////////////////////////////////
--  ///// point_on_square - sum-of-areas methode ////
-- /////////////////////////////////////////////////
-- note: slower than Harden Collider
function point_on_square(tab)	-- uses sum-of-areas approach
	local test1 = point_on_triangle(tab[1],tab[2],tab[3])
	local test2 = point_on_triangle(tab[3],tab[4],tab[1])
	return test1 or test2
	end

function point_on_triangle(A,B,C)	-- if point not on triangle, area of sub-triangles > triangle area
	local P = { mouseX, mouseY }
	local area0=area( { A, B, C } )
	local area1=area( { P, B, C } )
	local area2=area( { A, P, C } )
	local area3=area( { A, B, P } )
	local sum = area1 + area2 + area3
	local eps = 0.0001
	return area0 > sum - eps and area0 <area0 + eps
	end

function area( tri )		-- returns triangle area
	local x1, y1 = tri[1][1], tri[1][2]
	local x2, y2 = tri[2][1], tri[2][2]
	local x3, y3 = tri[3][1], tri[3][2]
	return math.abs( ( x1*y2 + x2*y3 + x3*y1 - x1*y3 - x3*y2 - x2*y1) / 2 )
	end

--   //////////////////////////////////////////////////
--  //// point-in-square - Hardon Collider method ////
-- //////////////////////////////////////////////////

function point_in_square(tab)	-- uses sum-of-areas approach
	local test1 = point_in_triangle(tab[1],tab[2],tab[3])
	local test2 = point_in_triangle(tab[3],tab[4],tab[1])
	return test1 or test2
	end

function point_in_triangle( a,b,c)	-- Harden Collider approach
	local p = {mouseX,mouseY}
	return onSameSide(p,a, b,c) and onSameSide(p,b, a,c) and onSameSide(p,c, a,b)
	end

function onSameSide(a,b, c,d)
	local px, py = d[1]-c[1], d[2]-c[2]
	local l = det(px,py,  a[1]-c[1], a[2]-c[2])
	local m = det(px,py,  b[1]-c[1], b[2]-c[2])
	return l*m >= 0 end

function det(x1,y1, x2,y2)	return x1*y2 - y1*x2 end

--   //////////////////////////////
--  //// end point-in-square /////
-- //////////////////////////////
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 24 guests