Guest

Dmitri

By: a guest on Oct 10th, 2008  |  syntax: C  |  size: 3.66 KB  |  hits: 870  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. const double PI = 3.1415926535897932384626433832795028841971;
  5. const double DR = 180 / PI; //degree / radian factor;
  6. const double W = 2 * PI / 365; // earth's mean orbital angular speed in radians/day
  7. const double WR = PI / 12; // earth's speed of rotation relative to sun (radians/hour)
  8. double C = -23.45 / DR; // reverse angle of earth's axial tilt in radians
  9. const double ST = sin(C); // sine of reverse tilt
  10. const double CT = cos(C); // cosine of reverse tilt
  11. const double E2 = 2 * 0.0167; // twice earth's orbital eccentricity
  12. const double SN = 10 * W; // 10 days from December solstice to New Year (Jan 1)
  13. const double SP = 12 * W; // 12 days from December solstice to perihelion
  14.  
  15. double rad2deg(double radians)
  16. {
  17.     return (180.0/PI)*radians;
  18. }
  19.  
  20. double deg2rad(double degrees)
  21. {
  22.     return (PI/180.0)*degrees;
  23. }
  24.  
  25. double ang(double x, double y)
  26. {
  27.         double ang;
  28.         if (x > 0)
  29.           ang = atan(y/x);
  30.         else if (x < 0)
  31.           ang = atan(y/x) + PI;
  32.         else if (y > 0)
  33.           ang = 1 * PI / 2;
  34.         else if (y < 0)
  35.           ang = -1 * PI / 2;
  36.         else
  37.           ang = 0;
  38.  
  39.         return ang;
  40. }
  41.  
  42. void P2C(double AZ, double EL, double *x, double *y, double *z)
  43. {
  44.         double c;
  45.         *z = sin(EL);
  46.         c = cos(EL);
  47.         *x = c * sin(AZ);
  48.         *y = c * cos(AZ);
  49. }
  50.  
  51. void C2P(double x, double y, double z, double *AZ, double *EL)
  52. {
  53.         *EL = ang(sqrt(x * x + y * y), z);
  54.         *AZ = ang(y, x);
  55.         if (*AZ < 0)
  56.           *AZ = *AZ + 2 * PI;
  57. }
  58.  
  59. int main (int argc, char *argv[])
  60. {
  61.         double A, B, SL, D, CL, DC, LD, sAZ, sEL, mAZ, mEL, tAZ, tEL, tX, tY, tZ, sX, sY, sZ, LT, LG;
  62.         int TZN, Mth, Day, Hr, Mn;
  63.  
  64.         //User input. Note: For brevity, no error checks on user inputs
  65.         printf("Use negative numbers for directions opposite to those shown.\n\n");    
  66.         printf("Observer's latitude (degrees North): ");
  67.         scanf("%lf", &LT);
  68.         LT = deg2rad(LT);
  69.        
  70.         printf("Observer's longitude (degrees East): ");
  71.         scanf("%lf", &LG);
  72.         LG = deg2rad(LG);
  73.        
  74.         printf("\nFor target direction of light reflected from mirror:\n");
  75.         printf("Azimuth of target direction (degrees): ");
  76.         scanf("%lf", &tAZ);
  77.         tAZ = deg2rad(tAZ);
  78.  
  79.         printf("Elevation of target direction (degrees): ");
  80.         scanf("%lf", &tEL);
  81.         tEL = deg2rad(tEL);
  82.  
  83.         printf("\nTime Zone (+/- hours from GMT/UT): ");
  84.         scanf("%d", &TZN);
  85.  
  86.         printf("Month: ");
  87.         scanf("%d", &Mth);
  88.  
  89.         printf("Day: ");
  90.         scanf("%d", &Day);
  91.  
  92.         printf("Hour (24-hr format): ");
  93.         scanf("%d", &Hr);
  94.  
  95.         printf("Minutes: ");
  96.         scanf("%d", &Mn);
  97.  
  98.        
  99.         CL = PI / 2 - LT; // co-latitude
  100.         D = int(30.6 * ((Mth + 9) % 12) + 58.5 + Day) % 365; // day of year (D = 0 on Jan 1)
  101.         A = W * D + SN; // orbit angle since solstice at mean speed
  102.         B = A + E2 * sin(A - SP); // angle with correction for eccentricity
  103.         C = (A - atan(tan(B) / CT)) / PI;
  104.         SL = PI * (C - floor(C + .5)); // solar longitude relative to mean position
  105.         C = ST * cos(B);
  106.         DC = atan(C / sqrt(1 - C * C)); // solar declination (latitude)
  107.         LD = (Hr - TZN + Mn / 60.0) * WR + SL + LG; // longitude difference
  108.  
  109.         P2C(LD, DC, &sX, &sY, &sZ); // polar axis (perpend'r to azimuth plane)
  110.         C2P(sY, sZ, sX, &sAZ, &sEL); // horizontal axis
  111.         P2C(sAZ + CL, sEL, &sY, &sZ, &sX); // rotate by co-latitude
  112.        
  113.         // Sun's position
  114.         C2P(sX, sY, sZ, &sAZ, &sEL); // vertical axis
  115.  
  116.         printf("\nSun Azimuth:          %.1f degrees\n",rad2deg(sAZ));
  117.         printf("Sun Elevation:        %.1f degrees\n\n",rad2deg(sEL));
  118.        
  119.         // Mirror aim direction
  120.         P2C(tAZ, tEL, &tX, &tY, &tZ);
  121.         C2P(sX + tX, sY + tY, sZ + tZ, &mAZ, &mEL);
  122.         printf("Mirror aim direction (perpendicular to surface): \n");
  123.         printf("Mirror Azimuth:       %.1f degrees\n",rad2deg(mAZ));
  124.         printf("Mirror Elevation:     %.1f degrees\n\n",rad2deg(mEL));
  125.        
  126.         return 0;              
  127. }