Re: [QUIZ] Triangle Area (#160)
- From: Adam Shelly <adam.shelly@xxxxxxxxx>
- Date: Mon, 21 Apr 2008 18:50:50 -0500
On 4/21/08, Todd Benson <caduceass@xxxxxxxxx> 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
.
- Follow-Ups:
- Re: [QUIZ] Triangle Area (#160)
- From: Todd Benson
- Re: [QUIZ] Triangle Area (#160)
- References:
- [QUIZ] Triangle Area (#160)
- From: Matthew Moss
- Re: [QUIZ] Triangle Area (#160)
- From: Todd Benson
- [QUIZ] Triangle Area (#160)
- Prev by Date: Re: WIN32OLE disable busy dialog etc
- Next by Date: Re: Database construction in Ruby
- Previous by thread: Re: [QUIZ] Triangle Area (#160)
- Next by thread: Re: [QUIZ] Triangle Area (#160)
- Index(es):
Relevant Pages
|
Loading