Skip to main content
gamedev.net
🔒 Locked

2d bounding rectangle

Started by Dennis Sep 25, 2000 at 6:03 AM 7 replies 1.3k views
Original Post
Dennis
Dennis
Ok, I really need to know how to get the 2d bounding rectangle (is that a real word?) of a polygon. Is there somebody who knows what I mean?
fifty1
fifty1
Need more information.

If the polygon is 2D, just check all the points and pick the min/max x and y coordinates as the bounding edge for the rectangle. If the polygon is 3D, first project it onto a 2D plane and then do the same.

neocron
neocron
Here's some code to gen min/max points on a 2D polyon. If you want a bounding cube just add the z.

    
// Returns the integer bounding box for polygon

void GetBoundingBox( POINT3D *rgPoints, int nPoints, RECT2D rc )
{
// Get the maximum/minimum y

rc.left = CSDScreen::GetHeight();
rc.right = 0;
rc.top = CSDScreen::GetWidth();
rc.bottom = 0;

int ix, x, y;
for( ix=0; ix<nPoints; ix++ )
{
x = DoubleToLong( rgPoints[ix].x );
rc.left = min( rc.left, x );
rc.right = max( rc.right, x );
y = DoubleToLong( rgPoints[ix].y );
rc.top = min( rc.top, y );
rc.bottom = max( rc.bottom, y );
}
}




Edited by - neocron on September 25, 2000 6:14:25 PM
Dennis
Dennis
It''s a 3d polygon, but how do I project it on a 2d plane?
neocron
neocron
Here''s some code: fOffX and fOffY are ususlly half-screen-width and half-screen-height respectively. fproj is the Projection Ratio = (dist from view plane)/(view plane width).

    
void Project( POINT3D *pVertex, POINT3D *pResult, unsigned long ulNumVertices, FLOAT fproj, FLOAT fOffX, FLOAT fOffY )
{
FLOAT k, k0, k1;
POINT3D *pVertexIn = &pVertex[0];
POINT3D *pVertexOut = &pResult[0];

unsigned long i, ulHalfVerts = (ulNumVertices>>1);

for( i=0; i<ulHalfVerts; ++i )
{
k = fproj/(pVertexIn->z * (pVertexIn+1)->z);
k0 = (pVertexIn+1)->z*k;
k1 = pVertexIn->z*k;

pVertexOut->x = fOffX + k0*pVertexIn->x;
pVertexOut->y = fOffY + k0*pVertexIn->y;
pVertexOut->z = pVertexIn->z;

++pVertexIn;
++pVertexOut;

pVertexOut->x = fOffX + k1*pVertexIn->x;
pVertexOut->y = fOffY + k1*pVertexIn->y;
pVertexOut->z = pVertexIn->z;

++pVertexIn;
++pVertexOut;
}

if ( ulNumVertices&0x1 )
{
// Do the last transition

k = fproj/pVertexIn->z;

pVertexOut->x = fOffX + k*pVertexIn->x;
pVertexOut->y = fOffY + k*pVertexIn->y;
pVertexOut->z = pVertexIn->z;
}
}
//

// Use with your 2D bounding box function:

//

POINT3D g_rgTmpPoints[ MAX_POINTS ];
void Calc_2DBoundingFrom3D( POINT3D *rgPoints, int nPoints, FLOAT fProj, FLOAT fOffX, FLOAT fOffY, RECT2D& rc )
{
ASSERT( nPoints < MAX_POINTS );

// Convert from 3D to 2D

Project( rgPoints, g_rgTmpPoints, nPoints, fProj, fOffX, fOffY );
// Get 2D bounding box

GetBoundingBox( g_rgTmpPoints, nPoints, rc );
}


...or something like this. Hope it helps!
fifty1
fifty1
Projection is a little different than just setting z=0, except for a special case that I''ll mention later.

You know how when you look down a long street, the distant parts are narrower than the nearby parts? Imagine if your view of the street was on a monitor (a 2D plane). The width of the street (x-direction) is constant in 3D, but gets smaller in 2D due to perspective. So when you project into 2D, you have to consider this ''perspective division''.

The perspective effect can be large or small depending on your field of view. A wide field of view will give a large perspective effect (like a wide-angle lens on a camera). A narrow field of view is like a telescope, and gives a small perspective effect.

The special case I mentioned above is an extremely narrow field of view where the perspective effect becomes zero. This is called on orthographic projection. (Don''t try to set it up using a zero fov, it won''t work. There are special functions in Direct3D and OpenGL for setting up orthographic projections).

BTW, you can use gluProject() and gluUnProject() in OpenGL to transform coordinates between monitor (2D) and world (3D) coordinates. There may be similar functions in Direct3DX or the Direct3D utility library.

Hope this helps.
fifty1
fifty1
I forgot something.

Even in an orthographic projection, setting z=0 will only project from 3D to 2D under a narrow set of circumstances:

1. Camera is looking directly down z-axis
2. Orthographic extents of your viewport match screen coordinates (if your window is 800x600 pixels your world space must be 800 units wide and 600 units high)
3. The camera is not rotated off the ''usual'' position (up vector is +y)

These circumstances are so restrictive that you may as well be in 2D rather than 3D.

The moral is: Don''t try to project from 3D to 2D using z=0.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.