• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Moon Distance

sarker306 Sep 18th, 2019 (edited) 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. #include <math.h>
3.
4. const double pi = 2*acos(0);
5. const double R = 6371; /* the radius of earth in km */
6.
7. /* given latitudes on same longitudes and zenith angles, get the distance of the moon.
8.    only works when latitudes are on different hemispheres.
9.    latitude on southern hemisphere is negative */
10. double get_moon_distance(double lat1, double lat2, double zen1, double zen2);
11.
12. int main(){
13.     double lat1, lat2, zen1, zen2;
14.
15.     while (scanf("%lf%lf%lf%lf", &lat1, &lat2, &zen1, &zen2) != EOF ){
16.         printf("%lf\n", get_moon_distance(lat1, lat2, zen1, zen2));
17.     }
18.
19.     return 0;
20. }
21.
22. double get_moon_distance(double lat1, double lat2, double zen1, double zen2) {
23.     double tmp;
24.     double lin_dist;
25.     double ang_dist;
26.     double lunar_angle_between_points;
27.     double moon_dist_from_center;
28.     double moon_dist_from_upper_point;
29.
30.     /* make sure lat1 > lat2 */
31.     if ( lat1 < lat2 ) {
32.         tmp = lat1; lat1 = lat2; lat2 = tmp;
33.         tmp = zen1; zen1 = zen2; zen2 = tmp;
34.     }
35.
36.     /* convert angles into radian */
37.     lat1 *= pi / 180.; lat2 *= pi / 180.; zen1 *= pi / 180.; zen2 *= pi / 180.;
38.
39.     ang_dist = lat1 - lat2;
40.     lin_dist = R * sin(ang_dist) / cos(ang_dist/2);
41.     lunar_angle_between_points = zen1 + zen2 - ang_dist;
42.     moon_dist_from_upper_point = lin_dist * cos(- zen2 + ang_dist/2) / sin(lunar_angle_between_points);
43.     tmp = R / moon_dist_from_upper_point;
44.     moon_dist_from_center = moon_dist_from_upper_point * sqrt(1 + tmp * tmp + 2 * tmp * cos(zen1));
45.     return moon_dist_from_center - R;
46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top