module Geo where import GraphicsUtils import List (nub) import IOExts(trace) -- to make ghc happy -- delete for hugs -- fromInt :: Num a=> Int-> a -- fromInt n = fromInteger (toInteger n) type Dimension = Int rect :: Dimension-> Dimension-> Figure triangle :: Dimension-> Angle-> Dimension-> Figure poly :: [Point]-> Figure circle :: Dimension-> Figure translate :: Point-> Figure-> Figure scale :: Double-> Figure-> Figure rotate :: Angle-> Figure-> Figure rotAt :: Point-> Angle-> Figure-> Figure rotAt (px, py) w = translate (px, py) . rotate w . translate (-px, -py) scaleFrom :: Point-> Double-> Figure-> Figure scaleFrom (px, py) f = translate (px, py). scale f. translate (-px, -py) data Figure = Polygon Point Double [Point] | Circle Point Double smult :: Double-> Point-> Point smult f (x, y) | f == 1 = (x, y) | otherwise = (round (f* fromInt x), round (f* fromInt y)) add :: Point-> Point-> Point add (x1, y1) (x2, y2) = (x1+ x2, y1+ y2) rot :: Angle-> Point-> Point rot w (x, y) | w == 0 = (x, y) | otherwise = (round (x1* cos w+ y1* sin w), round (-x1 * sin w + y1* cos w)) where x1 = fromInt x y1 = fromInt y rect a b = let x2= a `div` 2; y2= b `div` 2 in Polygon (0,0) 1 [(x2, y2), (-x2, y2), (-x2, -y2), (x2, -y2)] triangle l1 a l2 = Polygon (0,0) 1 [(0, 0), (l1, 0), rot a (l2, 0)] poly ps = Polygon (0, 0) 1 ps circle r = Circle (0, 0) (fromInt r) rotate a (Polygon t r ps) = Polygon (rot a t) r (map (rot a) ps) rotate a (Circle c r) = Circle (rot a c) r scale s (Polygon t r ps) = Polygon t (r* s) ps scale s (Circle c r) = Circle c (r* s) translate p (Polygon q r ps) = Polygon (add p q) r ps translate p (Circle c r) = Circle (add p c) r draw :: Point-> Figure-> Graphic draw m (Polygon q r ps) = polygon (map (add m. add q. smult r) ps) draw (x, y) (Circle (cx, cy) r) = ellipse (cx+x- rad, cy+y-rad) (cx+x+ rad, cy+y+rad) where rad= round r