Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is your implementation file
- #include <cmath>
- #include <string>
- #include <sstream>
- using namespace std;
- #include "p1.h"
- Rectangle::Rectangle()
- : p1{0.0,0.0}, p2{0.0,0.0} { }
- Rectangle::Rectangle(double x, double y, double w, double h)
- : p1{x,y}, p2{x+w,y-h} {}
- double Rectangle::getArea() const
- {
- return getWidth() * getHeight();
- }
- double Rectangle::getWidth() const
- {
- return abs(p1.x - p2.x);
- }
- double Rectangle::getHeight() const // Alondra
- {
- return abs(p1.y - p2.y);
- }
- double Rectangle::getX() const
- {
- return p1.x;
- }
- double Rectangle::getY() const
- {
- return p1.y;
- }
- void Rectangle::setLocation(double x, double y)
- {
- p1.x = x;
- p1.y = y;
- }
- void Rectangle::setSize(double width, double height)
- {
- p2.x = p1.x + width;
- p2.y = p1.y - height;
- }
- Rectangle intersection(Rectangle r1, Rectangle r2)
- {
- // FOR THE INTERSECTION FUNCTION, WE WILL ASUME THAT
- // P1 IS IN THE lower left corner AND THAT
- // P2 IS IN THE upper right corner:
- //
- // ___________________________ p2
- // | |
- // | |
- // | |
- // |__________________________|
- // p1
- //
- // SO THAT WE CAN GO THROUGH THE NUMBER LINE WITHIN
- // POSITIVE BOUNDARIES.
- Rectangle newRect;
- Point initPoint = {0,0}; //based on Point
- bool foundX = false, foundY = false;
- // 1. Slide through one Rect until the other is first intercepted
- // FINDING P1->X
- for (int i = r1.getX(); i < r1.getX() + r1.getWidth(); i++) // i from R1,P1,X to R1,P2,X
- {
- if (i >= r2.getX() && i < r2.getX() + r2.getWidth()) // if i is between R2,P1,X and R2,P2,X
- { initPoint.x = i; foundX = true; break; }
- }
- // FINDING P1->Y
- for (int i = r1.getY(); i < r1.getY() + r1.getHeight(); i++) // i from R1,P1,Y to R1,P2,Y
- {
- if (i >= r2.getY() && i < r2.getY() + r2.getHeight())
- { initPoint.y = i; foundY = true; break; }
- }
- // *now that I have my p1->x and p1->y, it's time to check if they are filled*
- if ((! foundX) || (! foundY))
- return newRect;
- // FINDING WIDTH AND HEIGHT
- int w = 0;
- for (int i = initPoint.x; i < (r1.getX() + r1.getWidth()) && i < (r2.getX() + r2.getWidth()); i++)
- w++;
- int h = 0;
- for (int i = initPoint.y; i < (r1.getY() + r1.getHeight()) && i < (r2.getY() + r2.getHeight()); i++)
- h++;
- // WRAPPING UP
- newRect.setLocation(initPoint.x, initPoint.y);
- newRect.setSize(w, h);
- return newRect;
- // 2. When the point is found (probably at the beggining),
- // Slide horizontally while they intercept
- // 3. After we find the "horizontal union,"
- // start from that first intercept, until numbers mismatch
- // (outside any of the rectangles)
- // [Keep the count on 2. and 3. for width and height]
- // [First intercept will be P1 of the new rectangle]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement