How to resolve the algorithm Horizontal sundial calculations step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Horizontal sundial calculations step by step in the D programming language

Table of Contents

Problem Statement

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

For 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. (Note: the "meridian" is approximately the same concept as the "longitude" - the distinction is that the meridian is used to determine when it is "noon" for official purposes. This will typically be slightly different from when the sun appears at its highest location, because of the structure of time zones. For most, but not all, time zones (hour wide zones with hour zero centred on Greenwich), the legal meridian will be an even multiple of 15 degrees.) 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 (also called the dial face or dial plate). 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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Horizontal sundial calculations step by step in the D programming language

Source code in the d programming language

import std.stdio, std.math, std.conv, std.string;

double radians(in double x) pure nothrow { return x * (PI / 180); }
double degrees(in double x) pure nothrow { return x / (PI / 180); }

T input(T)(in string msg) {
    msg.write;
    return readln.strip.to!T;
}

void main() {
    immutable lat = input!double("Enter latitude       => ");
    immutable lng = input!double("Enter longitude      => ");
    immutable lme = input!double("Enter legal meridian => ");
    writeln;

    double slat = lat.radians.sin;
    writefln("    sine of latitude:   %.3f", slat);
    writefln("    diff longitude:     %.3f", lng - lme);
    writeln;
    "Hour, sun hour angle, dial hour line angle from 6am to 6pm".writeln;

    foreach (immutable h; -6 .. 7) {
        immutable double hra = 15 * h - (lng - lme);
        immutable double hla = atan(slat * hra.radians.tan).degrees;
        writefln("HR=%3d; HRA=%7.3f; HLA=%7.3f", h, hra, hla);
    }
}


  

You may also check:How to resolve the algorithm Legendre prime counting function step by step in the Erlang programming language
You may also check:How to resolve the algorithm Topological sort step by step in the Ruby programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the REXX programming language
You may also check:How to resolve the algorithm Count the coins step by step in the Ruby programming language
You may also check:How to resolve the algorithm Ludic numbers step by step in the D programming language