与特定行和列的数据比较

时间:2013-06-30 17:38:58

标签: c++ arrays algorithm point

我在文本文件中有一组包含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的值。

再次从第2行取A,B,C,D的值,即0.A = 8147 B = 0.0000 C = 1.0000 D = 0.0000,则跳过x3,y3,x4,y4的值在第2行。

我希望它计算匹配点的数量并返回找到该点的行数。我该怎么办?

2 个答案:

答案 0 :(得分:1)

显然,这个问题包括从包含一些其他内容的列表中读取数字对(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”中的值来确定它是否符合您的条件,并输出相关信息。

答案 1 :(得分:0)

这可能不是答案,而是对O.P.的澄清问题

假设文本文件是4边多边形和多边形区域的点表。

第一个问题是找到线段[x1,y1,x2,y2]和线段[x3, y3, x4, y4]之间的任何交叉点?

是否要针对来自其他行的点检查线段[x1,y1,x2,y2]

无论如何,我建议如下:

  • 创建一个Point类,包含X和Y值。
  • 创建一个包含两个点的类Line:

class Line
{
Point begin;
Point end;
};

  • 创建一个名为Polygon的类,其中包含4个点和一个区域:

class Polygon
{
Point point_container[4];
double area;
};

  • 将文件读入std::vector<Polygon>
  • 根据点,线和多边形确定算法 根据多边形,点和线(片段)进行思考。

希望这可以消除困惑。我建议发一个新帖子并用点和多边形说话。