Advertisement
_code_feedback_

Task 1: AA_Rectangle.cpp

Apr 2nd, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. /*
  2.     File: AA_Rectangle.cpp
  3.    
  4.     Author: *REDACTED*
  5.  
  6.     Created: *REDACTED*
  7.  
  8.     Desc: File containing the definitions for the AA_Rectangle class
  9. */
  10.  
  11. #include "AA_Rectangle.h"
  12.  
  13.  
  14. AA_Rectangle::AA_Rectangle()
  15. {
  16.     mHeight = 1;
  17.     mWidth = 1;
  18.  
  19.     mBottomLeft = new Point2D(0.0f, 0.0f);
  20.     mTopRight   = new Point2D(mBottomLeft->x + mWidth, mBottomLeft->y + mHeight);
  21. }
  22.  
  23. AA_Rectangle::AA_Rectangle(Point2D * origin, float width = 1.0f, float height = 1.0f)
  24. {
  25.     mHeight = height;
  26.     mWidth = width;
  27.  
  28.     (origin == nullptr) ? mBottomLeft = new Point2D() : mBottomLeft = new Point2D(*origin);
  29.     mTopRight   = new Point2D(mBottomLeft->x + width, mBottomLeft->y + height);
  30. }
  31.  
  32. AA_Rectangle::AA_Rectangle(float xOrigin, float yOrigin, float width, float height)
  33. {
  34.     mHeight = height;
  35.     mWidth = width;
  36.  
  37.     mBottomLeft = new Point2D(xOrigin, yOrigin);
  38.     mTopRight   = new Point2D(xOrigin + width, yOrigin + height);
  39. }
  40.  
  41. AA_Rectangle::AA_Rectangle(const AA_Rectangle & other)
  42. {
  43.     mHeight = other.mHeight;
  44.     mWidth = other.mWidth;
  45. }
  46.  
  47.  
  48. void AA_Rectangle::operator=(const AA_Rectangle & other)
  49. {
  50.     mHeight = other.mHeight;
  51.     mWidth = other.mWidth;
  52.  
  53.     mBottomLeft = other.mBottomLeft;
  54.     mTopRight = other.mTopRight;
  55. }
  56.  
  57. AA_Rectangle::~AA_Rectangle()
  58. {
  59.     delete mBottomLeft;
  60.     delete mTopRight;
  61. }
  62.  
  63. // Testing if a point is in the rectangle is simple for Axis Aligned rectangles.
  64. // Just need to compare the rectangles bottomLeft and topRight points with the reference point
  65. bool AA_Rectangle::isPointInRectangle(Point2D * refPoint)
  66. {
  67.     // Early out if the refPoint is null
  68.     if (refPoint == nullptr)
  69.         return false;
  70.  
  71.     // compare the refPoint's (x,y) with the (x,y) of the bottomLeft and topRight points
  72.     if (refPoint->x > mBottomLeft->x && refPoint->x < mTopRight->x &&
  73.         refPoint->y > mBottomLeft->y && refPoint->y < mTopRight->y)
  74.     {
  75.         return true;
  76.     }
  77.  
  78.     return false;
  79. }
  80.  
  81. // Since we are dealing with Axis Aligned Rectangles, we do not need to worry about geometry calculations
  82. // or matrix transforms to ascertain intersections/overlap
  83. // We can just compare the x,y coordinates of the bottomLeft and topRight points of both rectangles
  84. // in each axis to find a possible intersection or overlap
  85. bool AA_Rectangle::isRectangleIntersecting(AA_Rectangle * refRectangle)
  86. {
  87.     // Early out if the refRectangle is null
  88.     if (refRectangle == nullptr)
  89.         return false;
  90.  
  91.     // Deal with the Y axis tests
  92.     if (mTopRight->y < refRectangle->mBottomLeft->y ||
  93.         mBottomLeft->y > refRectangle->mTopRight->y)
  94.     {
  95.         return false;
  96.     }
  97.  
  98.     // Deal with the X axis tests
  99.     if (mTopRight->x < refRectangle->mBottomLeft->x ||
  100.         mBottomLeft->x > refRectangle->mTopRight->x)
  101.     {
  102.         return false;
  103.     }
  104.  
  105.     return true;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement