Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- File: AA_Rectangle.cpp
- Author: *REDACTED*
- Created: *REDACTED*
- Desc: File containing the definitions for the AA_Rectangle class
- */
- #include "AA_Rectangle.h"
- AA_Rectangle::AA_Rectangle()
- {
- mHeight = 1;
- mWidth = 1;
- mBottomLeft = new Point2D(0.0f, 0.0f);
- mTopRight = new Point2D(mBottomLeft->x + mWidth, mBottomLeft->y + mHeight);
- }
- AA_Rectangle::AA_Rectangle(Point2D * origin, float width = 1.0f, float height = 1.0f)
- {
- mHeight = height;
- mWidth = width;
- (origin == nullptr) ? mBottomLeft = new Point2D() : mBottomLeft = new Point2D(*origin);
- mTopRight = new Point2D(mBottomLeft->x + width, mBottomLeft->y + height);
- }
- AA_Rectangle::AA_Rectangle(float xOrigin, float yOrigin, float width, float height)
- {
- mHeight = height;
- mWidth = width;
- mBottomLeft = new Point2D(xOrigin, yOrigin);
- mTopRight = new Point2D(xOrigin + width, yOrigin + height);
- }
- AA_Rectangle::AA_Rectangle(const AA_Rectangle & other)
- {
- mHeight = other.mHeight;
- mWidth = other.mWidth;
- }
- void AA_Rectangle::operator=(const AA_Rectangle & other)
- {
- mHeight = other.mHeight;
- mWidth = other.mWidth;
- mBottomLeft = other.mBottomLeft;
- mTopRight = other.mTopRight;
- }
- AA_Rectangle::~AA_Rectangle()
- {
- delete mBottomLeft;
- delete mTopRight;
- }
- // Testing if a point is in the rectangle is simple for Axis Aligned rectangles.
- // Just need to compare the rectangles bottomLeft and topRight points with the reference point
- bool AA_Rectangle::isPointInRectangle(Point2D * refPoint)
- {
- // Early out if the refPoint is null
- if (refPoint == nullptr)
- return false;
- // compare the refPoint's (x,y) with the (x,y) of the bottomLeft and topRight points
- if (refPoint->x > mBottomLeft->x && refPoint->x < mTopRight->x &&
- refPoint->y > mBottomLeft->y && refPoint->y < mTopRight->y)
- {
- return true;
- }
- return false;
- }
- // Since we are dealing with Axis Aligned Rectangles, we do not need to worry about geometry calculations
- // or matrix transforms to ascertain intersections/overlap
- // We can just compare the x,y coordinates of the bottomLeft and topRight points of both rectangles
- // in each axis to find a possible intersection or overlap
- bool AA_Rectangle::isRectangleIntersecting(AA_Rectangle * refRectangle)
- {
- // Early out if the refRectangle is null
- if (refRectangle == nullptr)
- return false;
- // Deal with the Y axis tests
- if (mTopRight->y < refRectangle->mBottomLeft->y ||
- mBottomLeft->y > refRectangle->mTopRight->y)
- {
- return false;
- }
- // Deal with the X axis tests
- if (mTopRight->x < refRectangle->mBottomLeft->x ||
- mBottomLeft->x > refRectangle->mTopRight->x)
- {
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement