Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Shape where
  2.  
  3. triangleArea :: Point -> Point -> Point -> Double
  4. triangleArea p1 p2 p3
  5.     | 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
  6.     | otherwise = 0
  7.     where
  8.         firstSide = sqrt(((x p2) - (x p1))^2 + ((y p2) - (y p1))^2)
  9.         secondSide = sqrt(((x p3) - (x p2))^2 + ((y p3) - (y p2))^2)
  10.         thirdSide = sqrt(((x p3) - (x p1))^2 + ((y p3) - (y p1))^2)
  11.  
  12. minx :: Point -> Point -> Point -> Double
  13. minx a b c = min (min (x a) (x b)) (x c)
  14.  
  15. maxx :: Point -> Point -> Point -> Double
  16. maxx a b c = max (max (x a) (x b)) (x c)
  17.  
  18. miny :: Point -> Point -> Point -> Double
  19. miny a b c = min (min (y a) (y b)) (y c)
  20.  
  21. maxy :: Point -> Point -> Point -> Double
  22. maxy a b c = max (max (y a) (y b)) (y c)
  23.  
  24. data Point = Point { x :: Double, y :: Double } deriving (Show)
  25.    
  26. -- Rectangles
  27.    
  28. data Rectangle = Rectangle { p1 :: Point, p2 :: Point } deriving (Show)
  29.  
  30. -- Circles
  31.  
  32. data Circle = Circle { m :: Point, r :: Double } deriving (Show)
  33.  
  34. -- Triangles
  35.  
  36. data Triangle = Triangle { a :: Point, b :: Point, c :: Point } deriving (Show)
  37.  
  38. class Area a where
  39.     area :: a -> Double
  40.    
  41. instance Area Rectangle where
  42.     area (Rectangle p1 p2) = ((x p2) - (x p1)) * ((y p2) - (y p1))
  43.  
  44. instance Area Circle where
  45.     area (Circle m r) = pi * r * r
  46.  
  47. instance Area Triangle where
  48.     area (Triangle a b c) = triangleArea a b c
  49.  
  50. class (Area a) => BoundingBox a where
  51.     bbox :: a -> Rectangle
  52.  
  53. instance BoundingBox Rectangle where
  54.     bbox (Rectangle p1 p2) = Rectangle { p1 = p1 , p2 = p2 }
  55.  
  56. instance BoundingBox Circle where
  57.     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)}}
  58.  
  59. instance BoundingBox Triangle where
  60.     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)}}
  61.  
  62. pa = Point { x = 20, y = 10 }
  63. pb = Point { x = 30, y = 30 }
  64. pc = Point { x = 50, y = 15 }
  65. box = Rectangle { p1 = pa, p2 = pb }
  66. circle = Circle { m = pa, r = 10 }
  67. triangle = Triangle { a = pa, b = pb, c = pc }
  68.  
  69. main = do
  70.     print(area box)
  71.     print(area circle)
  72.     print(area triangle)
  73.     print(bbox box)
  74.     print(bbox circle)
  75.     print(bbox triangle)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement