area (Rectangle a b) = abs(a * b)
area (RtTriangle a b) = abs(a * b) / 2
area (Ellipse r1 r2) = pi * abs(r1 * r2)
perimeter (Rectangle s1 s2) = 2 * (abs s1 + abs s2)
perimeter (RtTriangle s1 s2) = abs s1 + abs s2 + sqrt(s1^2 + s2^2)
perimeter (Ellipse r1 r2)
| abs r1 > abs r2 = ellipsePerim (abs r1) (abs r2)
| otherwise = ellipsePerim (abs r2) (abs r1)
where ellipsePerim r1 r2 = let e = sqrt (r1^2 - r2^2) / r1
aux s i = nextEl e s i
s = scanl aux (0.25 * e^2) [2..]
test x = x > epsilon
sSum = sum (takeWhile test s)
in 2 * r1 * pi * (1 - sSum) |

area (Rectangle a b) = abs(a * b)
area (RtTriangle a b) = abs(a * b) / 2
area (Ellipse r1 r2) = pi * abs(r1 * r2)
perimeter (Rectangle s1 s2) = 2 * (abs s1 + abs s2)
perimeter (RtTriangle s1 s2) = abs s1 + abs s2 + sqrt(s1^2 + s2^2)
perimeter (Ellipse r1 r2)
| abs r1 > abs r2 = ellipsePerim (abs r1) (abs r2)
| otherwise = ellipsePerim (abs r2) (abs r1)
where ellipsePerim r1 r2 = let e = sqrt (r1^2 - r2^2) / r1
aux s i = nextEl e s i
s = scanl aux (0.25 * e^2) [2..]
test x = x > epsilon
sSum = sum (takeWhile test s)
in 2 * r1 * pi * (1 - sSum)

The definitions dealing with other kinds of `Shape` need not be changed.

This entry was posted on Sunday, July 29th, 2007 at 11:38 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.