- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一组 (X,Y) 坐标,可以将单位正方形分割成子矩形。假设我的坐标是 -
( x1, y1) ( x2, y2)
(0.0000,0.0000) (0.3412,0.4175)
(0.7445,0.0000) (1.0000,0.6553)
(0.7445,0.6553) (1.0000,1.0000)
(0.0000,0.6553) (0.7445,1.0000)
(0.3412,0.0000) (0.7445,0.4175)
(0.3412,0.4175) (0.7445,0.6553)
(0.0000,0.4175) (0.3412,0.6553)....etc (total 10,000 coordinates)
举个例子,我只取了 16 组数据,这些坐标像这样分割我的正方形-
相似框的定义
那些具有相似邻居数的框被认为是相似框。对于上面的图像,box[8]、box[13] 等有 4 最近邻。所以它们被认为是相似的盒子。
下图应该很清楚-
::我的问题::
从图中我们可以看出-
对于 box[8] 最近的框是:
box(1)(有 4 个邻居)
box[4](也有 4 个邻居)
box[14](有 4 个邻居)
box[16](有 4 个邻居)
所以在这种情况下最近的框的邻居之和 = 4+4+4+4 =16
对于 box[13] ,最近的框是:
box[3](有 6 个邻居)
box[5](也有 4 个邻居)
box[6](有 3 个邻居)
box[12](有 3 个邻居)
所以在这种情况下最近的框的邻居之和 = 6+4+3+3 =16
这里(相似框)box[8] 和 box[13] 的邻居总数 = 16+16 =32。
同样,我想将所有具有 4 个邻居的框分组,并找到离它们最近的框的邻居之和。并对每个相似的组继续。
我的代码
这是我的代码。
#include <iostream>
#include <cstdlib>
#include <vector>
#include <stdio.h>
using namespace std;
class Rect {
public:
double x1, x2, y1, y2; // coordinates
Rect(double X1, double Y1, double X2, double Y2) {
if (X1 < X2) {
x1 = X1; x2 = X2;
} else {
x2 = X1; x1 = X2;
}
if (Y1 < Y2) {
y1 = Y1; y2 = Y2;
} else {
y2 = Y1; y1 = Y2;
}
}
bool isAdjacent(Rect rect) {
if (x1 == rect.x1 || x1 == rect.x2 ||
x2 == rect.x1 || x2 == rect.x2) {
// use only < when comparing y1 and rect.y2 avoids sharing only a corner
if (y1 >= rect.y1 && y1 < rect.y2) {
return true;
}
if (y2 > rect.y1 && y2 <= rect.y2) {
return true;
}
if (rect.y1 >= y1 && rect.y1 < y2) {
return true;
}
if (rect.y2 > y1 && rect.y2 <= y2) {
return true;
}
}
if (y1 == rect.y1 || y1 == rect.y2 ||
y2 == rect.y1 || y2 == rect.y2) {
if (x1 >= rect.x1 && x1 < rect.x2) {
return true;
}
if (x2 > rect.x1 && x2 <= rect.x2) {
return true;
}
if (rect.x1 >= x1 && rect.x1 < x2) {
return true;
}
if (rect.x2 > x1 && rect.x2 <= x2) {
return true;
}
}
return false;
}
};
void isNearest(int b){
vector<Rect> rects;
//Rect( x1 , y1 , x2 , y2 )
rects.push_back(Rect(0.0000,0.0000, 0.8147,0.1355));
rects.push_back(Rect(0.8147,0.0000, 1.0000,0.1355));
rects.push_back(Rect(0.8147,0.1355, 0.9058,0.8350));
rects.push_back(Rect(0.0000,0.1355, 0.1270,0.9689));
rects.push_back(Rect(0.9058,0.1355, 0.9134,0.2210));
rects.push_back(Rect(0.9058,0.8350, 1.0000,1.0000));
rects.push_back(Rect(0.8147,0.8350, 0.9058,1.0000));
rects.push_back(Rect(0.1270,0.1355, 0.6324,0.3082));
rects.push_back(Rect(0.1270,0.9689, 0.8147,1.0000));
rects.push_back(Rect(0.0000,0.9689, 0.1270,1.0000));
rects.push_back(Rect(0.9134,0.1355, 1.0000,0.2210));
rects.push_back(Rect(0.9134,0.2210, 1.0000,0.8350));
rects.push_back(Rect(0.9058,0.2210, 0.9134,0.8350));
rects.push_back(Rect(0.6324,0.1355, 0.8147,0.3082));
rects.push_back(Rect(0.6324,0.3082, 0.8147,0.9689));
rects.push_back(Rect(0.1270,0.3082, 0.6324,0.9689));
int nearBox_count = 0;
double TotalArea=0;
for (int x = 0; x < rects.size(); ++x) {
if (rects[b].isAdjacent(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] \n", (b+1), (x+1));
}
}
printf("Total number of nearest box for [%d] is %d \n",(b+1),nearBox_count );
printf("\n");
}
int main() {
for (int i = 0; i < 16; ++i)
{
isNearest(i);
}
return 0;
}
它给出了这样的正确结果-
box[1] is nearest to box[2]
box[1] is nearest to box[4]
box[1] is nearest to box[8]
box[1] is nearest to box[14]
Total number of nearest box for [1] is 4
box[2] is nearest to box[1]
box[2] is nearest to box[3]
box[2] is nearest to box[5]
box[2] is nearest to box[11]
Total number of nearest box for [2] is 4
box[3] is nearest to box[2]
box[3] is nearest to box[5]
box[3] is nearest to box[7]
box[3] is nearest to box[13]
box[3] is nearest to box[14]
box[3] is nearest to box[15]
Total number of nearest box for [3] is 6
box[4] is nearest to box[1]
box[4] is nearest to box[8]
box[4] is nearest to box[10]
box[4] is nearest to box[16]
Total number of nearest box for [4] is 4
box[5] is nearest to box[2]
box[5] is nearest to box[3]
box[5] is nearest to box[11]
box[5] is nearest to box[13]
Total number of nearest box for [5] is 4
box[6] is nearest to box[7]
box[6] is nearest to box[12]
box[6] is nearest to box[13]
Total number of nearest box for [6] is 3
box[7] is nearest to box[3]
box[7] is nearest to box[6]
box[7] is nearest to box[9]
box[7] is nearest to box[15]
Total number of nearest box for [7] is 4
box[8] is nearest to box[1]
box[8] is nearest to box[4]
box[8] is nearest to box[14]
box[8] is nearest to box[16]
Total number of nearest box for [8] is 4
box[9] is nearest to box[7]
box[9] is nearest to box[10]
box[9] is nearest to box[15]
box[9] is nearest to box[16]
Total number of nearest box for [9] is 4
box[10] is nearest to box[4]
box[10] is nearest to box[9]
Total number of nearest box for [10] is 2
box[11] is nearest to box[2]
box[11] is nearest to box[5]
box[11] is nearest to box[12]
Total number of nearest box for [11] is 3
box[12] is nearest to box[6]
box[12] is nearest to box[11]
box[12] is nearest to box[13]
Total number of nearest box for [12] is 3
box[13] is nearest to box[3]
box[13] is nearest to box[5]
box[13] is nearest to box[6]
box[13] is nearest to box[12]
Total number of nearest box for [13] is 4
box[14] is nearest to box[1]
box[14] is nearest to box[3]
box[14] is nearest to box[8]
box[14] is nearest to box[15]
Total number of nearest box for [14] is 4
box[15] is nearest to box[3]
box[15] is nearest to box[7]
box[15] is nearest to box[9]
box[15] is nearest to box[14]
box[15] is nearest to box[16]
Total number of nearest box for [15] is 5
box[16] is nearest to box[4]
box[16] is nearest to box[8]
box[16] is nearest to box[9]
box[16] is nearest to box[15]
Total number of nearest box for [16] is 4
虽然它可以识别最近的盒子并计算邻居的数量,但我无法弄清楚如何将相似的盒子分组(如上所述)并找到总和。
我被困在这里了。谁能帮帮我?
更新的代码片段
vector<CheckRect> rects;
unsigned isNearest(unsigned b, vector<unsigned>& neighbours) {
unsigned nearBox_count = 0;
for (unsigned x = 0; x < rects.size(); ++x) {
if (rects[b].isAdjacent(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] \n", (b+1), (x+1));
neighbours.push_back(x);
}
}
printf("Total number of nearest box for [%d] is %d \n",
(b+1), nearBox_count );
printf("\n");
return nearBox_count;
}
int main(){
cin>>N;
for(int b=0; b<N; b++){
ifstream inputFile1("RectCoordinates.txt"); //input from the file previously generated
int rect_number;
double xa0,ya0,xa1,ya1;
int neighbours;
isNearest( b, &neighbours);// This is the line that causing my ERROR
}
vector<unsigned> nearBox_count(rects.size());
vector< vector<unsigned> > neighbours(rects.size());
for (unsigned i = 0; i < rects.size(); ++i) {
nearBox_count[i] = isNearest(i, neighbours[i]);
}
// Calculate the sums of neighbouring boxes
vector<unsigned> neighCount(rects.size(), 0);
for (unsigned i = 0; i < rects.size(); i++) {
for (unsigned j = 0; j < neighbours[i].size(); j++) {
neighCount[i] += nearBox_count[neighbours[i][j]];
}
}
// Calculate your result
map<unsigned,unsigned> finalCount;
for (unsigned i = 0; i < rects.size(); i++)
{
if (finalCount.count(nearBox_count[i]) == 0)
finalCount[nearBox_count[i]] = neighCount[i];
else
finalCount[nearBox_count[i]] += neighCount[i];
}
// Print the result
for (map<unsigned,unsigned>::iterator it = finalCount.begin();
it != finalCount.end(); ++it) {
printf("Sum neighbours for the neighbours of similar boxes with %d "
"neighbours is %d\n", it->first, it->second);
}
return 0;
}
给我错误-
ss.cpp: In function ‘int main()’:
ss.cpp:102:29: error: invalid initialization of reference of type ‘std::vector<unsigned int>&’ from expression of type ‘unsigned int’
ss.cpp:22:10: error: in passing argument 2 of ‘unsigned int isNearest(unsigned int, std::vector<unsigned int>&)’
我该如何解决?
最佳答案
与其尝试在某些数据结构中维护矩形之间的关系,不如让矩形对象本身变得智能并了解它的邻居数量以及它们是谁可能是一个更好的主意。
例如(用于说明想法的不完整原型(prototype)):
class Rect {
public:
//methods
Rect(double X1, double Y1, double X2, double Y2);
//const access
double getX1() const;
double getX2() const;
double getY1() const;
double getY2() const;
int numNeighbors() const { return neighbors.size();}
int sumOfNeighbors() const { int res(0); for(size_t i=0;i< neighbors.size();++i) res += neighbors[i]->numNeighbors(); return res;}
std::vector<Rect*> getNeighbors() {return neighbors};
void addNeighbor(Rect* newNeighbor) {neighbors.push_back(newNeighbor);}
//data
private:
double x1, x2, y1, y2; // coordinates
std::vector<Rect*> neighbors;
};
有了这样的 rect 类,你可以为每个矩形添加邻居,检索它自己的每个 rect 的所有邻居,以及它们的所有邻居 - 所有关系都在 rect 本身而不是一些外部对象中维护,主程序的代码应该非常最小。
填充矩形后,您可以简单地遍历它们,选择具有所需邻居数量的矩形,并对它们执行任何操作。
关于c++ - 将相似的框分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18360313/
我遇到了一个让我非常难过的问题。但现在看来这一定是一个普遍的问题,我很惊讶我找不到关于它的另一个问题。 基本上,我的网站上会出现一些图标。我希望它们在每台设备上都显示出大致相同的大小——大致是可触摸的
我是一名 Java 专业人士。现在我喜欢转向其他编程语言。谁能建议我哪种编程语言更容易消化,并且该语言具有 Web 开发功能。我对 Web 开发感兴趣。 最佳答案 C# - 网络开发非常好(只需看看这
我需要为不同的 url 发送不同的响应。但是我正在使用的正则表达式不起作用。 有问题的两个正则表达式是 "/v1/users/[^/]+/permissions/domain/HTTP/" (例
让我描述一下我的问题。有一个输入字符串和一个包含数千个字符串的表。我正在寻找搜索与输入字符串最相似* 字符串的最佳方法。搜索应该返回一个包含 ~10 个建议字符串的列表,按相似度排序。字符串在数据库中
我在使用 crypt() 时遇到问题,如果用户有密码(本例中为 password1),并且他们将其更改为 password2,则散列会返回相同的结果。您可以在此处进行测试:旧链接输入 password
我问这个是因为我发现 XML 在视觉上没有吸引力。我知道 XML 非常有名,很有用,而且它被大多数(如果不是全部)程序员接受为一种非常有用的定义和结构化数据的语言;然而,我真的不觉得盯着它看、看着它或
我有工作需要在 14 天内完成。我有5名 worker 。一天正好需要3个 worker 。每个 worker 最多只能工作 9 天。每个 worker 都有自己的日期偏好,每个 worker 每天都
FileStream 或 DB 句柄类型的对象必须正确关闭,因为它在内部使用 Windows 句柄。同样,Threads 也使用 Windows 句柄吧?线程是否有一些内部机制来释放那些 Window
如果这是非常基本的,请原谅。我一直在尝试遍历行数组中的每个单元格包括标题单元格。我可以在 Nokogiri CSS 选择器中使用 OR 运算符吗? thang= Nokogiri::HTML(IO.r
默认情况下,RichTextFx(顶部控件)看起来不像 TextArea(底部控件): 我找到了一种添加圆角的方法(只需从 modena.css 复制并配置 some styles): .virtua
在 64 位 Java 中,每个对象实例往往包含一个 192-bit header , 包含 类指针, 标志和 锁(每个 64 位)。 这可能会导致小对象的大量内存开销。 尼姆的情况是否类似?用两种语
我正在开发一个具有多个功能(通过 tableview 选择)的应用程序,这些功能几乎都做同样的事情,相似的布局、相似的按钮、相似的输入字段等等。但在我相信它们自己的地方有足够的不同类(class)。每
题目地址:https://leetcode-cn.com/problems/similar-rgb-color/ 题目描述 Inthe following, every capital lette
我正在编写 R 代码,我希望它在“非调试”或“调试”模式下运行。在 Debug模式下,我希望代码打印出运行时信息。 在其他语言中,我通常会有某种打印功能,除非打开标志(用于编译或运行时),否则什么都不
我不明白这个OP接受的答案的一部分: Javascript function scoping and hoisting 作者说: " 此外,在这种情况下, function a() {} 行为与相同
我正在做一个bcmp(&v6_addr1,&v6_addr2,sizeof(v6_addr1)) (gdb) p v6_addr2 $15 = {u = {b = "0\001", '\000' ,
我有一个充满按钮的 Canvas,我将其 ID 存储在字典中。 Canvas 很长,带有垂直滚动条。有一种方法可以自动将 View 定位在给定的按钮上吗? 当使用 Text 时,txt.see(pos
我正在尝试使用 Python 2.6 和 PIL 计算两个图像的相似度(读作:Levenshtein 距离)。 我打算给我们e python-levenshtein用于快速比较的库。 主要问题: 什么
换句话说,php $object->method(); 和 $object->property = 'someValue'; 相当于,js: $object.method(); 和 $object.p
我写了一个简单的函数,它接收实现 .index() 的东西和要检查的字符列表。 我的假设是,由于字符串和元组都是不可变的,因此它们具有相似的性能(或者至少,元组的性能优于列表)。相反,元组似乎等同于列
我是一名优秀的程序员,十分优秀!