# How to get bounding box?

This topic is 4714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there a way besides having variables to keep track of where an object is, and how wide and high it is? I want to do this with basic shapes.

##### Share on other sites
Do you use a DirectX mesh? People need more detail to help.

##### Share on other sites
BASIC SHAPES, a rectangle, circle, triangle

##### Share on other sites
I use managed code, but look in the methods of ID3DXMesh for ComputeBoundingBox (maybe?), or you could do it manually with other shapes by simply finding the minimum and maximum extents of the vertices.
Hope that helps

##### Share on other sites
Ah - you're here, I assume 2D since you said rectangle as opposed to cuboid, so you would be getting the bounding rectangle. Still, it is all similar in 3D:

If your rectangle is axis-aligned then it is the bounding rectangle, else find the minimum and maximum x and y of its corners and that's your bounding rect. Do the same for a triangle.

Bounding rect of circle has the height and width of the circle's diameter.

##### Share on other sites
Thanks for the help. How would I find the min and max. Is there a function or would I have to find out where the vertices are. Sorry for all the questions but I am confused.

##### Share on other sites
What if the vertices change and it makes a longer rectangle. How could I find the positions of the vertices.

##### Share on other sites
Firstly tell me how you store your objects, then I'll be able to explain more clearly. Paste your class definitions maybe or describe the variables you hold them in.

##### Share on other sites
Well im tryin to plan a class, the base class of Object for rendering. And each subclass. For example the rectangle would be a quad so I would need 4 verts. Then I pass into the constructor four structures of my vertex structure
struct Vertex{ float x,y,z; D3DCOLOR color; //OR float u,v;};

Then i would create the vertex buffer from those four verts.

1---------2
|---------|
|---------|
|---------|
|---------|
4---------3

I could see getting the x and y pos from the first vertex, oh and by the way im using an orthographic view. and the width would be 1st + 2nd, and the height would be 1st + 4th.

##### Share on other sites
So it is 3D, ok. Some psuedo code 4u:

float minX, minY, minZ, maxX, maxY, maxZ
foreach vert in vertices
if (vert.X < minX) minX = vert.X
if (vert.X > maxX) maxX = vert.X
same for y and z
next vert

Hope that helps now

##### Share on other sites
Thanks, I think i get it now, It will go though each vert and get the min and max based on all the other verts.