Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Point inside of rectangle


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 h3ro   Members   -  Reputation: 161

Like
0Likes
Like

Posted 20 February 2008 - 01:06 PM

Hallo, How can I check if a point is inside a rectangle? The rectangle has been rotated, so I cant simply compare the point to the vertexes (I think). Thanks in advance

Sponsor:

#2 scgames   Members   -  Reputation: 2023

Like
0Likes
Like

Posted 20 February 2008 - 01:18 PM

Quote:
Original post by h3ro
Hallo,

How can I check if a point is inside a rectangle? The rectangle has been rotated, so I cant simply compare the point to the vertexes (I think).

Thanks in advance
Short answer: transform the point into the local space of the rectangle, and then perform a point-in-axis-aligned-rectangle test (which is straightforward).

#3 h3ro   Members   -  Reputation: 161

Like
0Likes
Like

Posted 20 February 2008 - 02:45 PM

Sorry for being dump here, but could you please explain a bit more? Math is not really my strongest side. Tried google, but couldnt find anything for "point-in-axis-aligned-rectangle test"


#4 Hodgman   Moderators   -  Reputation: 38486

Like
0Likes
Like

Posted 20 February 2008 - 03:03 PM

Another method:
If you know where the corners of the rectangle are, you can deduce the equation for the four line-segments that make up the edges, which you can then use to determine the normals of each of the edges.

Get the dot-product of the edge-normal and the vector formed from any point on the line to your test-point, this will tell you which side of the line the point is on.
If the point is on the 'inside' of all of the lines, it's inside the box.



Quote:
Original post by h3ro
Sorry for being dump here, but could you please explain a bit more? Math is not really my strongest side. Tried google, but couldnt find anything for "point-in-axis-aligned-rectangle test"

A "point-in-axis-aligned-rectangle test" is just:
bool Inisde( x, y, l, r, b, t )//x,y are the point, l,r,b,t are the extents of the rectangle
{
return x > l && x < r && y > b && y < t;
}


#5 Álvaro   Crossbones+   -  Reputation: 15566

Like
0Likes
Like

Posted 20 February 2008 - 03:05 PM

Another answer:
* P is the point.
* C is a corner of the rectangle.
* v1 and v2 are the two vectors that define the sides (with C as origin).
* v = P-C

P is in the rectangle if and only if
0<=dot_product(v,v1)<=dot_product(v1,v1) and 0<=dot_product(v,v2)<=dot_product(v2,v2)



#6 h3ro   Members   -  Reputation: 161

Like
0Likes
Like

Posted 20 February 2008 - 03:38 PM

Thank you to all of you. I have it working now :)

#7 Álvaro   Crossbones+   -  Reputation: 15566

Like
0Likes
Like

Posted 21 February 2008 - 07:27 AM

Quote:
Original post by h3ro
Thank you to all of you. I have it working now :)

What suggestion did you end up using?

#8 h3ro   Members   -  Reputation: 161

Like
0Likes
Like

Posted 21 February 2008 - 08:36 AM

I used yours. It was the simplest and the I understood it :P

Thanks

#9 scgames   Members   -  Reputation: 2023

Like
0Likes
Like

Posted 21 February 2008 - 08:47 AM

Quote:
Original post by h3ro
I used yours. It was the simplest and the I understood it :P
Just for the record, the solution proposed by alvaro is the same as the one I proposed in the first reply to your thread (alvaro, however, was kind enough to spell out the details for you :).

#10 Boder   Members   -  Reputation: 937

Like
0Likes
Like

Posted 21 February 2008 - 08:54 AM

Then it's all the same solution on this page!

V is converting to the local space, and uses the dot products for the point-in-rectangle test?

But the dot products are not really necessary if it's AABB, you could use the Hodgman method.

#11 h3ro   Members   -  Reputation: 161

Like
0Likes
Like

Posted 23 February 2008 - 05:47 AM

For some reason I cant get I to work anymore. Right now it is only drawing half the bounding box. Any ideas on why?

EDIT:
Fooling around for ever fixed it. Not really sure what it was though, but working now :)

[Edited by - h3ro on February 23, 2008 4:47:38 PM]




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS