#! /usr/local/bin/python # -*- coding: latin-1 -*- # Trigonometry # # Brendan Gillatt # # Released under the MIT License: http://www.opensource.org/licenses/mit-license.php # import math # Here Be Dragons from now on ;] def sine(inv, opp=0, hyp=0, ang=0): opp = float(opp)# convert to float hyp = float(hyp) ang = float(ang) if inv == 'a': # if angle is required to be calculated ans = math.degrees(math.asin(opp/hyp)) # return inverse sine of opp divided by hyp, converted to degrees return ans elif inv == 's': # if a side is required to be calculated sinetab = math.sin(math.radians(ang)) # return sine of angle sinetab = float(sinetab) # convert to float if hyp != 0: # if hyp is empty ans = sinetab*hyp elif opp != 0: # if opp is empty ans = opp/sinetab else: print "Error, both sides are non-empty - you've already got your answer!" return ans def cosine(inv, adj=0, hyp=0, ang=0): adj = float(adj)# convert to float hyp = float(hyp) ang = float(ang) if inv == 'a': # if angle is required to be calculated ans = math.degrees(math.acos(adj/hyp)) # return inverse cosine of adj divided by hyp, converted to degrees return ans elif inv == 's': # if a side is required to be calculated cosinetab = math.cos(math.radians(ang)) # return cosine of angle cosinetab = float(cosinetab) # convert to float if hyp != 0: # if hyp is empty ans = cosinetab*hyp elif adj != 0: # if adj is empty ans = adj/cosinetab else: print "Error, both sides are non-zero - you've already got your answer!" return ans def tan(inv, opp=0, adj=0, ang=0): adj = float(adj)# convert to float opp = float(opp) ang = float(ang) if inv == 'a': # if angle is required to be calculated ans = math.degrees(math.atan(opp/adj)) # return inverse tan of opp divided by adj, converted to degrees return ans elif inv == 's': # if a side is required to be calculated tantab = math.tan(math.radians(ang)) # return cosine of angle if opp != 0: # if opp is empty ans = tantab*opp elif adj != 0: # if opp is empty ans = adj/tantab else: print "Error, both sides are non-empty - you've already got your answer!" return ans def sincahtoa(): as = '' # Working out an angle or a side length typ = '' # Are we using SOAH CAH or TOA while as != 'A' and as != 'S': # while an unintelligable entry made. as = raw_input('Work out Angle or a Side? A/S: ') # poll the user for an entry while typ != 'Sin' and typ != 'Cos' and typ != 'Tan' and typ != 'SOH' and typ != 'CAH' and typ != 'TOA': # while we don't know which sides to use from SOH CAH TOA typ = raw_input('Enter type [Sin, Cos, Tan] or [SOH, CAH, TOA]: ') # Poll the user for an entry if typ == 'Sin' or typ == 'SOH': # if we are calculating something that requires Sine. if as == 'A': # If we are working out the angle from the Opposite and Hypotinuse side lengths opp = float(raw_input('Enter Opposite: ')) # poll for side length hyp = float(raw_input('Enter Hypotinuse: ')) # poll for side length print 'Angle:', sine('a',opp,hyp,0), 'ø' # pretty print it including DOS degrees sign. else: # We are working either the length of the Opposite or Hypotinuse side. ang = float(raw_input('Enter Angle: ')) # poll for the angle known opp = float(raw_input('Enter Opposite, 0 for unknown: ')) # poll for Opposite hyp = float(raw_input('Enter Hypotinuse, 0 for unknown: ')) # pll for Hypotinuse print 'Side:', sine('s',opp,hyp,ang) elif typ == 'Cos' or typ == 'CAH': # Same as bove but for Cosine if as =='A': adj = float(raw_input('Enter Adjacent: ')) hyp = float(raw_input('Enter Hypotinuse: ')) print 'Angle:', cosine('a',adj,hyp,0), 'ø' else: ang = float(raw_input('Enter Angle: ')) adj = float(raw_input('Enter Adjacent, 0 for unknown: ')) hyp = float(raw_input('Enter Hypotinuse, 0 for unknown: ')) print 'Side:', cosine('s',adj,hyp,ang) elif typ == 'Tan' or typ == 'TOA': # Same as bove but for Tangent if as =='A': adj = float(raw_input('Enter Adjacent: ')) opp = float(raw_input('Enter Opposite: ')) print 'Angle:', tan('a',opp,adj,0), 'ø' else: ang = float(raw_input('Enter Angle: ')) opp = float(raw_input('Enter Opposite, 0 for unknown: ')) adj = float(raw_input('Enter Adjacent, 0 for unknown: ')) print 'Side:', tan('s',adj,opp,ang) def hypotinuse(s1, s2): print math.sqrt(s1**2+s2**2) def side(h1, s1): print math.sqrt(h1**2-s1**2) def pythag(): typ = '' while typ != 'H' and typ != 'S': typ = raw_input('Compute Hypotinuse or Side? H/S: ') if typ == 'H': s1 = float(raw_input('Enter first side of triangle: ')) s2 = float(raw_input('Enter second side of triangle: ')) hypotinuse(s1, s2) if typ == 'S': h1 = float(raw_input('Enter hypotinuse of triangle: ')) s1 = float(raw_input('Enter side of triangle: ')) side(h1, s1) print "\nBrendan's Trigonometry Calculator \nPlease note that it works to a maximum precision of 10 float point digits.\n" typ = '' while typ != 'P' and typ != 'S': typ = raw_input('Pythagoras or SOHCAHTOA? P/S: ') if typ == 'P': pythag() elif typ == 'S': sincahtoa()