Debian Codegraph @

src/Diagrams/TwoD/Shapes.hs

raw symbols SCIP
1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE TemplateHaskell #-}
4 {-# LANGUAGE TypeFamilies #-}
5
6 {-# OPTIONS_GHC -fno-warn-unused-imports #-}
7 -- for Data.Semigroup
8
9 -----------------------------------------------------------------------------
10 -- |
11 -- Module : Diagrams.TwoD.Shapes
12 -- Copyright : (c) 2011 diagrams-lib team (see LICENSE)
13 -- License : BSD-style (see LICENSE)
14 -- Maintainer : [email protected]
15 --
16 -- Various two-dimensional shapes.
17 --
18 -----------------------------------------------------------------------------
19
20 module Diagrams.TwoD.Shapes
21 (
22 -- * Miscellaneous
23 hrule, vrule
24
25 -- * Regular polygons
26
27 , regPoly
28 , triangle
29 , eqTriangle
30 , square
31 , pentagon
32 , hexagon
33 , heptagon
34 , septagon
35 , octagon
36 , nonagon
37 , decagon
38 , hendecagon
39 , dodecagon
40
41 -- * Other special polygons
42 , unitSquare
43 , rect
44
45 -- * Other shapes
46
47 , roundedRect
48 , RoundedRectOpts(..), radiusTL, radiusTR, radiusBL, radiusBR
49 , roundedRect'
50 ) where
51
52 import Control.Lens (makeLenses, op, (&), (.~), (<>~), (^.))
53 import Data.Default
54 import Data.Semigroup
55
56 import Diagrams.Core
57
58 import Diagrams.Angle
59 import Diagrams.Located (at)
60 import Diagrams.Path
61 import Diagrams.Segment
62 import Diagrams.Trail
63 import Diagrams.TrailLike
64 import Diagrams.TwoD.Arc
65 import Diagrams.TwoD.Polygons
66 import Diagrams.TwoD.Transform
67 import Diagrams.TwoD.Types
68 import Diagrams.TwoD.Vector
69 import Diagrams.Util
70
71
72 -- | Create a centered horizontal (L-R) line of the given length.
73 --
74 -- <<diagrams/src_Diagrams_TwoD_Shapes_hruleEx.svg#diagram=hruleEx&width=300>>
75 --
76 -- > hruleEx = vcat' (with & sep .~ 0.2) (map hrule [1..5])
77 -- > # centerXY # pad 1.1
78 hrule :: (InSpace V2 n t, TrailLike t) => n -> t
79 hrule d = trailLike $ trailFromSegments [straight $ r2 (d, 0)] `at` p2 (-d/2,0)
80
81 -- | Create a centered vertical (T-B) line of the given length.
82 --
83 -- <<diagrams/src_Diagrams_TwoD_Shapes_vruleEx.svg#diagram=vruleEx&height=100>>
84 --
85 -- > vruleEx = hcat' (with & sep .~ 0.2) (map vrule [1, 1.2 .. 2])
86 -- > # centerXY # pad 1.1
87 vrule :: (InSpace V2 n t, TrailLike t) => n -> t
88 vrule d = trailLike $ trailFromSegments [straight $ r2 (0, -d)] `at` p2 (0,d/2)
89
90 -- | A square with its center at the origin and sides of length 1,
91 -- oriented parallel to the axes.
92 --
93 -- <<diagrams/src_Diagrams_TwoD_Shapes_unitSquareEx.svg#diagram=unitSquareEx&width=100>>
94 unitSquare :: (InSpace V2 n t, TrailLike t) => t
95 unitSquare = polygon (def & polyType .~ PolyRegular 4 (sqrt 2 / 2)
96 & polyOrient .~ OrientH)
97
98 -- > unitSquareEx = unitSquare # pad 1.1 # showOrigin
99
100 -- | A square with its center at the origin and sides of the given
101 -- length, oriented parallel to the axes.
102 --
103 -- <<diagrams/src_Diagrams_TwoD_Shapes_squareEx.svg#diagram=squareEx&width=200>>
104 square :: (InSpace V2 n t, TrailLike t) => n -> t
105 square d = rect d d
106
107 -- > squareEx = hcat' (with & sep .~ 0.5) [square 1, square 2, square 3]
108 -- > # centerXY # pad 1.1
109
110 -- | @rect w h@ is an axis-aligned rectangle of width @w@ and height
111 -- @h@, centered at the origin.
112 --
113 -- <<diagrams/src_Diagrams_TwoD_Shapes_rectEx.svg#diagram=rectEx&width=150>>
114 rect :: (InSpace V2 n t, TrailLike t) => n -> n -> t
115 rect w h = trailLike . head . op Path $ unitSquare # scaleX w # scaleY h
116
117 -- > rectEx = rect 1 0.7 # pad 1.1
118
119 -- The above may seem a bit roundabout. In fact, we used to have
120 --
121 -- rect w h = unitSquare # scaleX w # scaleY h
122 --
123 -- since unitSquare can produce any TrailLike. The current code
124 -- instead uses (unitSquare # scaleX w # scaleY h) to specifically
125 -- produce a Path, which is then deconstructed and passed back into
126 -- 'trailLike' to create any TrailLike.
127 --
128 -- The difference is that while scaling by zero works fine for
129 -- Path it does not work very well for, say, Diagrams (leading to
130 -- NaNs or worse). This way, we force the scaling to happen on a
131 -- Path, where we know it will behave properly, and then use the
132 -- resulting geometry to construct an arbitrary TrailLike.
133 --
134 -- See https://github.com/diagrams/diagrams-lib/issues/43 .
135
136 ------------------------------------------------------------
137 -- Regular polygons
138 ------------------------------------------------------------
139
140 -- | Create a regular polygon. The first argument is the number of
141 -- sides, and the second is the /length/ of the sides. (Compare to the
142 -- 'polygon' function with a 'PolyRegular' option, which produces
143 -- polygons of a given /radius/).
144 --
145 -- The polygon will be oriented with one edge parallel to the x-axis.
146 regPoly :: (InSpace V2 n t, TrailLike t) => Int -> n -> t
147 regPoly n l = polygon (def & polyType .~
148 PolySides
149 (repeat (1/fromIntegral n @@ turn))
150 (replicate (n-1) l)
151 & polyOrient .~ OrientH
152 )
153
154 -- > shapeEx sh = sh 1 # pad 1.1
155 -- > triangleEx = shapeEx triangle
156 -- > pentagonEx = shapeEx pentagon
157 -- > hexagonEx = shapeEx hexagon
158 -- > heptagonEx = shapeEx heptagon
159 -- > octagonEx = shapeEx octagon
160 -- > nonagonEx = shapeEx nonagon
161 -- > decagonEx = shapeEx decagon
162 -- > hendecagonEx = shapeEx hendecagon
163 -- > dodecagonEx = shapeEx dodecagon
164
165 -- | A synonym for 'triangle', provided for backwards compatibility.
166 eqTriangle :: (InSpace V2 n t, TrailLike t) => n -> t
167 eqTriangle = triangle
168
169 -- | An equilateral triangle, with sides of the given length and base
170 -- parallel to the x-axis.
171 --
172 -- <<diagrams/src_Diagrams_TwoD_Shapes_triangleEx.svg#diagram=triangleEx&width=100>>
173 triangle :: (InSpace V2 n t, TrailLike t) => n -> t
174 triangle = regPoly 3
175
176 -- | A regular pentagon, with sides of the given length and base
177 -- parallel to the x-axis.
178 --
179 -- <<diagrams/src_Diagrams_TwoD_Shapes_pentagonEx.svg#diagram=pentagonEx&width=100>>
180 pentagon :: (InSpace V2 n t, TrailLike t) => n -> t
181 pentagon = regPoly 5
182
183 -- | A regular hexagon, with sides of the given length and base
184 -- parallel to the x-axis.
185 --
186 -- <<diagrams/src_Diagrams_TwoD_Shapes_hexagonEx.svg#diagram=hexagonEx&width=100>>
187 hexagon :: (InSpace V2 n t, TrailLike t) => n -> t
188 hexagon = regPoly 6
189
190 -- | A regular heptagon, with sides of the given length and base
191 -- parallel to the x-axis.
192 --
193 -- <<diagrams/src_Diagrams_TwoD_Shapes_heptagonEx.svg#diagram=heptagonEx&width=100>>
194 heptagon :: (InSpace V2 n t, TrailLike t) => n -> t
195 heptagon = regPoly 7
196
197 -- | A synonym for 'heptagon'. It is, however, completely inferior,
198 -- being a base admixture of the Latin /septum/ (seven) and the
199 -- Greek γωνία (angle).
200 septagon :: (InSpace V2 n t, TrailLike t) => n -> t
201 septagon = heptagon
202
203 -- | A regular octagon, with sides of the given length and base
204 -- parallel to the x-axis.
205 --
206 -- <<diagrams/src_Diagrams_TwoD_Shapes_octagonEx.svg#diagram=octagonEx&width=100>>
207 octagon :: (InSpace V2 n t, TrailLike t) => n -> t
208 octagon = regPoly 8
209
210 -- | A regular nonagon, with sides of the given length and base
211 -- parallel to the x-axis.
212 --
213 -- <<diagrams/src_Diagrams_TwoD_Shapes_nonagonEx.svg#diagram=nonagonEx&width=100>>
214 nonagon :: (InSpace V2 n t, TrailLike t) => n -> t
215 nonagon = regPoly 9
216
217 -- | A regular decagon, with sides of the given length and base
218 -- parallel to the x-axis.
219 --
220 -- <<diagrams/src_Diagrams_TwoD_Shapes_decagonEx.svg#diagram=decagonEx&width=100>>
221 decagon :: (InSpace V2 n t, TrailLike t) => n -> t
222 decagon = regPoly 10
223
224 -- | A regular hendecagon, with sides of the given length and base
225 -- parallel to the x-axis.
226 --
227 -- <<diagrams/src_Diagrams_TwoD_Shapes_hendecagonEx.svg#diagram=hendecagonEx&width=100>>
228 hendecagon :: (InSpace V2 n t, TrailLike t) => n -> t
229 hendecagon = regPoly 11
230
231 -- | A regular dodecagon, with sides of the given length and base
232 -- parallel to the x-axis.
233 --
234 -- <<diagrams/src_Diagrams_TwoD_Shapes_dodecagonEx.svg#diagram=dodecagonEx&width=100>>
235 dodecagon :: (InSpace V2 n t, TrailLike t) => n -> t
236 dodecagon = regPoly 12
237
238 ------------------------------------------------------------
239 -- Other shapes ------------------------------------------
240 ------------------------------------------------------------
241 data RoundedRectOpts d = RoundedRectOpts { _radiusTL :: d
242 , _radiusTR :: d
243 , _radiusBL :: d
244 , _radiusBR :: d
245 }
246
247 makeLenses ''RoundedRectOpts
248
249 instance (Num d) => Default (RoundedRectOpts d) where
250 def = RoundedRectOpts 0 0 0 0
251
252 -- | @roundedRect w h r@ generates a closed trail, or closed path
253 -- centered at the origin, of an axis-aligned rectangle with width
254 -- @w@, height @h@, and circular rounded corners of radius @r@. If
255 -- @r@ is negative the corner will be cut out in a reverse arc. If
256 -- the size of @r@ is larger than half the smaller dimension of @w@
257 -- and @h@, then it will be reduced to fit in that range, to prevent
258 -- the corners from overlapping. The trail or path begins with the
259 -- right edge and proceeds counterclockwise. If you need to specify
260 -- a different radius for each corner individually, use
261 -- 'roundedRect'' instead.
262 --
263 -- <<diagrams/src_Diagrams_TwoD_Shapes_roundedRectEx.svg#diagram=roundedRectEx&width=400>>
264 --
265 -- > roundedRectEx = pad 1.1 . centerXY $ hcat' (with & sep .~ 0.2)
266 -- > [ roundedRect 0.5 0.4 0.1
267 -- > , roundedRect 0.5 0.4 (-0.1)
268 -- > , roundedRect' 0.7 0.4 (with & radiusTL .~ 0.2
269 -- > & radiusTR .~ -0.2
270 -- > & radiusBR .~ 0.1)
271 -- > ]
272
273 roundedRect :: (InSpace V2 n t, TrailLike t, RealFloat n) => n -> n -> n -> t
274 roundedRect w h r = roundedRect' w h (def & radiusTL .~ r
275 & radiusBR .~ r
276 & radiusTR .~ r
277 & radiusBL .~ r)
278
279 -- | @roundedRect'@ works like @roundedRect@ but allows you to set the radius of
280 -- each corner indivually, using @RoundedRectOpts@. The default corner radius is 0.
281 -- Each radius can also be negative, which results in the curves being reversed
282 -- to be inward instead of outward.
283 roundedRect' :: (InSpace V2 n t, TrailLike t, RealFloat n) => n -> n -> RoundedRectOpts n -> t
284 roundedRect' w h opts
285 = trailLike
286 . (`at` p2 (w/2, abs rBR - h/2))
287 . wrapTrail
288 . glueLine
289 $ seg (0, h - abs rTR - abs rBR)
290 <> mkCorner 0 rTR
291 <> seg (abs rTR + abs rTL - w, 0)
292 <> mkCorner 1 rTL
293 <> seg (0, abs rTL + abs rBL - h)
294 <> mkCorner 2 rBL
295 <> seg (w - abs rBL - abs rBR, 0)
296 <> mkCorner 3 rBR
297 where seg = lineFromOffsets . (:[]) . r2
298 diag = sqrt (w * w + h * h)
299 -- to clamp corner radius, need to compare with other corners that share an
300 -- edge. If the corners overlap then reduce the largest corner first, as far
301 -- as 50% of the edge in question.
302 rTL = clampCnr radiusTR radiusBL radiusBR radiusTL
303 rBL = clampCnr radiusBR radiusTL radiusTR radiusBL
304 rTR = clampCnr radiusTL radiusBR radiusBL radiusTR
305 rBR = clampCnr radiusBL radiusTR radiusTL radiusBR
306 clampCnr rx ry ro r = let (rx',ry',ro',r') = (opts^.rx, opts^.ry, opts^.ro, opts^.r)
307 in clampDiag ro' . clampAdj h ry' . clampAdj w rx' $ r'
308 -- prevent curves of adjacent corners from overlapping
309 clampAdj len adj r = if abs r > len/2
310 then sign r * max (len/2) (min (len - abs adj) (abs r))
311 else r
312 -- prevent inward curves of diagonally opposite corners from intersecting
313 clampDiag opp r = if r < 0 && opp < 0 && abs r > diag / 2
314 then sign r * max (diag / 2) (min (abs r) (diag + opp))
315 else r
316 sign n = if n < 0 then -1 else 1
317 mkCorner k r | r == 0 = mempty
318 | r < 0 = doArc 3 (-1)
319 | otherwise = doArc 0 1
320 where
321 doArc d s =
322 arc' r (xDir & _theta <>~ ((k+d)/4 @@ turn)) (s/4 @@ turn)