#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #include "stdafx.h" #include "object.h" #include "boundedobject.h" #include "math.h" // Note: min and max functions are used to calculate the // minimum and maximum of two numbers, respectively. void BoundedObject::calculateBounds() { // We need to run down the draw list and determine the // minx, miny, maxx, and maxy coordinates. Then the // (minx,miny) and (maxx, maxy) coordinates will define // the bounding box for us. DrawPrimitiveList::Position pos; int minx, miny, maxx, maxy; // Always make sure to check if the drawlist is empty or not. drawList.reset(pos); if (!drawList.isEmpty()) { // Get some initial value for the minx, miny, maxx, maxy by // setting them equal to the first element in the draw list. findMinMax(drawList.data(pos), minx, miny, maxx, maxy); // Now we go to the next element in the list. drawList.advance(pos); // Now, let's go down the rest of the list while (!drawList.endOfList(pos)) { // Find the min/max of the primitive int bminx, bminy, bmaxx, bmaxy; findMinMax(drawList.data(pos), bminx, bminy, bmaxx, bmaxy); minx = min(minx, bminx); miny = min(miny, bminy); maxx = max(maxx, bmaxx); maxy = max(maxy, bmaxy); drawList.advance(pos); } // We now have the min/max X and Y positions of the object. // Use this to determine the bounding box. rect.setCorners(DrawPoint(minx,miny), DrawPoint(maxx,maxy)); } } bool BoundedObject::intersects(const BoundedObject& other) const { // We now have two rectangles. Now we need to compare // them to see if there exists any overlap. This is done // by checking to see if any corner in one rectangle is in // the other. // Check to see if any corner in this object is within the other DrawPoint pt1, pt2; rect.getCorners(pt1, pt2); // Now, put it into world coordinates. pt1.setX(pt1.getX()+origin.getX()); pt1.setY(pt1.getY()+origin.getY()); pt2.setX(pt2.getX()+origin.getX()); pt2.setY(pt2.getY()+origin.getY()); if (other.intersects(pt1) || other.intersects(pt2) || other.intersects(DrawPoint(pt1.getX(),pt2.getY())) || other.intersects(DrawPoint(pt2.getX(),pt1.getY()))) return true; // Get the corners and then make the points relative to the world. other.rect.getCorners(pt1, pt2); pt1.setX(pt1.getX()+other.origin.getX()); pt1.setY(pt1.getY()+other.origin.getY()); pt2.setX(pt2.getX()+other.origin.getX()); pt2.setY(pt2.getY()+other.origin.getY()); // Check to see if any corner in the other object's bounds // is within this object. if (intersects(pt1) || intersects(pt2) || intersects(DrawPoint(pt1.getX(),pt2.getY())) || intersects(DrawPoint(pt2.getX(),pt2.getY()))) return true; // Otherwise the two rectangles don't intersect. return false; } bool BoundedObject::intersects(const DrawPoint& point) const { // Simply call the superclass intersects method. return DrawableObject::intersects(point); } void BoundedObject::getBounds(DrawRectangle& r) const { r = rect; } bool BoundedObject::findMinMax(DrawPrimitive* prim, int& minx,int& miny, int& maxx,int& maxy) const { if (isA(prim, DrawEllipse)) // is the primitive an ellipse? { // Typecast prim into a DrawEllipse*. That way, we can // access the primitive's ellipse specific methods. DrawEllipse* ell = (DrawEllipse*) prim; DrawRectangle rect; // Get the bounds of the rectangle ell->getBounds(rect); DrawPoint pt1, pt2; // Find each of the individual corners of the // bounding rectangle rect.getCorners(pt1, pt2); minx = min(pt1.getX(), pt2.getX()); maxx = max(pt1.getX(), pt2.getX()); miny = min(pt1.getY(), pt2.getY()); maxy = max(pt1.getY(), pt2.getY()); return true; } else if (isA(prim, DrawLine)) { DrawLine* line = (DrawLine*) prim; DrawPoint pt1 = line->getBegin(); DrawPoint pt2 = line->getEnd(); minx = min(pt1.getX(), pt2.getX()); maxx = max(pt1.getX(), pt2.getX()); miny = min(pt1.getY(), pt2.getY()); maxy = max(pt1.getY(), pt2.getY()); return true; } else if (isA(prim, DrawPoint)) { DrawPoint* pnt = (DrawPoint*) prim; // Only one point, so the mins are the same as the maxes. minx = maxx = pnt->getX(); miny = maxy = pnt->getY(); return true; } else if (isA(prim, DrawRectangle)) { DrawRectangle* rect = (DrawRectangle*) prim; DrawPoint pt1, pt2; rect->getCorners(pt1, pt2); minx = min(pt1.getX(), pt2.getX()); maxx = max(pt1.getX(), pt2.getX()); miny = min(pt1.getY(), pt2.getY()); maxy = max(pt1.getY(), pt2.getY()); return true; } return false; }