Hi all, this is my first post.
A couple days ago, in preparation for a math final, I attempted to code a program which would allow me to solve triangles (law of cosines, sines, etc.). Now that the test is over, I am now interested in getting it working.
#This is TRIANGLE SOLVER!!!
#solves triangles, in SSS, ASA, SSA, AAS, SAS
#Written by Jared Miller, 2013, AAST, June 1 2010
import math
#functions
def askyesno():
if input("\nAgain?")==("y" or "Y" or "yes" or "Yes" or "YES"):
return True
else:
return False
#math constants
PI=math.pi
#math functions
sq=lambda x: math.sqrt(x)
p2=lambda x: x**2
#trig functions
sin=lambda x: math.sin(rad(x))
cos=lambda x: math.cos(rad(x))
#trig inverses
asin=lambda x: deg(math.asin(x))
acos=lambda x: deg(math.acos(x))
#trig measurements
deg=lambda x: math.degrees(x)
rad=lambda x: math.radians(x)
#laws of cosines and sines
#law of cosines, for angle and side
def lawcosside(a,b,C):
"""Law of cosines, solving side C"""
return sq(p2(a)+p2(b)-2*a*b*cos(C))
def lawcosangle(a,b,c):
"""Law of cosines, solving for angle C"""
return acos((p2(a)+p2(b)-p2(c))/(2*a*b))
#law of sines, for missing angle and side:
def lawsinside(A,a,B):
"""Law of sines, solving for side b"""
return (a*sin(B))/sin(A)
def lawsinangle(a,A,b):
"""Law of sines, solving for angle B"""
return asin((b*sin(A))/a)
#Solving triangles
#Single solution, regular cases
def SSS(a,b,c):
"""Finds angles A, B, C"""
A=lawcosangle(b,c,a)
B=lawcosangle(c,a,b)
C=180-A-B
assert not A+B+C>182
return {"sides":(a,b,c),"angles":(A,B,C)}
def ASA(A,c,B):
"""Finds sides a, b; and angle C"""
C=180-A-B
a=lawsinside(C,c,A)
b=lawsinside(C,c,B)
assert not A+B+C>182
return {"sides":(a,b,c),"angles":(A,B,C)}
def SAS(a,B,c):
"""Finds side b and angles A and C"""
b=lawcosside(a,c,B)
A=lawsinangle(b,B,a)
C=180-A-B
assert not A+B+C>182
return {"sides":(a,b,c),"angles":(A,B,C)}
def AAS(A,B,a):
"""Finds sides b, c; and angle C"""
b=lawsinside(A,a,B)
C=180-A-B
c=lawsinside(A,a,C)
assert not A+B+C>182
return {"sides":(a,b,c),"angles":(A,B,C)}
#Holy...multiple possible triangles
def SSA(c,a,A):
"""Returns 0,1,or 2 triangles, depending upon the case"""
#height declaration
A=rad(A)
h=c*sin(A)
#case 1:
#h>a
#0 triangles
if h>a:
return []
#case 2:
#h=a
#1 triangle
elif h==a:
C=90
B=180-A-C
b=lawsinside(A,a,B)
assert not A+B+C>182
return [{"sides":(a,b,c),"angles":(A,B,C)}]
#case 3:
#h<a<c
#2 triangles
elif h<a and a<c:
C1a=lawsinangle(a,A,c)
C2a=180-C1a
if C1a>C2a:
C1,C2=C1a,C2a
else:
C1,C2=C2a,C1a
B1=180-A-C1
B2=180-A-C2
assert not A1+B1+C1>182
assert not A2+B2+C2>182
b1=lawsinside(A,a,B1)
b2=lawsinside(A,a,B2)
return [{"sides triangle 1":(a,b1,c),"angles traingle 1":(A,B1,C1)}, {"sides triangle 2":(a,b2,c),"angles triangle 2":(A,B2,C2)}]
#case 4:
#a>=c
#1 triangle
else:
C=lawsinangle(a,A,c)
B=180-A-C
b=lawsinside(A,a,B)
assert not A+B+C>182
return [{"sides":(a,b,c),"angles":(A,B,C)}]
#main program
def main():
"""Start the program"""
again=True
print("""Welcome to the Triangle Solver!
Here, you will be able to solve a triangle in SSS, ASA, SSA, AAS, SAS forms.
Type in a form, and press enter to begin.
""")
while again:
type=input("Form:\t")
#SSS
if type=="SSS":
print("\nYou picked SSS. Type in each number, seperated by a space\n")
nums=input("Numbers:\t").split()
nums=[float(eval(i)) for i in nums]
resultdict=SSS(nums[0],nums[1],nums[2])
resultdict["angles"]=[deg(ang) for ang in resultdict["angles"]]
print("\nsides:\t ", resultdict["sides"], " \nangles:\t ", resultdict["angles"])
#ASA
elif type=="ASA":
print("\nYou picked ASA. Type in each number, seperated by a space\n")
nums=input("Numbers:\t").split()
nums=[float(eval(i)) for i in nums]
resultdict=ASA(nums[0],nums[1],nums[2])
resultdict["angles"]=[deg(ang) for ang in resultdict["angles"]]
print("\nsides:\t ", resultdict["sides"], " \nangles:\t ", resultdict["angles"])
#SSA
elif type=="SSA":
print("\nYou picked SSA. Type in each number, seperated by a space\n")
nums=input("Numbers:\t").split()
nums=[float(eval(i)) for i in nums]
resultlist=SSA(nums[0],nums[1],nums[2])
if len(resultlist)==0:
print("\nThere are no triangles that fit these specifications")
elif len(resultlist)==1:
resultdict=resultlist[0]
resultdict["angles"]=[deg(ang) for ang in resultdict["angles"]]
print("\nsides:\t ", resultdict["sides"], " \nangles:\t ", resultdict["angles"])
else:
resultdict1=resultlist[0]
resultdict1["angles"]=[deg(ang) for ang in resultdict1["angles"]]
resultdict2=resultlist[1]
resultdict2["angles"]=[deg(ang) for ang in resultdict2["angles"]]
print("\nsides:\t ", resultdict1["sides"], " \nangles:\t ", resultdict1["angles"])
print("sides:\t ", resultdict2["sides"], " \nangles:\t ", resultdict2["angles"])
#AAS
elif type=="AAS":
print("\nYou picked AAS. Type in each number, seperated by a space\n")
nums=input("Numbers:\t").split()
nums=[float(eval(i)) for i in nums]
resultdict=AAS(nums[0],nums[1],nums[2])
resultdict["angles"]=[deg(ang) for ang in resultdict["angles"]]
print("\nsides:\t ", resultdict["sides"], " \nangles:\t ", resultdict["angles"])
#SAS
elif type=="SAS":
print("\nYou picked SAS. Type in each number, seperated by a space\n")
nums=input("Numbers:\t").split()
nums=[float(eval(i)) for i in nums]
resultdict=SAS(nums[0],nums[1],nums[2])
resultdict["angles"]=[deg(ang) for ang in resultdict["angles"]]
print("\nsides:\t ", resultdict["sides"], " \nangles:\t ", resultdict["angles"])
#Catch-all for bugs
else:
print("I could not recognize this request.\n")
again=askyesno()
#start of program
main()
When run, the angle measurements return outrageous values in the high 2-4 thousands, and the assert statements are failing. I'm pretty sure there is a more efficient method for parsing and synthesizing the input, but the method I have now works.
Thanks in advance,
Jared