------art_4431_17378229.1208752980858
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi,

My solution to this week's Ruby Quiz is attached.

A quick overview of my solution:
1. Find the lengths of all the sides of the triangle using the
Pythagorean theorem.
2. Find the perimeter and the semiperimeter (half the perimeter) from
those values.
3. Use Heron's Formula to calculate area.  Heron's formula is a sqrt(s(s-a)(s-b)(s-c)) where s is the semiperimeter and a, b, and c
are the side lengths.

Dan

On Sat, Apr 19, 2008 at 12:39 PM, Matthew Moss <matthew.moss / gmail.com> wrote:
> 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 Quiz 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 Quiz 2 by submitting ideas as often as you can! (A
>  permanent, new website is in the works for Ruby Quiz 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 Talk 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  ambda { Vector[rand(101)-50, rand(101)-50] }
>
>     class Triangle
>         def initialize(a, b, c)
>                 @a, @b, @c  , b, c
>         end
>
>         def Triangle.random(foo  ANDOM_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.
>
>
>  2. 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.
>
>
>  3. 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.
>
>
>  4. 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!
>
>
>

------art_4431_17378229.1208752980858
Content-Type: application/x-ruby; name=triangle-ruby-quiz.rb
Content-Transfer-Encoding: base64
X-Attachment-Id: f_ffak702i0
Content-Disposition: attachment; filename=triangle-ruby-quiz.rb

cmVxdWlyZSAncnVieWdlbXMnCnJlcXVpcmUgJ2ZhY2V0cycKcmVxdWlyZSAnbWF0cml4JwpyZXF1
aXJlICdlbnVtZXJhdG9yJwoKY2xhc3MgQXJyYXkKCWRlZiBwcm9kdWN0CgkJaW5qZWN0KC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