This repository was archived by the owner on Dec 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleDrawing.hs
More file actions
144 lines (115 loc) · 3.68 KB
/
Copy pathSimpleDrawing.hs
File metadata and controls
144 lines (115 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, DeriveFunctor, IncoherentInstances #-}
module SimpleDrawing ( draw
, run
, GLfloat
, Point
, Drawable
, Polygon(..)
, Line(..)
, Circle(..)
, Triangle(..)
, fromPolar2D
, Rotate
, rotate
, Scale
, scale
, flush
, run1
, run2
, mainLoop
, GLGraphics
, clear
, currentColor
, ($=)
, Color4 (..)
, Radius
, Angle ) where
import Graphics.UI.GLUT hiding (Point, Polygon, Line, Triangle, rotate, scale, clear)
import qualified Graphics.UI.GLUT as GL
import Control.Concurrent
clear = do
GL.clear [GL.ColorBuffer]
flush
{- 1. Посмотреть про полигоны и для чего нужен PolygonMode
-}
type Point = (GLdouble, GLdouble)
type GLGraphics a = IO a
data Polygon p = Polygon [p] deriving (Show, Functor)
data Line p = Line p p deriving (Show, Functor)
data Circle p = Circle p Radius deriving (Show, Functor)
data Triangle p = Tr p p p deriving (Show, Functor)
type Polar2D = (Radius, Angle)
fromPolar2D :: Polar2D -> Point
fromPolar2D (r,a) = (r*cos a, r*sin a)
class Drawable a where
draw :: a -> GLGraphics ()
instance Drawable a => Drawable [a] where
draw = mapM_ draw
instance Drawable Point where
draw (x,y) = renderPrimitive Points $ vertex $ Vertex2 x y
instance Drawable (Line Point) where
draw (Line (x0,y0) (x1,y1)) = renderPrimitive Lines $ do
vertex $ Vertex2 x0 y0
vertex $ Vertex2 x1 y1
instance Drawable (Polygon Point) where
draw (Polygon ps) = renderPrimitive LineLoop $ mapM_ (vertex . uncurry Vertex2) ps
instance Drawable (Circle Point) where
draw (Circle (a,b) r) = draw $ Polygon $ map (\(x,y) -> (x+a, y+b)) [fromPolar2D (1,t) | t <- [0,pi/24..2*pi]]
instance Drawable (Triangle Point) where
draw (Tr p0 p1 p2) = draw $ Polygon [p0,p1,p2]
class Rotate a where
rotate :: a -> Angle -> a
-- Почему происходит overlapping???
instance Rotate Point where
rotate (x,y) a = let x1 = x*cos a - y*sin a
y1 = x*sin a + y*cos a
in (x1,y1)
instance (Rotate p, Functor f) => Rotate (f p) where
rotate p a = fmap (\x -> rotate x a) p
class Scale a where
scale :: a -> GLdouble -> a
instance Scale Point where
scale (x,y) m = (m*x, m*y)
instance (Functor f, Scale p) => Scale (f p) where
scale p m = fmap (\x -> scale x m) p
run :: IO () -> IO ()
run io = do
getArgsAndInitialize
createWindow "App"
displayCallback $= do
GL.clear [ColorBuffer]
currentColor $= Color4 0 0.3 1 1
io
flush
mainLoop
run1 :: IO () -> IO ()
run1 io = do
getArgsAndInitialize
createWindow "App"
displayCallback $= do
GL.clear [ColorBuffer]
currentColor $= Color4 0 0.3 1 1
io
flush
run2 :: MVar (IO ()) -> IO ()
run2 mvar = do
getArgsAndInitialize
createWindow "App"
-- GL.clear [ColorBuffer]
-- currentColor $= Color4 0 0.3 1 1
-- flush
idleCallback $= Just m
displayCallback $= do
GL.clear [ColorBuffer]
currentColor $= Color4 0 0.3 1 1
flush
-- print "DisplayCallback"
-- mainLoop
-- keyboardMouseCallback $= Just (\_ _ _ _ -> print "Click")
where
m1 = takeMVar mvar >>= id
m = do
v <- tryTakeMVar mvar
case v of
Just io -> io
Nothing -> return ()