Created
April 21, 2018 04:17
-
-
Save 0xDaksh/3d07e17ea2e9c91bb65798532e715116 to your computer and use it in GitHub Desktop.
Khan Academy Precalculus Vector homework with Python!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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