How to resolve the algorithm Horizontal sundial calculations step by step in the Scala programming language
How to resolve the algorithm Horizontal sundial calculations step by step in the Scala 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 Scala programming language
Source code in the scala programming language
import java.util.Scanner
import scala.math.{atan2, cos, sin, toDegrees, toRadians}
object Sundial extends App {
var lat, slat,lng, ref = .0
val sc = new Scanner(System.in)
print("Enter latitude: ")
lat = sc.nextDouble
print("Enter longitude: ")
lng = sc.nextDouble
print("Enter legal meridian: ")
ref = sc.nextDouble
println()
slat = Math.sin(Math.toRadians(lat))
println(f"sine of latitude: $slat%.3f")
println(f"diff longitude: ${lng - ref}%.3f\n")
println("Hour, sun hour angle, dial hour line angle from 06h00 to 18h00")
for (h <- -6 to 6) {
val hra = 15.0 * h - lng + ref
val hraRad = toRadians(hra)
val hla = toDegrees(atan2(Math.sin(hraRad) * sin(Math.toRadians(lat)), cos(hraRad)))
println(f"HR= $h%3d;\tHRA=$hra%7.3f;\tHLA= $hla%7.3f")
}
}
You may also check:How to resolve the algorithm Doubly-linked list/Element definition step by step in the Lang programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the Metafont programming language
You may also check:How to resolve the algorithm Maze generation step by step in the Rascal programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the Burlesque programming language
You may also check:How to resolve the algorithm Nested templated data step by step in the Phix programming language