与特定行和列的数据比较
Data comparison with specific row and column
我在一个文本文件中有一组10000行10列的数据。这里有一些样品-
# x1 y1 x2 y2 x3 y3 x4 y4 Area
1 0.0000 0.0000 0.8147 0.0000 0.8147 0.1355 0.0000 0.1355 0.1104
2 0.8147 0.0000 1.0000 0.0000 1.0000 0.1355 0.8147 0.1355 0.0251
3 0.8147 0.1355 0.9058 0.1355 0.9058 0.8350 0.8147 0.8350 0.0637
4 0.0000 0.1355 0.8147 0.1355 0.8147 1.0000 0.0000 1.0000 0.7043
5 0.9058 0.1355 1.0000 0.1355 1.0000 0.8350 0.9058 0.8350 0.0659
6 0.9058 0.8350 1.0000 0.8350 1.0000 1.0000 0.9058 1.0000 0.0155
7 0.8147 0.8350 0.9058 0.8350 0.9058 1.0000 0.8147 1.0000 0.0150
我想使用这个程序来检查这些点
#include <iostream>
#include <cmath>
using namespace std;
double CheckPoint(){
double slope, intercept,A, B, C, D,px, py,left, top, right, bottom,dx, dy;
cin >> A; // take from column
cin >> B; // take from column
cin >> C; // take from column
cin >> D; // take from column
cin >> px; // take value from other rows and column and check
cin >> py; // take value from other rows and column and check
dx = C - A;
dy = D - B;
slope = dy / dx;
// y = mx + c
// intercept c = y - mx
intercept = B - slope * A; // which is same as D - slope * C
// For Bounding Box
if(A < C)
{
left = A;
right = C;
}
else
{
left = C;
right = A;
}
if(B < D)
{
top = B;
bottom = D;
}
else
{
top = B;
bottom = D;
}
if( slope * px + intercept > (py - 0.01) &&
slope * px + intercept < (py + 0.01))
{
if( px >= left && px <= right &&
py >= top && py <= bottom )
{
// cout the numbers of common point and the line number
}
else
// cout the numbers of common point and the line number
}
else
// cout no common point;
}
int main()
{
cout<<CheckPoint();
return 0;
}
首先我想
- 从x1中取A的值
- 从y1中取B的值
- 从x2中取C的值
- 从y2中取D的值
- 并检查它们是否与x3、y3、x4和y4有任何共同点。但它不会检查自己行中的值。对于其他行,此过程也将继续
然后我想
- 从x2中取A的值
- 从y2中取B的值
- 从x3中取C的值
- 从y3中取D的值
- 并检查它们是否与x1、y1、x4和y4有任何公共点。如上所述,它不会检查自己行的值,也不会应用于row2、row3。。。。等等
回复评论的示例
当程序取A=0.0000 B=0.0000 C=0.8147 D=0.0000时,它不会将值与x3=0.8147 y3=0.1355 x4=0.0000 y4=0.1355进行比较。即,它将跳过取x1、y1、x2、y2值的行。
再次,当它从第二行获取A、B、C、D的值时,即0.A=8147 B=0.0000 C=1.0000 D=0.0000,则它跳过第2行中的x3、y3、x4、y4的值。
我想让它计算匹配点的数量,并返回找到该点的行的数量。我该怎么做?
显然,这个问题包括从一个还包含一些其他内容的列表中读取成对的数字(x,y)。让我们先来讨论一下:
std::fstream fin("infile.txt"); // Adjust as needed.
struct Point
{
double x, y;
};
struct Line
{
int lineNum;
Point pts[4];
double area;
}
Line ln;
fin >> ln.lineNum; // Read the line number.
for(int i = 0; i < 4; i++)
{
fin >> line.pts[i].x >> line.pts[i].y;
}
fin >> ln.area;
现在,您可以使用"line"中的值来确定它是否符合您的条件,并输出相关信息。
这可能不是对O.p.的回答,而是一个澄清问题
假设文本文件是四边形的点表和多边形的面积。
第一个问题是找到线段[x1,y1,x2,y2]
和线段[x3, y3, x4, y4]
之间的交点吗?
是否要将线段[x1,y1,x2,y2]
与其他行中的点进行检查?
无论如何,我建议如下:
- 创建一个包含X和Y值的类"点"
- 创建一个包含两点的类Line:
class Line
{
Point begin;
Point end;
};
- 创建一个名为"多边形"的类,该类包含4个点和一个区域:
class Polygon
{
Point point_container[4];
double area;
};
- 将文件读入
std::vector<Polygon>
- 根据点、线和多边形来确定算法
用多边形、点和线(线段)来思考
希望这能消除混乱。我建议做一个新的帖子,用点和多边形来说话。
相关文章:
- 将字符串与文件上的数据进行比较
- 如何在 c++ 中比较 2 个链表并将匹配的数据放入另一个链表中
- 将收到的 usart uint8_t* 数据与常量字符串进行比较
- 比较付款数据
- 将constexpr字节数组与缓冲区的一部分(指向数据的指针)进行比较
- 任何方法都可以在C++中比较数据结构中的许多方法
- C++,"由于数据类型范围有限,比较总是正确的"
- 比较存储在 std::string 中的数据中的字节数
- C++ - 比较模板和类之间的数据类型
- 将此类数据作为字符串进行比较时出现问题
- 将当前多边形数据单元与下一个多边形单元格 - VTK 进行比较
- 比较 JSON::Value 变量中的数据,然后更新到文件
- 如何使用自定义比较器初始化类数据成员,该成员是 std::set
- 将 ProcessEntry32.szExeFile 与用户输入的数据进行比较时,C++ _wcsicmp代码未编译
- 可以比较不同数据类型 c++ 的模板函数
- 比较推送数据(100 万个数字)在 std::vector 中预先调整大小和没有
- 开发C++数据比较的警告
- 平滑后的GPS数据比较
- 与特定行和列的数据比较
- 当数据比较复杂时,读写二进制数据到文件