- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这个问题和我之前问的相似this question .但是我不想只返回最近的盒子的数量,而是想找到相应盒子的面积。
详细信息:假设我有一组像这样的盒子坐标-
#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的值,但是没有返回它们对应区域的值。
谁能帮我解决这个问题?
最佳答案
您实际上并没有使用 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;
关于c++ - 最近盒子的面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18063443/
我正在开发一个企业名录网站,其搜索将由 Google map 驱动。用户将能够根据各种标准搜索他们所在地区的企业,但主要的想法是,如果您搜索例如“新泽西州的水管工”,您将获得新泽西州所有水管工的结果。
我得到了一条任意形状的曲线,包围了一些区域。 我想估计曲线在 iPhone/iPad 屏幕上包围的像素数。我该怎么做? 曲线被定义为点的连续 x/y 坐标。 闭合曲线。 通过用户的触摸(touches
我想删除 R 在点阵图周围的默认边距。这意味着我想摆脱红色矩形之外的所有空白。这是示例: library (raster) library(rasterVis) f <- system.file("e
无法找到任何直接的解决方案来计算 GMSPolygon 对象面积。有什么方法可以做到这一点,或者我必须用边长和一些数学计算来计算它? 最佳答案 感谢@Larme; GMSGeometryArea 就是
假设例如我想将标准正态分布的密度曲线下方的面积着色为十分。我希望最左边 10% 的区域具有与接下来的 10% 不同的阴影,依此类推。 这是问题“Shading a kernel density plo
我正在为 Extjs 开发一个混合图表组件,并且曲线太尖锐了。我找不到曲线具有半径的配置。如果你处理过类似的事情,你能提供一些方法让我的曲线变得平滑一点吗?这是我的代码: Ext.define('Ex
上下文 我有一个 3D 对象,我有它的坐标。然后我将对象旋转 n 次,当对象投影到网格上时,我想计算对象的 2D 面积(以纳米为单位)。 例如, 我在下面有一张图片描述了我的问题。我有相同的对象,但在
当我知道我需要的地 block 总数并且我希望排列是一个正方形(可能有一些空的子地 block )时,我正在尝试弄清楚如何计算子地 block 尺寸。 例如,如果我需要 22 个子图,那么我会为总共
我是一名数据科学家。主要使用Python和SQL来编写代码。我使用data studio进行可视化。所以我对JS不熟悉。我的诀窍data studio community visualizations
我有 1797 张 Mnist 图像,为此我需要提取两个特征(FilledArea、EulerNumber)。我知道如何在 Matlab 中做到这一点。我的特征矩阵在 Matlab 中具有(并且是正确
我希望能够在 Google map 上绘制形状(圆形、多边形和矩形),但我想限制可以绘制的形状的大小(面积)。因此,以圆圈为例,期望的行为是当用户开始从 map 上的某个点拖动鼠标以形成圆圈时,圆圈会
我是一名优秀的程序员,十分优秀!