File: location.cc

package info (click to toggle)
xplanet 0.43-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 904 kB
  • ctags: 321
  • sloc: cpp: 3,041; sh: 1,977; makefile: 141
file content (98 lines) | stat: -rw-r--r-- 2,314 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/******************************************************************************
    Xplanet 0.43 - render an image of the earth into an X window
    Copyright (C) 1999 Hari Nair <hari@alumni.caltech.edu>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

******************************************************************************/

#include <math.h>

#include "location.h"
#include "util.h"

location::location (double r) 
{
  lon = 0.;
  lat = 0.;
  range = r;
}

location::location (double a, double b, double c) 
{
  x = a;
  y = b;
  z = c;
}
  
location::location (double latitude, double longitude, double r, 
		    char *text)
{
  lat = latitude * deg_to_rad;
  if (longitude > 180) longitude -= 360;
  else if (longitude < -180) longitude += 360;
  lon = longitude * deg_to_rad;
  range = r;

  if (text != NULL) 
    {
      name = new char [strlen(text) + 1];
      strcpy (name, text);
    }
  else 
    name = NULL;
}

void
location::setlocation (double a, double b, double c)
{
  x = a; 
  y = b;
  z = c;
}

double
location::dot (location l1)
{
  return (x * l1.x + y * l1.y + z * l1.z);
}

void 
location::rotate (double a[3][3]) 
{
  double newx = a[0][0] * x + a[0][1] * y + a[0][2] * z;
  double newy = a[1][0] * x + a[1][1] * y + a[1][2] * z;
  double newz = a[2][0] * x + a[2][1] * y + a[2][2] * z;
      
  x = newx;
  y = newy;
  z = newz;
}

void 
location::spher_to_rect (void) 
{
  x = range * cos (lon) * cos (lat);
  y = range * sin (lon) * cos (lat);
  z = range * sin (lat);
}
  
void 
location::rect_to_spher (void) 
{
  range = sqrt (x*x + y*y + z*z);
  lat = M_PI_2 - acos (z/range);
  lon = atan2 (y,x);
}