-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShapeGraphics.hs
151 lines (125 loc) · 3.62 KB
/
ShapeGraphics.hs
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
145
146
147
148
149
150
module ShapeGraphics (
drawPicture,
Point(..),
Vector(..),
Line(..),
Picture,
Colour (..),
LineStyle (..),
FillStyle (..),
PictureObject(..),
white, black, blue, red, green, yellow, magenta, orange
) where
-- Rasterific
import Graphics.Rasterific hiding (Point, Vector, Line, Path)
import Graphics.Rasterific.Texture
import Graphics.Rasterific.Transformations
import Codec.Picture
data Colour
= Colour
{ redC :: Int
, greenC :: Int
, blueC :: Int
, opacityC :: Int
}
deriving (Show, Eq)
white = Colour 255 255 255 255
black = Colour 0 0 0 255
blue = Colour 0 0 255 255
red = Colour 255 0 0 255
green = Colour 10 255 10 235
yellow = Colour 255 255 0 235
magenta = Colour 153 0 153 255
orange = Colour 254 154 46 255
data Point
= Point
{ xPoint :: Float
, yPoint :: Float
} deriving (Show, Eq)
data Vector
= Vector
{ xVector :: Float
, yVector :: Float
} deriving (Show, Eq)
data Line
= Line
{ startLine :: Point
, endLine :: Point
} deriving (Show, Eq)
data LineStyle
= Solid
| Dashed
| Dotted
deriving (Show, Eq)
data FillStyle
= NoFill
| SolidFill
deriving (Eq, Show)
data PictureObject
= Path
{ pointsPO :: [Point]
, colourPO :: Colour
, lineStylePO :: LineStyle
}
| Circle
{ centerPO :: Point
, radiusPO :: Float
, colourPO :: Colour
, lineStylePO :: LineStyle
, fillStylePO :: FillStyle
}
| Ellipse
{ centerPO :: Point
, widthPO :: Float
, heightPO :: Float
, rotationPO :: Float
, colourPO :: Colour
, lineStylePO :: LineStyle
, fillStylePO :: FillStyle
}
| Polygon
{ pointsPO :: [Point]
, colourPO :: Colour
, lineStylePO :: LineStyle
, fillStylePO :: FillStyle
} deriving (Show, Eq)
type Picture = [PictureObject]
drawPicture linewidth picture
= renderDrawing 800 800 (toColour (Colour 0 0 0 255)) $ do
{ mapM drawObj picture
; return ()
}
where
style SolidFill _ = fill
style _ Solid = stroke linewidth JoinRound (CapRound, CapRound)
style _ Dashed = dashed linewidth JoinRound (CapRound, CapRound)
style _ Dotted = dotted linewidth JoinRound (CapRound, CapRound)
dotted = dashedStroke [linewidth/12, 2 * linewidth]
dashed = dashedStroke [3* linewidth, 6 * linewidth]
texture colour = withTexture (uniformTexture $ toColour colour)
textureG (x1, y1) (x2, y2)
= withTexture (linearGradientTexture
[(0, PixelRGBA8 255 0 0 255), (1, PixelRGBA8 255 255 255 255)]
(V2 x1 y1)(V2 x2 y2))
drawObj (Path points colour lineStyle) =
texture colour
$ style NoFill lineStyle
$ polyline
$ map (\((Point x y)) -> V2 x y) points
drawObj (Circle (Point px py) radius colour lineStyle fillStyle) =
texture colour
$ style fillStyle lineStyle
$ circle (V2 px py) radius
drawObj (Ellipse (Point px py) h w r colour lineStyle fillStyle) =
texture colour
$ style fillStyle lineStyle
. transform (applyTransformation
$ rotateCenter r (V2 px py))
$ ellipse (V2 px py) h w
drawObj (Polygon points colour lineStyle fillStyle) =
texture colour
$ style fillStyle lineStyle
$ polygon
$ map (\((Point x y)) -> V2 x y) points
toColour (Colour a b c d)
= PixelRGBA8 (fromIntegral a) (fromIntegral b) (fromIntegral c) (fromIntegral d)