# public Khan /khan-exercises

### Subversion checkout URL

You can clone with HTTPS or Subversion.

Fetching contributors…

Cannot retrieve contributors at this time

file 60 lines (58 sloc) 3.433 kb
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59         Heron's formula

randomTriangleAngles.triangle()                    []                    []                    []                    randRange( 3, 7 ) + random()

What is the area of this triangle? ( Round to two decimal places )

init({                            range: [ [-1, 12 ], [ -7, 2.5 ] ]                        })                        var trA = new Triangle( [ 3, -3 ], ANGLES ,AREA, {} );                        SIDES = trA.niceSideLengths;                        trA.boxOut( [ [ [ -10, 2.3 ], [ 10, 2.3 ] ] ] , [ 0, -0.7 ] );                        trA.boxOut( [ [ [ -1, -10 ], [ -1, 10 ] ] ] , [ 0.7, 0 ] );                        trA.boxOut( [ [ [ 11.5, -10 ], [ 11.5, 10 ] ] ] , [ -0.7, 0 ] );                        trA.draw();                        trA.labels = { "sides" : [commafy(SIDES[0]), commafy(SIDES[1]), commafy(SIDES[2])] };                        trA.drawLabels();                        S = roundTo(2, ( ( SIDES[ 0 ] + SIDES[ 1 ] + SIDES[ 2 ] ) / 2 ));                        ANS = roundTo(2, sqrt(S *( S -SIDES[ 0 ] ) * ( S - SIDES[ 1 ] ) * ( S - SIDES[ 2 ] ) ));                        \$( "#ans" ).html( ANS ) ;

We know all sides of this triangle, so we can use Heron's formula to calculate the area.

Heron's formula states that the area of a triangle A=\sqrt{s(s-a)(s-b)(s-c)}

s = \dfrac{ a + b + c }{ 2 }

s = \dfrac{ commafy(SIDES[0]) + commafy(SIDES[1]) + commafy(SIDES[2]) }{ 2 }

s = \dfrac{ localeToFixed( SIDES[ 0 ] + SIDES[ 1 ] + SIDES[ 2 ], 1) }{ 2 }

s = commafy(S)

A = \sqrt{ commafy(S) \cdot ( commafy(S) - commafy(SIDES[0]) ) \cdot ( commafy(S) - commafy(SIDES[1]) ) \cdot ( commafy(S) - commafy(SIDES[2]) ) }

A = \sqrt{ commafy(S) \cdot commafy(roundTo(2, S - SIDES[ 0 ])) \cdot commafy(roundTo(2, S - SIDES[ 1 ])) \cdot commafy(roundTo(2, S - SIDES[ 2 ])) }

A = commafy(ANS)

Something went wrong with that request. Please try again.