On 4/21/08, Todd Benson <caduceass / gmail.com> wrote:

> 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).

1/2(b*h) wa the first thing I thought of when I read the quiz title.
Here's my implementation:

class Triangle
  def area
    pts = [@a, @b, @c]
    #filter out degenerate triangles
    return 0 if pts.uniq!

    #move one point to the origin
    offset = pts[0]*-1.0
    pts.map!{|v|v+=offset}

    #find the angle of one leg
    angle=Math::atan(pts[1][1]/pts[1][0])

    #rotate that leg so it lies along X axis
    rotmat = Matrix.rows( [[Math::cos(angle),Math::sin(angle)],
                           [-Math::sin(angle),Math::cos(angle)]])
    pts.map!{|v|rotmat*v}

    #use basic geometry
    base = pts[1][0].abs
    height = pts[2][1].abs
    area=base*height/ 2.0
  end
end


-Adam