Re: Triangle Area (#160)

Thanks for your work in keeping up RubyQuiz! My spare thinking time has
been taken up by other work lately, but this one’s simple enough that I
feel fairly confident in typing up this solution while away from the
nearest computer with a Ruby interpreter installed (browser Ruby didn’t
work for testing).

As a competition-loving math student, for me the obvious way to
calculate the area of a triangle is Hero’s formula:

class Vector
def distance(oth)
Math.sqrt(to_a.zip(oth.to_a).inject(0){|s,(a,b)|s+(a-b)**2})
end
end

class Triangle
def area
ab = @a.distance(@b)
bc = @b.distance(@c)
ac = @a.distance(@c)
s = (ab+bc+ac)/2
Math.sqrt(s*(s-ab)(s-bc)(s-ac))
end
end

----- Original Message ----
From: Matthew M. [email protected]
To: ruby-talk ML [email protected]
Sent: Saturday, April 19, 2008 11:39:34 AM
Subject: [QUIZ] Triangle Area (#160)

Apologies for the latest… Some busy stuff this week in “real life.”
In light of that, I’ve kept this quiz simple: you only need implement
one function. I do provide brief descriptions of a few possible
techniques, but don’t feel you need to do them all! Just pick one that
sounds interesting to you…

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Q. 2:

  1. Please do not post any solutions or spoiler discussion for this
    quiz until 48 hours have passed from the time on this message.

  2. Support Ruby Q. 2 by submitting ideas as often as you can! (A
    permanent, new website is in the works for Ruby Q. 2. Until then,
    please visit the temporary website at

    http://matthew.moss.googlepages.com/home.

  3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby T. follow the discussion. Please reply to
the original quiz message, if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

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.

There are a few techniques that come to mind for determining (or
closely
estimating) the area of a triangle. You do not need to attempt all of
these;
just pick a technique that sounds fun and do implement it.

  1. Determinant Method

It is possible to calculate the area of a triangle very simply using
just the
points as part of a matrix, and calculating the determinant of that
matrix.
See (http://mathforum.org/library/drmath/view/55063.html) for an
explanation
of the technique. This is quick and easy, so if you don’t have much
time this
week, try this.

  1. Monte Carlo Method

The Monte Carlo method first requires that you determine a bounding
area
(typically a box) that surrounds the test area (i.e. the triangle).
Then you
choose thousands of random points within the box, determining for each
point
whether it falls inside or outside the triangle.

Knowing the area of the box (an easier calculation) and the percentage
of
random points that fell inside the triangle, you can multiply those
two values
together to get the triangle’s area.

  1. Scan-Line Method

Imagine covering the triangle with horizontal bars of a certain
height, such
that each bar is only wide enough to hide the triangle underneath.
Knowing
the width and height of each bar (i.e. rectangle) lets you calculate
the area
of each, and summed together is an approximation of the triangle’s
area.

(This is sometimes called a scan-line method, as you are examining
horizontal
slices of the subject, very much like a television scan line draws a
number of
horizontal slices of the picture.)

Each time the height of the bars are halved (and twice as many are
employed),
your estimate of the triangle’s area will improve. Those familiar with
calculus
will recognize this as integration, as the height of each horizontal
slice
approaches zero.

  1. Something else!

If none of these methods interest you, but you have with another
method to
estimate or determine exactly the triangle’s area, please do!

  ____________________________________________________________________________________

Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now.
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs