Jan Skibinski <[EMAIL PROTECTED]> writes:
> On 22 Aug 2000, Friedrich Dominicus wrote:
>
> > Dear Haskell Fans, I'm afraid that I'm a bit dumb but I'm somewhat
> > stuck.
> >
> > Can someone give me a hand on this problem
> >
> > I wrote this code to solve SOE, exc 5.1.
> >
> > import Shape
> >
> > triangleArea :: [Vertex] -> Float
> > triangleArea (v1:v2:v3:_) = let a = distBetween v1 v2
> > b = distBetween v2 v3
> > c = distBetween v3 v1
> > s = 0.5 * (a + b + c)
> > in sqrt (s * (s-a) *(s-b) * (s-c))
> > triangleArea _ = 0
>
> By the way:
> A classical way of calculating area of a triangle is to compute
> cross product r = p x q, where p = v2 - v1 and q = v3 - v1, and
> then take half of its norm; 1/2 * sqrt (r1^2 + r2^2 + r3^2).
> Here, function 'sqrt' is called once only (providing that you
> compute vector 'r' algebraically, not via norms); that is:
> r1 = p2*q3 - p3*q2
> r2 = p3*q1 - p1*q3
> r3 = p1*q2 - p2*q1
> In your example you call 'sqrt' four times (I presume here
> that 'distanceBetween' is defined in terms of 'sqrt').
> Although I appreciate the reason why this algorithm has been
> exposed in SOE, it is - in fact - quite inefficient due to
> all those 'sqrt' computations.
I have to admit I did not care very much about efficiency. I just
reuse what is there and with that I like to solve the problem. But
anyway I pointed my browser to the given URL and will have a look.
>
> Furthemore, if you wish to compute area of a polygon, you
> can indeed split it into a list of triangles, sum
> all the vector products, and finally take one only 'sqrt' of
> the result and divide it by two. It works for any type
> of polygons, even those that have local convexes, for
> example 'stars', etc. Internal 'holes' in your polygons
> can be also automatically accounted for.
Sometimes knowin math is quite a good thing. I forgot much and so it's
quite a bit hard for me solving some problems. I guess this problem is
an example;-)
--
for e-mail reply remove all after .com