Skip to content

Instantly share code, notes, and snippets.

@john212
Created January 29, 2016 22:03
Show Gist options
  • Save john212/f237a42729161dd02b1f to your computer and use it in GitHub Desktop.
Save john212/f237a42729161dd02b1f to your computer and use it in GitHub Desktop.
Python - Add Two Vectors
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Mr. C.'s Add Two Vectors Program</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<meta name="description" content="A program to add two vectors in polar form.">
<meta name="keywords" content="Python, vectors, polar form, Mth113, Mr. C.">
<meta name="Author" content="John Cantlin">
</head>
<body>
<!-- This program adds two vectors that are given in polar form. The vectors are passed from this web page to a Python script that does the math and the reults are provided in a web page generated by the Python script.-->
<!-- filename: port_of_second_program.py
rev. date/time: 03-30-13-0834 hrs.
by Mr. Cantlin. -->
<!-- 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 -->
<h2>Mr. C.'s Add Two Vectors Program</h2>
<p>The purpose of this program is to add two vectors that are in polar form.</p>
<p>To add two vectors in polar form, they are each converted to rectangular form and the x and y components of the vectors are then added. This resultant vector in rectangular form is then converted back to polar form.</p>
<p>This problem is number 76 on Page 236 of your Mth113 textbook: "Trigonometry with Applications" 3rd Ed. 1994 by Wesner and Mahler. ISBN 0-697-12292-1.</p>
<p>This program is a combination of HTML and Python. The HTML (this page) is used to collect the user input. This input is passed to a Python script which does the math and outputs the results in HTML so you can see it in your browser. The Python script is written in Python ver. 3.3.0. The Python script is passed to the Python interpreter which is installed on the server that BulldogMath runs on. In general, to run Python this way, you need to have access to a server that has the same version of Python on it that you are using. A more limited way of using Python, much easier for writing and debugging the script, is to install Python on your computer. This allows you to output the program results to the console (aka terminal, aka your screen).</p>
<p>Python is a cool, powerful, and very popular programming language. It is named after Monty Python, not the snake. 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. This rule was clearly derived from the Holy Hand Grenade :-)</p>
<p>A pair of vectors to use for testing:<br />
Vector 1 = (13.8, 40.2 degrees)<br />
Vector 2 = (20.9, 164.6 degrees)<br />
Resultant Vector - Rectangular Form = (-9.6, 14.5)<br />
<b>Resultant Vector - Polar Form = (17.4, 123.6 degrees)</b></p>
<font color="#8000FF"><h3>Enter Your Two Vectors in Polar Form Here</h3></font>
<p><i>Note: your input numbers are limited to 20 characters (18 numbers plus the sign, if negative, and a decimal point). Enter only numbers and the decimal point if needed, and the negative sign if needed. The magnitude of the vector has no units, the angle is in degrees.</i></p>
<hr/>
<form name="inputvectors" action="../cgi-bin/port_of_second_program.cgi" method="post">
Vector 1 Magnitude:&nbsp; <input type="text" name="vector1_magnitude_s" maxlength="20" value="0.0"><br/>
Vector 1 Angle:&nbsp; <input type="text" name="vector1_angle_s" maxlength="20" value="0.0">&nbsp;degrees<br/><br/>
Vector 2 Magnitude:&nbsp; <input type="text" name="vector2_magnitude_s" maxlength="20" value="0.0"><br/>
Vector 2 Angle:&nbsp; <input type="text" name="vector2_angle_s" maxlength="20" value="0.0">&nbsp;degrees<br/><br/>
<input value="Submit" type="submit">
</form>
<hr/>
</body></html>
#!/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("<html><head>")
print("<title>Mr. C. Add Two Vectors</title>")
print ("</head><body>")
# **************************************************************
# **************************************************************
# 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("<H2>Results: Adding Two Vectors</H2>")
print("<p>Vector 1 Magnitude:", vector1_magnitude,"<br/>")
print("Vector 1 Angle:", vector1_angle, " degrees")
print("<p>Vector 2 Magnitude:", vector2_magnitude,"<br/>")
print("Vector 2 Angle:", vector2_angle, " degrees")
print("<p>Vector Sum in Rectangular Form: ","(",vector_sum_x,",",vector_sum_y,")")
print("<p><b>Vector Sum in Polar Form: ","(",vector_sum_magnitude,",",vector_sum_angle,"degrees",")","</b>")
# **************************************************************
# **************************************************************
# Section 3: End Main HTML Output i.e. html framework
# End of the HTML section.
print ("</body></html>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment