#!/usr/bin/python3 # -*- coding: UTF-8 -*- # filename: port_of_second_program.py # rev. date/time: 03-30-13-0834 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. # /usr/bin/python3 is where Python is installed # on the Bulldogmath server. # It is not needed when you are running programs from IDLE. # **************************************************** # A pair of vectors below 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 # **************************************************** # ************************************************************** # Install CGI support and math support. # The Python script is run as a CGI program. # CGI=Common Gateway Interface, used for many scripting languages. # Most Linux servers have a /cgi-bin/ folder for these programs. # This .py program will be changed to .cgi program to run. # The Python interpreter on the server is smart enough to # still know it is a Python program. Prob. from the shebang line. # Python needs to install several CGI realted modules. # ************************************************************** # ************************************************************** # Intall modules Python needs for this program. Don't forget "math". import math import cgi import cgitb cgitb.enable() # enable debugging # ************************************************************** # ************************************************************** # Define and initialize all variables # Use 0.0 to define as Floating Point. "anything" to define as strings. # The input received from the hlml form will be as strings (_s added to these.. vector1_magnitude = 0.0 vector1_angle = 0.0 vector2_magnitude = 0.0 vector2_angle = 0.0 vector1_magnitude_s = "input_string" vector1_angle_s = "input_string" vector2_magnitude_s = "input_string" vector2_angle_s = "input_string" 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 # ************************************************************** # ************************************************************** # Print to HTML requires this setup - it has THREE sections. # Section 1: Two Lines Separate this from Section 2 with blank line print("Content-Type: text/html") # HTML is following print() # blank line, end of headers # ************************************************************** # ************************************************************** # Section 2: Start Main HTML Output i.e. html framework print("") print("Mr. C. Add Two Vectors") print ("") # ************************************************************** # ************************************************************** # Read the Form Input. Convert text input to decimal form. # Use cgi.escape to stip any html characters = improved security. form = cgi.FieldStorage() vector1_magnitude_s = cgi.escape(form["vector1_magnitude_s"].value, quote=True) vector1_angle_s = cgi.escape(form["vector1_angle_s"].value, quote=True) vector2_magnitude_s = cgi.escape(form["vector2_magnitude_s"].value, quote=True) vector2_angle_s = cgi.escape(form["vector2_angle_s"].value, quote=True) vector1_magnitude = float(vector1_magnitude_s) vector1_angle = float(vector1_angle_s) vector2_magnitude = float(vector2_magnitude_s) vector2_angle = float(vector2_angle_s) # ************************************************************** # **************************************************** # 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 :-(. See above, it # seems like good practice to import all the modules in one place. 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("

Results: Adding Two Vectors

") print("

Vector 1 Magnitude:", vector1_magnitude,"
") print("Vector 1 Angle:", vector1_angle, " degrees") print("

Vector 2 Magnitude:", vector2_magnitude,"
") print("Vector 2 Angle:", vector2_angle, " degrees") print("

Vector Sum in Rectangular Form: ","(",vector_sum_x,",",vector_sum_y,")") print("

Vector Sum in Polar Form: ","(",vector_sum_magnitude,",",vector_sum_angle,"degrees",")","") # ************************************************************** # ************************************************************** # Section 3: End Main HTML Output i.e. html framework # End of the HTML section. print ("")