From: Gavin Sinclair Date: 2008-04-22T10:15:07+09:00 Subject: Re: Triangle Area (#160) On Apr 20, 2:39 am, Matthew Moss wrote: > > Quiz #160 > Triangle Area > > Start with the following code for a Triangle class: > > require 'matrix' > > RANDOM_PT = lambda { Vector[rand(101)-50, rand(101)-50] } > > class Triangle > def initialize(a, b, c) > @a, @b, @c = a, b, c > end > > def Triangle.random(foo = RANDOM_PT) > Triangle.new(foo.call, foo.call, foo.call) > end > > def [](i) > [@a, @b, @c][i] > end > > def area > # Fill in this stub. > end > > def inspect > "Triangle[#{@a}, #{@b}, #{@c}]" > end > alias to_s inspect > end > > Your task this week is to write the code for the `area` method. > A method that hasn't been mentioned so far is "Heron's formula": Area = sqrt( s(s-a)(s-b)(s-c) ) where a, b, c are the lengths of the sides and 2s = a + b + c If someone wants to implement that, I'd be keen to see what the code looks like. Cheers, Gavin