与特定行和列的数据比较

Data comparison with specific row and column

本文关键字:数据 比较      更新时间:2023-10-16

我在一个文本文件中有一组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>
  • 根据点、线和多边形来确定算法
    用多边形、点和线(线段)来思考

希望这能消除混乱。我建议做一个新的帖子,用点和多边形来说话。