Horizontal sundial calculations

From Rosetta Code
Revision as of 04:31, 24 June 2010 by rosettacode>NevilleDNZ (→‎[[Horizontal_sundial_calculations#ALGOL 68]]: Task: Scientific calculations example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Horizontal sundial calculations
You are encouraged to solve this task according to the task description, using any language you may know.

A program that calculates the hour, sun hour angle, dial hour line angle from 6am to 6pm for an operator entered location.

As the example the user is prompted for a location and inputs the latitude and longitude 4°57′S 150°30′W (4.95°S 150.5°W) of Jules Verne's Lincoln Island, aka Ernest Legouve Reef). With a legal meridian of 150°W.

Wikipedia: A sundial is a device that measures time by the position of the Sun. In common designs such as the horizontal sundial, the sun casts a shadow from its style (also called its Gnomon, a thin rod or a sharp, straight edge) onto a flat surface marked with lines indicating the hours of the day. As the sun moves across the sky, the shadow-edge progressively aligns with different hour-lines on the plate. Such designs rely on the style being aligned with the axis of the Earth's rotation. Hence, if such a sundial is to tell the correct time, the style must point towards true north (not the north or south magnetic pole) and the style's angle with horizontal must equal the sundial's geographical latitude.

ALGOL 68

Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8-8d

Example extracted - with permission for a GPL - from Simon Wheaton-Smith's Illustrating Time's Shadow web page. <lang algol68>BEGIN

 print  ( (new line, new line) ) ;
 print  ( ("*------------ Horizontal Dial ------------*", new line) ); 
 print  ( ("www.illustratingshadows.com       hdial.a68", new line) ); 
 print  ( ("Algol 68 from:", new line) ); 
 print  ( ("         www.xs4all.nl/~jmvdveer/algol.html", new line) ); 
 print  ( ("*----------- Algol 68 June 2009 ----------*", new line) );
 print  ( (new line, new line) ) ; 
 REAL lat, slat;
 print  ( "Enter latitude       " );
 read   ( lat ) ;
 slat := sin(lat*2*3.1416/360) ;
 print ( ("sin latitude:        ", float(slat,8,2,1), new line ) );
 REAL lng ;
 print  ( "Enter longitude      " );
 read (lng);
 REAL ref ;
 print  ( "Enter legal meridian " );
 read (ref);
 print ( ("diff long:           ", (lng - ref), new line ) );
 # test data: 0 8.25 17.35 28.41 43.14 63.65 90 for l=32.75 lng=ref #
 print ( (new line, new line) ) ;
 print ((  "e-2 means divide by 100, e-1 means divide by 10, e+0 means asis"));
 print ( (new line, new line) ) ;
 print ( ("Hour, sun hour angle, dial hour line angle from am to pm", new line ) ) ;
 FOR h FROM -6 TO 6
 DO 
    REAL hra , hla ;             # define hour angle and hour line angle #
    hra := 15 * h  ;             # hour angle is 15 times the hour #
    hra := hra - (lng - ref);    # but correct for longitude difference #
    hla := atan ( slat * tan(hra*2*3.1416/360) ) * 360 / ( 2*3.1416) ;
    # page 132 of a68gdoc.pdf documentationfile #
    print ( ("HR=",h, "  HRA=",float(hra,8,2,1), "  HLA=",float(hla,8,2,1), new line) )  
 OD

END</lang> Output:

Enter latitude       => -4.95
Enter longitude      => -150.5
Enter legal meridian => -150  

    sine of latitude:   -86.3e-3
    diff longitude:     -.500

Hour, sun hour angle, dial hour line angle from 6am to 6pm
HR= -6; HRA= -89.500; HLA= +84.225
HR= -5; HRA= -74.500; HLA= +17.283
HR= -4; HRA= -59.500; HLA=  +8.334
HR= -3; HRA= -44.500; HLA=  +4.847
HR= -2; HRA= -29.500; HLA=  +2.795
HR= -1; HRA= -14.500; HLA=  +1.278
HR= +0; HRA=  +0.500; HLA=  -0.043
HR= +1; HRA= +15.500; HLA=  -1.371
HR= +2; HRA= +30.500; HLA=  -2.910
HR= +3; HRA= +45.500; HLA=  -5.018
HR= +4; HRA= +60.500; HLA=  -8.671
HR= +5; HRA= +75.500; HLA= -18.451
HR= +6; HRA= +90.500; HLA= +84.225