2 Replies - 569 Views - Last Post: 07 March 2009 - 10:23 AM

#1 atik97 Reputation: 144
• Posts: 715
• Joined: 16-September 08

A function to find the area of triangle

Posted 20 January 2009 - 04:09 PM

Description: You can calculate the area of any triangle.
import math
def triangle(x,y,z):
sumOfSides=x+y+z
#angleX represents the angle between side y and side z
#The value of asine() is in radian
#The value of angle is calculated from angle/opposite_site ratio
angleX=math.asin((math.pi*x)/sumOfSides)
#Height of apex Y from side y
h=z*math.sin(angleX)
#area of triangle
area=(y*h)/2.0
print "Area of the triangle is %s"%area

Is This A Good Question/Topic? 0

Replies To: A function to find the area of triangle

#2 JackOfAllTrades Reputation: 6258
• Posts: 24,026
• Joined: 23-August 08

Re: A function to find the area of triangle

Posted 07 March 2009 - 07:03 AM

What are the conditions for this function to work? I'm way too far removed from trigonometry and geometry to remember, and searching is not helping. If I pass x=5, y=4, z=3 I get a math domain error:
File "C:/Python25/tests/coutdown", line 15, in
triangle(5,4,3)
File "C:/Python25/tests/coutdown", line 7, in triangle
angleX=math.asin((math.pi*x)/sumOfSides)
ValueError: math domain error

#3 atik97 Reputation: 144
• Posts: 715
• Joined: 16-September 08

Re: A function to find the area of triangle

Posted 07 March 2009 - 10:23 AM

Oh! I Had made a silly mistake in the logical like(angleX) in the earlier. That is fixed now and the function can find out area of any triangle. Sorry for mistake.
angleX=math.acos((pow(y,2)+pow(z,2)-pow(x,2))*3.14/(360*y*z))

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }