Skip to content

Instantly share code, notes, and snippets.

@0xDaksh
Created Apr 21, 2018
Embed
What would you like to do?
Khan Academy Precalculus Vector homework with Python!
import math
def quadrant(vector):
x = vector[0]
y = vector[1]
if x > 0 and y > 0:
return 1
if x < 0 and y > 0:
return 2
if x < 0 and y < 0:
return 3
if x > 0 and y < 0:
return 4
def calculateMagnitude(vector):
x = vector[0]
y = vector[1]
return round(math.sqrt(x**2 + y**2), 2)
def findCoordinates(magnitude, theta):
theta = math.radians(theta)
x = magnitude * math.cos(theta)
y = magnitude * math.sin(theta)
return [round(x, 2), round(y, 2)]
def findUnitVector(vector):
x = vector[0]
y = vector[1]
magnitude = calculateMagnitude(vector)
if magnitude > 1:
return [round(x/magnitude, 2), round(y/magnitude, 2)]
else:
return magnitude
def findTheta(vector):
x = vector[0]
y = vector[1]
qdr = quadrant(vector)
theta = round(math.degrees(math.atan(y / x)), 2)
if qdr == 1:
return theta
elif qdr == 2 or qdr == 3:
return theta + 180
else:
return theta + 360
def addVectors(*args):
final = [0, 0]
for vector in args:
final[0] += vector[0]
final[1] += vector[1]
return [round(final[0], 2), round(final[1], 2)]
# for example
firstVector = findCoordinates(8, 140)
secondVector = findCoordinates(4, 40)
finalVector = addVectors(firstVector, secondVector)
print(finalVector)
print(calculateMagnitude(finalVector))
print(findTheta(finalVector))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment