by shigemk2

当面は技術的なことしか書かない

Pointデータ型で形を整える

-- 空間の点を表す中間データ構造を作る
-- そうするともっと図形をもっとわかりやすくできるお
data Point = Point Float Float deriving (Show)
data Shape = Circle Point Float | Rectangle Point Point deriving (Show)

area :: Shape -> Float
area (Circle _ r) = pi * r ^ 2
area (Rectangle (Point x1 y1) (Point x2 y2))
     = (abs $ x2 - x1) * (abs $ y2 - y1)

-- *Main> area (Rectangle (Point 0 0) (Point 100 100))
-- 10000.0
-- *Main> area (Circle (Point 0 0) 24)
-- 1809.5574

nudge :: Shape -> Float -> Float -> Shape
nudge (Circle (Point x y) r) a b = Circle (Point (x+a) (y+b)) r
nudge (Rectangle (Point x1 y1) (Point x2 y2)) a b
       = Rectangle (Point (x1+a) (y1+b)) (Point (x2+a) (y2+b))

-- *Main> nudge (Circle (Point 34 34) 10) 5 10
-- Circle (Point 39.0 44.0) 10.0

-- 半径を取って、座標系の原点を中心とし、与えられた半径の円を作る
-- 関数を作る
baseCircle :: Float -> Shape
baseCircle r = Circle (Point 0 0) r
-- 幅と高さを取って、左下の頂点が原点にある長方形を作る関数を作る
baseRect :: Float -> Float -> Shape
baseRect width height = Rectangle (Point 0 0) (Point width height)
-- *Main> nudge (baseRect 40 100) 60 23
-- Rectangle (Point 60.0 23.0) (Point 100.0 123.0)