0

I have a text file that include X,Y,Z coordinates of points. My aim is finding minimum and maximum points and write it another file. For this aim I write a distance function. The points that have maximum distance is minimum and maximum points. here is my code. It works but it does not calculate or write anything.

#include <iostream>
#include<fstream>
#include <math.h>

using namespace std;

double distance (float X1, float Y1, float Z1, float X2, float Y2, float Z2)
{
    return sqrt(pow((X2-X1),2)+ pow((Y2-Y1),2)+pow((Z2-Z1),2));
}

int main ()
{ 
    float x1, y1, z1,x2, y2,z2;
    ifstream file("D:\\points.txt");
    ofstream result ("D:\\result.txt");
    double bigdistance=0;

    if (file.is_open())
    { 
        while (!file.eof())
        {
            file>>x1>>y1>>z1;
            while (!file.eof())
            {
                file>>x2>>y2>>z2;
                double d= distance (x1,y1,z1,x2,y2,z2);
                if (bigdistance<d)
                {
                    bigdistance=d;
                    result<<x1<<y1<<z1<<endl<<x2<<y2<<z2;
                }
            }
        }
    }
    else cout <<"cannot open file";

    system ("PAUSE");
    return 0;
}
dwxw
  • 1,089
  • 1
  • 10
  • 17
user2875046
  • 1
  • 1
  • 1
  • It doesn't do what it's supposed to do, and yet it works? Even if you hadn't made the mistake of using `eof()`, you still wouldn't check all pairs. – molbdnilo Oct 15 '13 at 09:06

2 Answers2

0

A couple of suggestions:

while (file >> x1 >> y1 >> z1 >> x2 >> y2 >> z2) {

}

will read your input and stop if any of the reads failed.

You will want to read the entire input and store it in a vector of points, or similar. Then, you can use two nested loops to iterate over each pair of points. Right now, your programs assumes that the input file contains all pairs. That is, each point is read multiple times form the input, in the inner loop.

There are also algorithms that are faster than quadratic, and you might need those if you have more than a couple thousand input points. See for example this StackOverflow question.

Community
  • 1
  • 1
Sjlver
  • 1,227
  • 1
  • 12
  • 28
  • Thanks all your answers. I am really new in C++ and the other programing language. Sjlver, You are right a need faster algorithms, Becasuse my txt file include nearly 3 thousand points. Every line holds X,Y,Z value of points coordinates. I want to calculate distance between all points. Like Point1->point2, point 1->point3.....point1 -->pointn; point2->point3, point2->point4....pointn; And using this distance I want to find point which have minimum coordinates and point which I maximum coordinates. – user2875046 Oct 15 '13 at 14:31
  • Here there is a line sample of my txt file. -18.810526 -6.0689259 -2.4343202 – user2875046 Oct 15 '13 at 14:32
0
#include <iostream>
#include<fstream>
#include <math.h>

using namespace std;
enter code here`{
    for (int i = 1; !f1.eof(); i++)
    {
        int x1, y1, x2, y2, x3, y3, x4, y4);
        read(f1, x1, y1, x3, y3);
        x2 = x3;
        y2 = y1;
        x4 = x1;
        y4 = y3;

        if (y4 > y1)
        {
            a = y1 - y4;
        }
        else
            a = y4 - y1;


        if (x4 > x3)
        {
            b = x4 - x3;
        }
        else
            b = x3 - x4;
    }
}