how to do coordinate tranformation to cartesian

Hi:
I have set of galactic coordinates and want to convert them to cartesian coordinates.
Thanks
Last edited on
Please provide a sample input.
closed account (48T7M4Gy)
Galactic coordinates l, b alone won't give you enough information to get all 3 x,y and z coordinates.

To convert (decimal) degrees to radians multiply the degrees by PI/180

The sample input of the star coordinates in galactic coordinates system as follow:
99565
315.354095 58.873371 5.652584
315.163086 58.982288 11.904491
315.292389 58.879555 12.095946
315.314941 58.796120 14.750858
315.155487 58.818413 9.117417
315.136444 58.880348 21.239729
315.384277 58.873161 21.124430
315.390137 58.881294 20.614161
315.124756 58.863174 23.276495
315.253357 58.811478 50.708523
315.269409 58.791531 38.226456
315.540802 58.877274 2.810194
.
.
I have a bout 18 text files which each one includes above 90000 coordinates.
The first line will contain the number of stars in the file
The remainer of the file are star points (three values per star per line).
Last edited on
closed account (48T7M4Gy)
Which column is L and which column is B?
It doesn't matter which are L and B.

Remember your sine and cosine functions. The trick is to remember that the ray from origin to D (radius) must be transformed twice.

1
2
3
4
5
6
7
8
void spherical_to_cartesian( 
  double inclination, double azimuth, double radius,
  double& x, double& y, double& z
) {
  x = radius * std::sin( inclination ) * std::cos( azimuth );
  y = radius * std::sin( inclination ) * std::sin( azimuth );
  z = radius * std::cos( inclination );
}

Remember that the angles must be in radians. Helper functions:

1
2
double to_radians( double x ) { return x * 3.141592 / 180; }
double to_degrees( double x ) { return x * 180 / 3.141592; }

I personallay can't stand it when people use non-standard 1-letter replacements for stuff, but I suppose it is possible you're from a country where those letters are standard...

The usual values are inclination θ (theta), azimuth φ (phi), and radius r.

Hope this helps.
closed account (48T7M4Gy)
It doesn't matter which are L and B.


Well I think it does, and I wouldn't have asked otherwise, because there is no way of determining a radius from a two-angle galactic coordinate measurement. I can only guess the first column is L and the second column is B.

So what is the third column? One of the great OP mysteries as it stands. Perhaps you know which is x y and z because my geometry/trigonometry differentiates between an angle and a line.



No, it only matters if you are aligning to a known coordinate system, otherwise your visualization will be fine.

We can only make good guesses as to which column represents what; the question can be answered without caring either way (as I did).

If I were to guess from the looks of the data and OP's comments, though, it looks like it is (L, B, r).
they're essentially spherical coordinates


Not quite. Radius r is the same, azimuthal angle (phi in spherical coordinates, L in galactic coordinates) is the same. However, galactic coordinates use latitude B (angle from the equator), whilst spherical coordinates use co-latitude theta (angle from the pole).


It doesn't matter which are L and B.

I'm with @kemort on this. From the sample given I can't even work out which is the radius, let alone the the two angles.
I'd have a guess at L, B, r, with the radius in parsecs, but it could be in billions of km for all I can tell.
Last edited on
closed account (48T7M4Gy)
We can only make good guesses as to which column represents what
This is now completely pointless. All the guy has to do is answer the question.
Guessing at ... L, B, r
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 );
}


input.txt
8
30    0     1
120   45    1
225   90    1
300   30    1
60   -30    2
120  -45    2
225  -60    2
300  -30    2


output.txt
8
     0.86603     0.50000     0.00000
    -0.35355     0.61237     0.70711
    -0.00000    -0.00000     1.00000
     0.43301    -0.75000     0.50000
     0.86603     1.50000    -1.00000
    -0.70711     1.22474    -1.41421
    -0.70711    -0.70711    -1.73205
     0.86603    -1.50000    -1.00000



But what do you plan to do with 1.6 million stars?
Last edited on
Topic archived. No new replies allowed.