6

This question is similar to my previously asked this question . But instead of returning only the number of nearest boxes, I would like to find the area of corresponding boxes.

Details: Suppose I have a set of coordinates of boxes like this-

#Rect    x1      y1          x2       y2         area

1     0.0000   0.0000      0.8147   0.1355      0.1104
2     0.8147   0.0000      1.0000   0.1355      0.0251
3     0.8147   0.1355      0.9058   0.8350      0.0637
4     0.0000   0.1355      0.1270   0.9689      0.1058
5     0.9058   0.1355      0.9134   0.2210      0.0006
6     0.9058   0.8350      1.0000   1.0000      0.0155
7     0.8147   0.8350      0.9058   1.0000      0.0150
8     0.1270   0.1355      0.6324   0.3082      0.0873
9     0.1270   0.9689      0.8147   1.0000      0.0214
10    0.0000   0.9689      0.1270   1.0000      0.0040
11    0.9134   0.1355      1.0000   0.2210      0.0074
12    0.9134   0.2210      1.0000   0.8350      0.0532
13    0.9058   0.2210      0.9134   0.8350      0.0047
14    0.6324   0.1355      0.8147   0.3082      0.0315
15    0.6324   0.3082      0.8147   0.9689      0.1205
16    0.1270   0.3082      0.6324   0.9689      0.3339

Suppose these coordinates split an unit square into sub-rectangles like this picture- enter image description here

Now this code returns the value of the number of nearest boxes, but it fails to returns the area of that boxes. Here is my code-

#include <iostream>
#include <cstdlib>
#include <vector>
#include <stdio.h>

using namespace std;

class Rect {
  public:
  double x1, x2, y1, y2, area; 

  Rect(double X1, double Y1, double X2, double Y2, double Area) {
    if (X1 < X2) {
      x1 = X1; x2 = X2;
    } else {
      x2 = X1; x1 = X2;
    }
    if (Y1 < Y2) {
      y1 = Y1; y2 = Y2;
    } else {
      y2 = Y1; y1 = Y2;
    } Area =area;

  }

  bool NearestBox(Rect rect) {

    //for x-axis
    if (x1 == rect.x2 || x2 == rect.x1) {     

      if (y1 >= rect.y1 && y1 < rect.y2) {
        return true;
      }
      if (y2 > rect.y1 && y2 <= rect.y2) {
        return true;
      }
    }              

    // for y-axis    

    if (y1 == rect.y2 || y2 == rect.y1) {
      if (x1 >= rect.x1 && x1 < rect.x2) {
        return true;
      }
      if (x2 > rect.x1 && x2 <= rect.x2) {
        return true;
      }
    }

    return false;  

  }
};

int main() {

  vector<Rect> rects;     
                //Rect(  x1 ,  y1  ,   x2  ,  y2   ,  area) 
  rects.push_back(Rect(0.0000,0.0000, 0.8147,0.1355, 0.1104));
  rects.push_back(Rect(0.8147,0.0000, 1.0000,0.1355, 0.0251));

  rects.push_back(Rect(0.8147,0.1355, 0.9058,0.8350, 0.0637));
  rects.push_back(Rect(0.0000,0.1355, 0.1270,0.9689, 0.1058 ));

  rects.push_back(Rect(0.9058,0.1355, 0.9134,0.2210, 0.0006));
  rects.push_back(Rect(0.9058,0.8350, 1.0000,1.0000, 0.0155));
  rects.push_back(Rect(0.8147,0.8350, 0.9058,1.0000, 0.0150));



  rects.push_back(Rect(0.1270,0.1355, 0.6324,0.3082, 0.0873));
  rects.push_back(Rect(0.1270,0.9689, 0.8147,1.0000, 0.0214));
  rects.push_back(Rect(0.0000,0.9689, 0.1270,1.0000, 0.0040));

  rects.push_back(Rect(0.9134,0.1355, 1.0000,0.2210, 0.0074));
  rects.push_back(Rect(0.9134,0.2210, 1.0000,0.8350, 0.0532));
  rects.push_back(Rect(0.9058,0.2210, 0.9134,0.8350, 0.0047));


  rects.push_back(Rect(0.6324,0.1355, 0.8147,0.3082, 0.0315));
  rects.push_back(Rect(0.6324,0.3082, 0.8147,0.9689, 0.1205));
  rects.push_back(Rect(0.1270,0.3082, 0.6324,0.9689, 0.3339));

  int b=13;
  int nearBox_count = 0;
  //double area=0;
  double TotalArea=0;

  for (int x = 0; x < rects.size(); ++x) {

    if (rects[b].NearestBox(rects[x])) {  
      if (x==b) {
        continue; //this is our box , so do not count it.
      }

    nearBox_count++;
    printf("box[%d] is nearest to box[%d] and has area %f \n", (b+1), (x+1), rects[x].area);

    TotalArea +=rects[x].area;

    }
  }

  printf("Total number of nearest box for box[%d] = %d, and the sum of area is= %f \n", (b+1), nearBox_count, TotalArea );

  return 0;
}

It prints the results-

box[14] is nearest to box[1] and has area 0.000000 
box[14] is nearest to box[3] and has area 0.000000 
box[14] is nearest to box[8] and has area 0.000000 
box[14] is nearest to box[15] and has area 0.000000 
Total number of nearest box for box[14] = 4, and the sum of area is= 0.000000 

So by comparing the results with the picture above, you can see that it returns the value of the nearest boxes but fails to return the value of their corresponding areas.

Can anyone help me to fix that?

Community
  • 1
  • 1
aries0152
  • 381
  • 1
  • 14
  • 3
    The reason the areas aren't displaying is because you don't assign them. The only assignment I see is `double area=0;`. – Geobits Aug 05 '13 at 16:51
  • You create a double variable called `area`, you initialize it to zero, you never, ever use it again until a line comes in which you print it out. What do you expect to see? – Daniel Daranas Aug 05 '13 at 16:52
  • @Geobits: Thanks for the reply! How can I fetch the values of area from *Rect* ?? – aries0152 Aug 05 '13 at 16:54
  • 2
    you have not assigned areas in the constructor. write this line at the end of RECT constructor after the if conditions `area = Area` – masad Aug 05 '13 at 17:09
  • @masad Edited my code and add that at the end of the constructor. Also change area to rects[x].area; But it still prints 0's. – aries0152 Aug 05 '13 at 17:20
  • 3
    You have put it wrong, its not `Area = area`. `Area` with a capital A is your input argument (therefore you cannot assign it to something). The correct line will be `area = Area`, which is totally different then what you have done. – masad Aug 05 '13 at 17:24
  • I am getting the following result with this fix: box[14] is nearest to box[1] and has area 0.110400 box[14] is nearest to box[3] and has area 0.063700 box[14] is nearest to box[8] and has area 0.087300 box[14] is nearest to box[15] and has area 0.120500 Total number of nearest box for box[14] = 4, and the sum of area is= 0.381900 – masad Aug 05 '13 at 17:26

2 Answers2

2

You're not actually using the area in rect[x]. You don't need the extra area variable, either. Just do something like:

printf("box[%d] is nearest to box[%d] and has area %f \n", (b+1), (x+1), rects[x].area);

TotalArea += rects[x].area;

Also, from masad's comment, you don't assign area in the constructor. You need to add a line:

area = Area;
Geobits
  • 22,218
  • 6
  • 59
  • 103
2

Instead of printing area you should be printing rects[x].area.

A couple of other things that may become issues:

  • You're testing equality on doubles. This isn't a good idea because rounding errors can always creep in. It's better to do checks for equality by testing whether the two numbers are within a tolerance of each other.
  • You define the bounding box of the rectangles and the area. Since you have four significant figures in each, one of these definitions already has a substantial rounding error. For some applications you'll instead want to calculate the area from the bounding box.
John
  • 15,990
  • 10
  • 70
  • 110