-3

テキスト ファイルに 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 の場合、2 行目の x3、y3、x4、y4 の値をスキップします。 .

一致するポイントの数をカウントし、ポイントが見つかった行の数を返すようにしたかったのです。どうすればいいですか?

4

2 に答える 2

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; 

これで、「行」の値を使用して、条件に一致するかどうかを判断し、関連情報を出力できます。

于 2013-06-30T18:26:08.773 に答える
0

これは答えではないかもしれませんが、OPへの明確化の質問です

テキスト ファイルが 4 辺の多角形と多角形の面積の点のテーブルであると仮定します。

最初の質問は、線分[x1,y1,x2,y2]と線分の交点を見つけること[x3, y3, x4, y4]ですか?

[x1,y1,x2,y2]他の行の点に対して線分をチェックしますか?

いずれにせよ、私は次のことを提案します。

  • X 値と Y 値を含む Point クラスを作成します。
  • 2 つのポイントを含むクラス Line を作成します。

class Line
{
Point begin;
Point end;
};

  • 4 つの点と領域を持つ Polygon という名前のクラスを作成します。

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

  • ファイルをstd::vector<Polygon>.
  • ポイント、ライン、およびポリゴンに関してアルゴリズムを決定します。
    多角形、点、および線 (セグメント) の観点から考えてください。

うまくいけば、これで混乱が解消されるはずです。新しい投稿を作成し、ポイントとポリゴンの観点から話すことをお勧めします。

于 2013-06-30T18:25:34.513 に答える