Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Shape where
- triangleArea :: Point -> Point -> Point -> Double
- triangleArea p1 p2 p3
- | firstSide + secondSide >= thirdSide && firstSide + thirdSide >= secondSide && secondSide + thirdSide >= firstSide = ((x p1) * ((y p2) - (y p3)) + (x p2) * ((y p3) - (y p1)) + (x p3) * ((y p1) - (y p2))) / 2
- | otherwise = 0
- where
- firstSide = sqrt(((x p2) - (x p1))^2 + ((y p2) - (y p1))^2)
- secondSide = sqrt(((x p3) - (x p2))^2 + ((y p3) - (y p2))^2)
- thirdSide = sqrt(((x p3) - (x p1))^2 + ((y p3) - (y p1))^2)
- minx :: Point -> Point -> Point -> Double
- minx a b c = min (min (x a) (x b)) (x c)
- maxx :: Point -> Point -> Point -> Double
- maxx a b c = max (max (x a) (x b)) (x c)
- miny :: Point -> Point -> Point -> Double
- miny a b c = min (min (y a) (y b)) (y c)
- maxy :: Point -> Point -> Point -> Double
- maxy a b c = max (max (y a) (y b)) (y c)
- data Point = Point { x :: Double, y :: Double } deriving (Show)
- -- Rectangles
- data Rectangle = Rectangle { p1 :: Point, p2 :: Point } deriving (Show)
- -- Circles
- data Circle = Circle { m :: Point, r :: Double } deriving (Show)
- -- Triangles
- data Triangle = Triangle { a :: Point, b :: Point, c :: Point } deriving (Show)
- class Area a where
- area :: a -> Double
- instance Area Rectangle where
- area (Rectangle p1 p2) = ((x p2) - (x p1)) * ((y p2) - (y p1))
- instance Area Circle where
- area (Circle m r) = pi * r * r
- instance Area Triangle where
- area (Triangle a b c) = triangleArea a b c
- class (Area a) => BoundingBox a where
- bbox :: a -> Rectangle
- instance BoundingBox Rectangle where
- bbox (Rectangle p1 p2) = Rectangle { p1 = p1 , p2 = p2 }
- instance BoundingBox Circle where
- bbox (Circle m r) = Rectangle { p1 = Point {x = ((x m) - r) , y = ((y m) - r)} , p2 = Point {x = ((x m) + r) , y = ((y m) + r)}}
- instance BoundingBox Triangle where
- bbox (Triangle a b c) = Rectangle { p1 = Point {x = (minx a b c) , y = (miny a b c)} , p2 = Point {x = (maxx a b c) , y = (maxy a b c)}}
- pa = Point { x = 20, y = 10 }
- pb = Point { x = 30, y = 30 }
- pc = Point { x = 50, y = 15 }
- box = Rectangle { p1 = pa, p2 = pb }
- circle = Circle { m = pa, r = 10 }
- triangle = Triangle { a = pa, b = pb, c = pc }
- main = do
- print(area box)
- print(area circle)
- print(area triangle)
- print(bbox box)
- print(bbox circle)
- print(bbox triangle)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement