6

这个问题和我之前问的这个问题类似。但是,我不想只返回最近的盒子的数量,而是想找到相应盒子的面积。

详细信息: 假设我有一组这样的框坐标-

#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

假设这些坐标将一个单位正方形分成如下图所示的子矩形 - 在此处输入图像描述

现在此代码返回最近框数的值,但无法返回该框的面积。这是我的代码-

#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;
}

它打印结果-

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 

所以通过与上图对比结果可以看出,它返回了最近的boxes的值,但是没有返回它们对应区域的值。

谁能帮我解决这个问题?

4

2 回答 2

2

您实际上并没有使用rect[x]. 您也不需要额外的area变量。只需执行以下操作:

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

TotalArea += rects[x].area;

此外,根据 masad 的评论,您没有area在构造函数中赋值。您需要添加一行:

area = Area;
于 2013-08-05T16:55:18.940 回答
2

area您应该打印而不是打印rects[x].area

其他一些可能会成为问题的事情:

  • 您正在测试doubles 上的相等性。这不是一个好主意,因为舍入错误总是会蔓延。最好通过测试两个数字是否在彼此的容差范围内来检查相等性。
  • 您定义矩形区域的边界框。由于每个都有四个有效数字,因此其中一个定义已经存在很大的舍入误差。对于某些应用程序,您可能希望根据边界框计算面积。
于 2013-08-05T17:04:51.687 回答