Below is a Tcl script (solvetriangle.tcl) for solving a triangle given its 3 sides. It will also compute the area and the circumscribed and inscribed radii of the triangle. You will need the Tcl/Tk programming language installed in order to run the script. You can download ActiveTcl, a version of Tcl/Tk for many operating systems, for free here: http://www.activestate.com/activetcl/downloads/


#!/usr/bin/tclsh
if {[llength $argv] != 3 || [lsearch -regexp -not $argv {^[0-9]*[.]?[0-9]*$}] > 0} {
   puts "Error: You must pass 3 positive numbers as arguments."
   exit
} else {
   set a [format "%.10f" [lindex $argv 0]]
   set b [format "%.10f" [lindex $argv 1]]
   set c [format "%.10f" [lindex $argv 2]]
   if {[expr $a + $b] <= $c || [expr $b + $c] <= $a || [expr $c + $a] <= $b} {
      puts "This triangle has no solution."
      exit
   } else {
      set r2d [expr 180 / acos(-1)]; #Conversion factor for radians to degrees
      puts "Sides:\na = $a\nb = $b\nc = $c\n\nAngles:"
      set A [expr acos(($b*$b + $c*$c - $a*$a)/(2*$b*$c)) * ${r2d}]
      set B [expr acos(($c*$c + $a*$a - $b*$b)/(2*$c*$a)) * ${r2d}]
      set C [expr acos(($a*$a + $b*$b - $c*$c)/(2*$a*$b)) * ${r2d}]
      puts "A = ${A} degrees"
      puts "B = ${B} degrees"
      puts "C = ${C} degrees"
      set s [format "%.10f" [expr ($a + $b + $c) / 2]]
      puts "\ns = $s"
      puts "Area (.5ab*sinC) = [expr 0.5*$a*$b*sin($C/${r2d})]"
      puts "Area (.5bc*sinA) = [expr 0.5*$b*$c*sin($A/${r2d})]"
      puts "Area (.5ca*sinB) = [expr 0.5*$c*$a*sin($B/${r2d})]"
      set K [expr sqrt($s*($s - $a)*($s - $b)*($s - $c))]
      puts "Area (Heron)     = $K"
      set sides [eval lsort -real -decreasing \{$a $b $c\}]
      set a [lindex $sides 0]
      set b [lindex $sides 1]
      set c [lindex $sides 2]
      puts "Area (Kahan)     = [expr sqrt(($a + ($b + $c))*($c - ($a - $b))*($c + ($a - $b))*($a + ($b - $c)))/4]"
      puts "Area (Jiushao)   = [expr sqrt(pow($a*$c,2) - pow(($a*$a + $c*$c - $b*$b)/2,2)) / 2]"
      puts "\nCircumscribed and inscribed radii:"
      puts "R = [expr ($a*$b*$c)/(4*$K)]"
      puts "r = [expr $K / $s]"
   }
}


Copy that script to a file called solvetriangle.tcl (or download it here), then run it using the lengths of the 3 sides as command-line parameters.

For example, to solve a triangle having sides with lengths 2, 3, and 4, you would run this command:

solvetriangle.tcl 2 3 4

If you are using the Windows version of ActiveTcl 8.5.x linked to above, the command would be:

tclsh85 solvetriangle.tcl 2 3 4

The output will look like this:


Sides:
a = 2.0000000000
b = 3.0000000000
c = 4.0000000000

Angles:
A = 28.9550243719 degrees
B = 46.5674634422 degrees
C = 104.477512186 degrees

s = 4.5000000000
Area (.5ab*sinC) = 2.90473750966
Area (.5bc*sinA) = 2.90473750966
Area (.5ca*sinB) = 2.90473750965
Area (Heron)     = 2.90473750966
Area (Kahan)     = 2.90473750966
Area (Jiushao)   = 2.90473750966

Circumscribed and inscribed radii:
R = 2.06559111797
r = 0.645497224369


Notice that the script shows 6 different ways of calculating the area of the triangle, using different formulas discussed in the book. As an experiment, see how the six area formulas compare when using sides with lengths 40, 60, and 99.9999999997:


solvetriangle.tcl 40 60 99.9999999997

Sides:
a = 40.0000000000
b = 60.0000000000
c = 99.9999999997

Angles:
A = 0.000114590291538 degrees
B = 0.000171888617868 degrees
C = 179.999713519 degrees

s = 99.9999999998
Area (.5ab*sinC) = 0.00600004519627
Area (.5bc*sinA) = 0.00599993363445
Area (.5ca*sinB) = 0.00600004465697
Area (Heron)     = 0.0048990231997
Area (Kahan)     = 0.00600005353866
Area (Jiushao)   = 0.00600006164827

Circumscribed and inscribed radii:
R = 12247339.4295
r = 4.89902319971e-05


As discussed in the book, Heron's formula loses some accuracy with “extreme” triangles such as the one above. Notice that one angle is very close to 180° while the other two are almost 0°. The “true” area of the above triangle is approximately 0.00599999999998, so all but Heron's formula give an area close to that. As is also mentioned in the book, these accuracy issues can be solved by using software where an arbitrary precision can be set, e.g. Java's BigDecimal class or Python's decimal module. The Tcl script uses Tcl's default level of precision, so if higher precision is needed then you would have to explore one of these alternatives. This would also be an issue with other programming languages (and calculators) that can not set an arbitrary precision.

Back to the main page