From: Todd Benson Date: 2008-04-22T07:08:48+09:00 Subject: Re: [QUIZ] Triangle Area (#160) I'm doing a little cheating here, and may have severely wimped out. Not only did I fail to meet the actual requirements of the quiz, but also ignored the unit test (well, not completely). I was going to --and may still-- use dot products some other day to show off my meager knowledge of math and allow myself to enter valhalla. I'm not here for prosperity, so, anyways, my one-liner to satisfy the quiz was atrocious. To replace it, I'll use this for now... require 'mathn' puts 0.5 * Matrix[[28.0, 23.0, 1.0], [-46.0, 31.0, 1.0], [-39.0, 5.0, 1.0]].det.abs => 934.0 That's probably the easiest way. Doing a redneck modify to the test data structure... require 'mathn' arr = [ [[-42, 4, 1], [-26, -34, 1], [ 2, 8, 1]], [[ 45, -44, 1], [ 1, 43, 1], [ 42, 48, 1]], [[-24, 29, 1], [ 42, -1, 1], [ 10, 43, 1]], [[ 48, -19, 1], [-19, 37, 1], [-15, 36, 1]], [[-10, -40, 1], [-35, -19, 1], [ 1, 33, 1]], [[ 28, 23, 1], [-46, 31, 1], [-39, 5, 1]], [[-32, 17, 1], [-50, -8, 1], [-39, 27, 1]], [[ 40, -19, 1], [ 39, -34, 1], [-37, -15, 1]], [[ 47, -34, 1], [ 26, -37, 1], [ 50, -7, 1]], [[-49, 46, 1], [ 29, 46, 1], [ 5, -34, 1]], ] arr.each do |a| puts 0.5 * Matrix[*a].det.abs end 868.0 1893.5 972.0 78.5 1028.0 934.0 177.5 579.5 279.0 3120.0 It's not "application worthy", but I wasn't shooting for that. I'm also not sure if you might have to use Floats in the array above or not, because it passes the test given Integers. I wrote some code that attempts to build a random array, but I'm embarrassed to show it; and also the Vector into Matrix code I used looks ugly. I wrote that snippet of code before going to wikipedia. I had this feeling originally that I should attempt something like Heron's idea, but didn't have the time. I suppose another way to approach it could be to use a matrix transformation to get your 'base' (turn the triangle, or the coordinate system; however you prefer to see it) and use a simple 1/2(b*h). Non-euclidean would be an interesting extra credit. Todd