1 // area.c -- routines to assist with inserting "areas" into FG terrain
3 // Written by Curtis Olson, started March 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
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.
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.
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.
22 // (Log is kept at end of this file)
29 #include <Include/fg_constants.h>
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) );
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) );
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 ) {
52 offset.dist *= METER_TO_NM * NM_TO_RAD;
54 result.lat = asin( sin(orig.lat) * cos(offset.dist) +
55 cos(orig.lat) * sin(offset.dist) * cos(offset.theta) );
57 if ( cos(result.lat) < FG_EPSILON ) {
58 result.lon = orig.lon; // endpoint a pole
61 fmod(orig.lon - asin( sin(offset.theta) * sin(offset.dist) /
62 cos(result.lat) ) + FG_PI, FG_2PI) - FG_PI;
69 point2d cart_to_polar_2d(point2d in) {
71 result.dist = sqrt( in.x * in.x + in.y * in.y );
72 result.theta = atan2(in.y, in.x);
78 void batch_cart_to_polar_2d(point2d *in, point2d *out, int size) {
81 for ( i = 0; i < size; i++ ) {
82 out[i] = cart_to_polar_2d( in[i] );
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
91 void make_area(point2d orig, point2d *cart, point2d *result,
92 int size, double angle ) {
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);
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;
109 printf("(%.2f, %.2f)\n", rad[i].dist, rad[i].theta);
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);
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)
133 l = (length / 2.0) + (length * 0.1);
134 w = (width / 2.0) + (width * 0.1);
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);
146 make_area(orig, cart, result, 4, heading);
148 for ( i = 0; i < 4; i++ ) {
149 printf("(%.8f, %.8f)\n", result[i].lon, result[i].lat);
158 // Revision 1.1 1998/07/20 12:54:05 curt