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 60 61 62 63 64
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cmath>
using namespace std;
// Function prototypes
void galacticToCartesian( double L, double B, double r, double &x, double &y, double &z );
//======================================================================
int main( int argc, char *argv[] )
{
int numStars, numRead = 0;
double L, B, r; // L = longitude (aka azimuthal angle or phi), B = latitude (90-theta)
double x, y, z; // Assumes x axis is L=B=0, y axis is L=90, B=0 and z axis is B=90 deg
// Filenames; either accept the default or enter at the command line
string infileName = "input.txt";
string outfileName = "output.txt";
if ( argc > 1 ) infileName = argv[1];
if ( argc > 2 ) outfileName = argv[2];
ifstream in ( infileName ); assert( in );
ofstream out( outfileName );
in >> numStars;
out << numStars << '\n';
while ( in >> L >> B >> r )
{
numRead++;
galacticToCartesian( L, B, r, x, y, z );
#define SP << " " << fixed << setprecision( 5 ) << setw( 10 ) << // output format as required
out SP x SP y SP z << '\n';
// cout SP x SP y SP z << '\n';
}
in.close();
out.close();
cout << numStars << " stars stated." << '\n';
cout << numRead << " sets of coordinates written to file " << outfileName << '\n';
}
//======================================================================
void galacticToCartesian( double L, double B, double r, double &x, double &y, double &z )
{
constexpr double PI = 3.14159265358979;
constexpr double DEGTORAD = PI / 180.0;
double rcyl = r * cos( B * DEGTORAD ); // distance from polar axis (assumes B is latitude, not theta)
x = rcyl * cos( L * DEGTORAD );
y = rcyl * sin( L * DEGTORAD );
z = r * sin( B * DEGTORAD );
}
|