Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The purpose of this question is to write a complete python program that creates

ID: 3910172 • Letter: T

Question

The purpose of this question is to write a complete python program that creates and displays a table of polar and Cartesian coordinates for a spiral. An example of a spiral is shown below. Write a function that begins with the following header. def computeCoordinates(intervals, maxAngle, scale): where intervals is the number of equally spaced angles, maxAngle is the maximum angle for the spiral, and scale is the scale factor for the spiral. The larger maxAngle is, the more revolutions in the spiral. The larger scale is, the farther apart are the lines of the spiral. Create a list of equally spaced angles from 0 to maxAngle radians. Create a list of radii. Use the formula radius = scale × angle to compute each radius from the corresponding angle. Add the radius to the list of radii. The lists of angles and radii are the polar coordinates of the points in the spiral. Using the polar coordinates, create lists of the corresponding X and Y coordinates (Cartesian coordinates) of the points on the spiral. Use the formula x = radius × cos(angle) to compute each X coordinate from the corresponding radius and angle. Add each X coordinate to the list of X coordinates. Use the formula y = radius × sin(angle) to compute each Y coordinate from the corresponding radius and angle. Add each Y coordinate to the list of Y coordinates. Return the lists of angles, radii, X coordinates and Y coordinates in that order. Write a function that begins with the following header. def displayCoordinates(angles, radii, xCoords, yCoords): This function is given the lists of angles, radii, X coordinates and Y coordinates of the points in the spiral. Display the angle, radius, X coordinate and Y coordinate of each point in the spiral with the coordinates for one point per line of output. See the sample run of the program for the format of the output. Display each angle and radius to 4 decimal places in 10 character positions. Display each X and Y coordinate to 6 decimal places in 14 character positions. Separate each value in a line of output from the next value by one space. Display the two lines of headings so that the text is positioned as shown in the sample run of the program. Write a function that begins with the following header. def main(): Input the number of intervals as an integer Loop as long as the number of intervals is greater than zero: • input the maximum angle as a float, if the angle is less than or equal to zero display an error message that includes the value of the angle as shown in the sample run of the program, otherwise continue • input the scale factor as a float, if the scale factor is less than or equal to zero display an error message that includes the value of the scale factor as shown in the sample run of the program, otherwise continue • call computeCoordinates to get the lists of angles, radii, X coordinates and Y coordinates • call displayCoordinates to display the lists of angles, radii, X coordinates and Y coordinates • input the number of intervals as an integer Call displayTerminationMessage to display the termination message. The main program (not to be confused with function named main) should contain the function definitions, any import statements needed by the functions and a call to the function named main.

Explanation / Answer

Note: There were no sample output in the question as given so, I have assumed the error messages and displaying part. Please change the message strings as per your need.

CODE

import numpy as np

import math

def displayTerminationMessage():

    """

    Displays the termination message.

    """

    print('Process has been terminated.')

def computeCoordinates(intervals, maxAngle, scale):

    """

    Returns teh calculated values for angles, radii, x coordinates and y coordinates.

    """

    # calculating the equally spaced angles

    angles = np.linspace(0, maxAngle, intervals).tolist()

    # calculating the radius using scale x angle

    radii = [scale * angle for angle in angles]

    # calculating x coordinate using radius x cos(angle)

    x_coords = [radius * math.cos(angle) for radius, angle in zip(radii, angles)]

    # calculating y coordinate using radius x sin(angle)

    y_coords = [radius * math.sin(angle) for radius, angle in zip(radii, angles)]

    # returning all the calculated lists

    return angles, radii, x_coords, y_coords

def displayCoordinates(angles, radii, xCoords, yCoords):

    """

    Printings the angle, radii, x coordinates and y coordinates for each point calculated.

    """

    for i, (angle, radius, x, y) in enumerate(zip(angles, radii, xCoords, yCoords)):

        print('Point %d: Angle: %10.4f Radius: %10.4f X: %14.6f Y: %14.6f' %(i, angle, radius, x, y))

def main():

    # Taking input for number of intervals

    number_of_intervals = input("Enter the number of intervals: ")

    # Looping while number of intervals is greater than 0

    while number_of_intervals > 0:

        # Taking input for maximum angle

        max_angle = float(input("Enter the maximum angle: "))

        if max_angle <= 0:

            # if angle is less than or equal to 0, we again ask for the max angle

            print('Error: Value of maximum angle: {} should be greater than 0.'.format(max_angle))

            continue

        # Taking input for scale factor

        scale_factor = float(input("Enter the scale factor: "))

        if scale_factor <= 0:

            # if scale factor is less than or equal to 0, we again ask for the max angle

            print('Error: Value of scale factor: {} should be greater than 0.'.format(scale_factor))

            continue

        # Calling computeCoordinates

        angles, radii, xCoords, yCoords = computeCoordinates(number_of_intervals, max_angle, scale_factor)

        # Calling displayCoordinates

        displayCoordinates(angles, radii, xCoords, yCoords)

        # Again ask for number of intervals

        number_of_intervals = input("Enter the number of intervals: ")

    # Displaying the termination message.

    displayTerminationMessage()

if __name__ == '__main__':

    main()

OUTPUT

Enter the number of intervals: 10

Enter the maximum angle: 250

Enter the scale factor: 7

Point 0: Angle:     0.0000 Radius:     0.0000 X:       0.000000 Y:       0.000000

Point 1: Angle:    27.7778 Radius:   194.4444 X:    -170.961087 Y:      92.633411

Point 2: Angle:    55.5556 Radius:   388.8889 X:     212.366513 Y:    -325.783720

Point 3: Angle:    83.3333 Radius:   583.3333 X:     -47.272778 Y:     581.414708

Point 4: Angle:   111.1111 Radius:   777.7778 X:    -313.896836 Y:    -711.622827

Point 5: Angle:   138.8889 Radius:   972.2222 X:     768.755533 Y:     595.173068

Point 6: Angle:   166.6667 Radius: 1166.6667 X:   -1151.342903 Y:    -188.469178

Point 7: Angle:   194.4444 Radius: 1361.1111 X:    1285.760260 Y:    -446.591548

Point 8: Angle:   222.2222 Radius: 1555.5556 X:   -1048.823547 Y:    1148.791649

Point 9: Angle:   250.0000 Radius: 1750.0000 X:     421.729534 Y:   -1698.424034

Enter the number of intervals: 0

Process has been terminated.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote