Astronomy Hour of Code

Activity 13: Convert Right Ascension to Degrees

Instructions:

In order for us to give our virtual telescope movement commands in the computer, we have to convert the Right Ascension numbers into degrees. Since a full turn of a circle is 360 degrees we can translate the hours and minutes and seconds to degrees. We can construct a formula for this by converting the hours and minutes and seconds that the user input into the total number of hours using decimal values and then converting the total number of hours to degrees.

Let's start by creating a totalHours variable of type number (since we will need decimal places) and assign it the value contained in hours from the user input:

number totalHours = hours

Next, we know that there are 60 minutes in every hour, so we can convert minutes to hours by dividing the value contained in the variable minutes by 60 and adding it to totalHours. We will put parentheses around the minutes calculation to tell the computer to do that part first.

We can also use the variable totalHours twice in the same equation (on the left and right side of the equation) because the computer will use the original value in the computer's memory in the calculation on the right side of the equation first and then write the new value into memory on the left side of the equation after the calculation is complete. The next line in the conversion will look like this:

totalHours = totalHours + (minutes / 60)

We can finish our calculation for totalHours by converting seconds to hours and adding that to totalHours. Since there are 60 minutes in an hour and 60 seconds in a minute, there are 3600 seconds per hour, so we can make this calculation like this:

totalHours = totalHours + (seconds / 3600)

Now totalHours contains the total number of hours as a decimal value and we can convert that number to degrees. Since there are 360 degrees in a circle and 24 hours in a day, we can calculate the number of degrees per hour by dividing 360 by 24, which gives us 15. So if we multiply totalHours by 15, we have the number of degrees. We can write this in code like this:

number degrees = totalHours * 15

To output the answer, we can combine the value of degrees with a text string to make our answer in a nice format. We do that like this:

output "The number of degrees are: " + degrees

When you are ready, select the green Run Program button below the editor on the right (second in the tab order).

Exploration Challenge (Optional):

The code we wrote in this activity has more lines than we need to use. Try to re-write the code to put the formula on a single line without using the variable totalHours.

Next Tutorial

In the next tutorial, we will discuss Astronomy Hour of Code | Activity 0, which describes Accessible astronomy themed Hour of Code.