Advertisement
Guest User

my cpp

a guest
Nov 23rd, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. // This is your implementation file
  2. #include <cmath>
  3. #include <string>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7. #include "p1.h"
  8.  
  9. Rectangle::Rectangle()
  10.     : p1{0.0,0.0}, p2{0.0,0.0}  { }
  11.  
  12. Rectangle::Rectangle(double x, double y, double w, double h)
  13.     : p1{x,y}, p2{x+w,y-h} {}
  14.  
  15. double Rectangle::getArea() const
  16. {
  17.     return getWidth() * getHeight();
  18. }
  19. double Rectangle::getWidth() const
  20. {
  21.     return abs(p1.x - p2.x);
  22. }
  23. double Rectangle::getHeight() const // Alondra
  24. {
  25.     return abs(p1.y - p2.y);
  26. }
  27. double Rectangle::getX() const
  28. {
  29.     return p1.x;
  30. }
  31. double Rectangle::getY() const
  32. {
  33.     return p1.y;
  34. }
  35.  
  36. void Rectangle::setLocation(double x, double y)
  37. {
  38.     p1.x = x;
  39.     p1.y = y;
  40. }
  41. void Rectangle::setSize(double width, double height)
  42. {
  43.     p2.x = p1.x + width;
  44.     p2.y = p1.y - height;
  45. }
  46.  
  47. Rectangle intersection(Rectangle r1, Rectangle r2)
  48. {
  49.     // FOR THE INTERSECTION FUNCTION, WE WILL ASUME THAT
  50.     // P1 IS IN THE lower left corner AND THAT
  51.     // P2 IS IN THE upper right corner:
  52.     //
  53.     //     ___________________________ p2
  54.     //     |                          |
  55.     //     |                          |
  56.     //     |                          |
  57.     //     |__________________________|
  58.     //   p1
  59.     //
  60.     // SO THAT WE CAN GO THROUGH THE NUMBER LINE WITHIN
  61.     // POSITIVE BOUNDARIES.
  62.    
  63.     Rectangle newRect;
  64.     Point initPoint = {0,0}; //based on Point
  65.    
  66.     bool foundX = false, foundY = false;
  67.    
  68.     // 1. Slide through one Rect until the other is first intercepted
  69.     // FINDING P1->X
  70.     for (int i = r1.getX(); i < r1.getX() + r1.getWidth(); i++) // i from R1,P1,X to R1,P2,X
  71.     {
  72.         if (i >= r2.getX() && i < r2.getX() + r2.getWidth()) // if i is between R2,P1,X and R2,P2,X
  73.             { initPoint.x = i; foundX = true; break; }
  74.     }
  75.    
  76.     // FINDING P1->Y
  77.     for (int i = r1.getY(); i < r1.getY() + r1.getHeight(); i++) // i from R1,P1,Y to R1,P2,Y
  78.     {
  79.         if (i >= r2.getY() && i < r2.getY() + r2.getHeight())
  80.             { initPoint.y = i; foundY = true; break; }
  81.     }
  82.    
  83.     // *now that I have my p1->x and p1->y, it's time to check if they are filled*
  84.     if ((! foundX) || (! foundY))
  85.         return newRect;
  86.    
  87.     // FINDING WIDTH AND HEIGHT
  88.     int w = 0;
  89.     for (int i = initPoint.x; i < (r1.getX() + r1.getWidth()) && i < (r2.getX() + r2.getWidth()); i++)
  90.         w++;
  91.    
  92.     int h = 0;
  93.     for (int i = initPoint.y; i < (r1.getY() + r1.getHeight()) && i < (r2.getY() + r2.getHeight()); i++)
  94.         h++;
  95.    
  96.     // WRAPPING UP
  97.     newRect.setLocation(initPoint.x, initPoint.y);
  98.     newRect.setSize(w, h);
  99.    
  100.     return newRect;
  101.    
  102.     // 2. When the point is found (probably at the beggining),
  103.     //    Slide horizontally while they intercept
  104.     // 3. After we find the "horizontal union,"
  105.     //    start from that first intercept, until numbers mismatch
  106.     //    (outside any of the rectangles)
  107.     //    [Keep the count on 2. and 3. for width and height]
  108.     //    [First intercept will be P1 of the new rectangle]
  109.    
  110.    
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement