Thursday, August 25, 2011

Chapter 4. Shapes II: Drawing Shapes

이번 장에서는 Shape를 Graphic으로 변경하여 윈도우에 그리는 코드를 작성해보자. 모듈 이름은 Draw라 하자.
module Draw (...) where

import Shape
import SOEGraphics

-- 윈도우의 사이즈: 600x500
xWin, yWin :: Int
xWin = 600
yWin = 500

-- 인치를 픽셀로 바꾸는 함수
inchToPixel :: Float -> Int
inchToPixel x = round (100 * x)

-- 윈도우의 중심을 (0,0)으로 하는 것으로 쓰는 것이 편하므로 이것을 실제 윈도우의 좌표(좌상단을 (0,0)으로 하는 것)로 바꾸어 주는 함수를 만든다.
trans :: Vertex -> Point
trans (x, y) = (xWin2 + inchToPixel x, yWin2 - inchToPixel y)

xWin2, yWin2 :: Int
xWin2 = xWin 'div' 2
yWin2 = yWin 'div' 2

transList :: [Vertex] -> [Point]
transList []     = []
transList (p:ps) = trans p : transList ps

--Shape을 Graphic으로 바꾸어 주는 함수
shapeToGraphic :: Shape -> Graphic
shapeToGraphic (Rectangle s1 s2)
= let s12 = s1 / 2
s22 = s2 / 2
in polygon (transList
[(-s12, -s22), (-s12, s22), (s12, s22), (s12, -s22)])
shapetoGraphic (Ellipse r1 r2)
= ellipse (trans (-r1, -r2)) (trans (r1, r2))
shapeToGraphic (RtTriangle s1 s2)
= polygon (transList [(0,0), (s1,0), (0,s2)])
shapeToGraphic (Polygon vts)
= polygon (transList vts)

No comments:

Post a Comment