Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Point in OBB (Oriented Bounding Box) algorithm?

Given a center point, width, height and angle forming an OBB, how can I find if a given point P is inside the OBB?

Thanks

like image 949
jmasterx Avatar asked Sep 07 '11 03:09

jmasterx


People also ask

How is bounding box calculated?

The area of the box is the width times height. Here, 29 times 50, or 1450. The perimeter of the box is twice the width plus height. Here, that is 2(29+50), or 158.

What is the difference between AABB and OBB?

AABB - is Axis-Aligned Bounding Box. OBB - Oriented Bounding Box. Those are well established computer science terms. The difference is pretty simple - AABB is not rotated, OBB can be rotated.

What are bounding box extents?

In computational geometry, the minimum bounding rectangle (MBR), also known as bounding box (BBOX) or envelope, is an expression of the maximum extents of a two-dimensional object (e.g. point, line, polygon) or set of objects within its x-y coordinate system; in other words min(x), max(x), min(y), max(y).


1 Answers

I take it that the wrinkle in your problem is that the bounding box can be rotated? If so, the easiest solution to me seems to be to do all calculations in the rotated coordinate plane, centered on the center of the bounding box.

To calculate the coordinates of the point relative to these axes:

newy = sin(angle) * (oldy - centery) + cos(angle) * (oldx - centerx);
newx = cos(angle) * (oldx - centerx) - sin(angle) * (oldy - centery);

(you may need to adjust this depending on how angle is supposed to be measured, I'll leave that to you, since you didn't specify)

Then hit test, the normal way:

return (newy > centery - height / 2) && (newy < centery + height / 2) 
    && (newx > centerx - width / 2) && (newx < centerx + width / 2);
like image 176
acjay Avatar answered Oct 08 '22 08:10

acjay