#!/usr/local/bin/python3 # filename: port_of_second_program.py # rev. date/time: 03-28-13-0825 hrs. # by Mr. Cantlin. # Line 1 above is traditionally called a "shebang" line in Python. # The #! symbols are the reason for the name. # The # does not start a comment line in this first line. # There should be no white space in the shebang line. # The shebang tells the OS where to find the Python files. # It tells Linux to look at the $PATH environmental variable # for the location of the Python.exe program. # It may not be needed if your OS is Windows. # It is not needed when you are running programs from IDLE. # **IDLE is set to reformat paragraphs after 70 characters per line**. # Python really thinks indents are important. # Indented modules must be tabbed or indented 4 spaces. # Thou shall not indent three, or two, except when passing to 4. # Clearly derived from the Holy Hand Grenade :-) # **************************************************** # Description of Program: # Write a program that will add two or more vectors # when the vectors are given in polar form. # Reference: Page 236 Problem 76. # This Python program is a port of a Javascript program # that I wrote earlier as a demo for Dalton - Mr. Cantlin. # Spring Break 2013. # **************************************************** # **************************************************** # A pair of vectors to use for testing. See Ex. 3 Pg. 230 # Vector 1 = (13.8, 40.2 degrees) # Vector 2 = (20.9, 164.6 degrees) # Resultant Vector - Rectangular Form = (-9.6, 14.5) # Resultant Vector - Polar Form = (17.4, 123.6 degrees) # All text references are from "Trigonometry with Applications" # By Wesner and Mahler, 3rd Ed. 1994, ISBN 0-697-12292-1 # **************************************************** # **************************************************** # Define and initialize variables. # Use 0.0 to define as Floating Point. No EOL ";" of course. # The "var" keyword is not used either. vector1_magnitude = 0.0 vector1_angle = 0.0 vector2_magnitude = 0.0 vector2_angle = 0.0 vector1_x = 0.0 vector1_y = 0.0 vector2_x = 0.0 vector2_y = 0.0 vector_sum_x = 0.0 vector_sum_y = 0.0 vector_sum_magnitude = 0.0 vector_sum_angle = 0.0 # **************************************************** # **************************************************** # Prompt for user input. # The input is converted to floating point in the same step. # Default ending to print() is end='\n' i.e. one new line. # Use end='\n\n' for two new line or end=" " for no new line. print("Add Two Vectors in Polar Form", end='\n\n') vector1_magnitude = float(input("Enter Vector 1 Magnitude: ")) vector1_angle = float(input("Enter Vector 1 Angle (Degrees): ")) vector2_magnitude = float(input("Enter Vector 2 Magnitude: ")) vector2_angle = float(input("Enter Vector 2 Angle (Degrees): ")) # **************************************************** # **************************************************** # Find the x and y components of the input vectors # Since the math function works only in radians # convert degrees to radians by multiplying by pi/180 # Python Gotcha, before you use any math() function, # you have to import the math module :-(. import math # import the math function before first use of it. vector1_x = vector1_magnitude * math.cos(vector1_angle*math.pi/180) vector2_x = vector2_magnitude * math.cos(vector2_angle*math.pi/180) vector1_y = vector1_magnitude * math.sin(vector1_angle*math.pi/180) vector2_y = vector2_magnitude * math.sin(vector2_angle*math.pi/180) # **************************************************** # **************************************************** # Add the x components and y components to get the # sum of the vectors in rectangular form. vector_sum_x = vector1_x + vector2_x vector_sum_y = vector1_y + vector2_y # **************************************************** # **************************************************** # Convert the vector sum from rectangular to polar form. # Use the atan2(y,x) function to ensure the angle returned # is in the proper Quadrant. Warning-two arguments: y is first! vector_sum_magnitude = math.sqrt(math.pow(vector_sum_x,2)+math.pow(vector_sum_y,2)) vector_sum_angle = math.atan2(vector_sum_y,vector_sum_x) vector_sum_angle = vector_sum_angle * (180/math.pi) # **************************************************** # **************************************************** # Display Results print("", end='\n\n') # skip two lines print("*****************************") print("Results: ") print("Vector 1 Magnitude: ",vector1_magnitude) print("Vector 1 Angle (Degrees): ",vector1_angle, end='\n') print("Vector 2 Magnitude: ",vector2_magnitude) print("Vector 2 Angle (Degrees): ",vector2_angle, end='\n\n') print("Vector Sum in Rectangular Form: ","(",vector_sum_x,",",vector_sum_y,")",end='\n\n') print("Vector Sum in Polar Form: ","(",vector_sum_magnitude,",",vector_sum_angle,"degrees",")",end='\n\n') print("*****************************") # ****************************************************