]> git.mxchange.org Git - flightgear.git/blob - Areas/area.cxx
Changed the way I handle #define ANSI_DECLARATORS
[flightgear.git] / Areas / area.cxx
1 // area.c -- routines to assist with inserting "areas" into FG terrain
2 //
3 // Written by Curtis Olson, started March 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23 //
24
25
26 #include <math.h>
27 #include <stdio.h>
28
29 #include <Include/fg_constants.h>
30
31 #include "area.hxx"
32
33
34 // calc new x, y for a rotation
35 double rot_x(double x, double y, double theta) {
36     return ( x * cos(theta) + y * sin(theta) );
37 }
38
39
40 // calc new x, y for a rotation
41 double rot_y(double x, double y, double theta) {
42     return ( -x * sin(theta) + y * cos(theta) );
43 }
44
45
46 // calc new lon/lat given starting lon/lat, and offset radial, and
47 // distance.  NOTE: distance is specified in meters (and converted
48 // internally to radians)
49 point2d calc_lon_lat( point2d orig, point2d offset ) {
50     point2d result;
51
52     offset.dist *= METER_TO_NM * NM_TO_RAD;
53
54     result.lat = asin( sin(orig.lat) * cos(offset.dist) + 
55                        cos(orig.lat) * sin(offset.dist) * cos(offset.theta) );
56
57     if ( cos(result.lat) < FG_EPSILON ) {
58         result.lon = orig.lon;      // endpoint a pole
59     } else {
60         result.lon = 
61             fmod(orig.lon - asin( sin(offset.theta) * sin(offset.dist) / 
62                                   cos(result.lat) ) + FG_PI, FG_2PI) - FG_PI;
63     }
64
65     return(result);
66 }
67
68
69 point2d cart_to_polar_2d(point2d in) {
70     point2d result;
71     result.dist = sqrt( in.x * in.x + in.y * in.y );
72     result.theta = atan2(in.y, in.x);    
73
74     return(result);
75 }
76
77
78 void batch_cart_to_polar_2d(point2d *in, point2d *out, int size) {
79     int i;
80
81     for ( i = 0; i < size; i++ ) {
82         out[i] = cart_to_polar_2d( in[i] );
83     }
84 }
85
86
87 // given a set of 2d coordinates relative to a center point, and the
88 // lon, lat of that center point, as well as a potential orientation
89 // angle, generate the corresponding lon and lat of the original 2d
90 // verticies.
91 void make_area(point2d orig, point2d *cart, point2d *result, 
92                int size, double angle ) {
93     point2d rad[size];
94     int i;
95
96     // convert to polar coordinates
97     batch_cart_to_polar_2d(cart, rad, size);
98     for ( i = 0; i < size; i++ ) {
99         printf("(%.2f, %.2f)\n", rad[i].dist, rad[i].theta);
100     }
101     printf("\n");
102
103     // rotate by specified angle
104     for ( i = 0; i < size; i++ ) {
105         rad[i].theta += angle;
106         while ( rad[i].theta > FG_2PI ) {
107             rad[i].theta -= FG_2PI;
108         }
109         printf("(%.2f, %.2f)\n", rad[i].dist, rad[i].theta);
110     }
111     printf("\n");
112
113     for ( i = 0; i < size; i++ ) {
114         result[i] = calc_lon_lat(orig, rad[i]);
115         printf("(%.8f, %.8f)\n", result[i].lon, result[i].lat);
116     }
117     printf("\n");
118 }
119
120
121 // generate an area for a runway
122 void gen_runway_area( double lon, double lat, double heading, 
123                       double length, double width,
124                       point2d *result, int *count) 
125 {
126     point2d cart[4];
127     point2d orig;
128     double l, w;
129     int i;
130
131     orig.lon = lon;
132     orig.lat = lat;
133     l = (length / 2.0) + (length * 0.1);
134     w = (width / 2.0) + (width * 0.1);
135
136     // generate untransformed runway area vertices
137     cart[0].x =  l; cart[0].y =  w;
138     cart[1].x =  l; cart[1].y = -w;
139     cart[2].x = -l; cart[2].y = -w;
140     cart[3].x = -l; cart[3].y =  w;
141     for ( i = 0; i < 4; i++ ) {
142         printf("(%.2f, %.2f)\n", cart[i].x, cart[i].y);
143     }
144     printf("\n");
145
146     make_area(orig, cart, result, 4, heading);
147
148     for ( i = 0; i < 4; i++ ) {
149         printf("(%.8f, %.8f)\n", result[i].lon, result[i].lat);
150     }
151     printf("\n");
152
153     *count = 4;
154 }
155
156
157 // $Log$
158 // Revision 1.1  1998/07/20 12:54:05  curt
159 // Initial revision.
160 //
161 //