-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdist_to_center.cpp
59 lines (50 loc) · 1.26 KB
/
dist_to_center.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
struct Point{
double x,y,z;
Point(double x, double y, double z) : x(x), y(y), z(z){}
};
double getDist(Point A, Point B){
double x = A.x - B.x;
double y = A.y - B.y;
double z = A.z - B.z;
return sqrt(x*x + y*y + z*z);
}
int main(int argc, char *argv[]){
if(argc < 3){
cerr << "Error, el archivo espera 2 parametros (nombre de archivo de entrada y de salida)" << endl;
cerr << "Ejemplo: ./dist_to_center ./datasets/Complete/alabastron.obj ./distancias.txt" << endl;
exit(1);
}
string infile = argv[1];
string outfile = argv[2];
cerr << "Archivo de entrada = " << infile << endl;
cerr << "Archivo de salida = " << outfile << endl;
freopen(infile.c_str(), "r", stdin);
freopen(outfile.c_str(), "a", stdout);
string line;
vector<Point> points;
double xt = 0, yt = 0, zt = 0;
while(getline(cin,line)){
if(line[0] == 'v'){
istringstream iss(line);
char ch; double x,y,z;
iss >> ch >> x >> y >> z;
points.push_back(Point(x,y,z));
xt += x;
yt += y;
zt += z;
}
}
int n = points.size();
xt /= n;
yt /= n;
zt /= n;
Point center(xt,yt,zt);
double dist = 0;
for(Point p : points){
dist = max(dist, getDist(p,center));
}
cout << fixed << setprecision(6) << dist << endl;
return 0;
}