| 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 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 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 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 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 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 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 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 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 |
|
| 166 |
eqTriangle :: (InSpace V2 n t, TrailLike t) => n -> t |
| 167 |
eqTriangle = triangle |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
triangle :: (InSpace V2 n t, TrailLike t) => n -> t |
| 174 |
triangle = regPoly 3 |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
pentagon :: (InSpace V2 n t, TrailLike t) => n -> t |
| 181 |
pentagon = regPoly 5 |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
hexagon :: (InSpace V2 n t, TrailLike t) => n -> t |
| 188 |
hexagon = regPoly 6 |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
heptagon :: (InSpace V2 n t, TrailLike t) => n -> t |
| 195 |
heptagon = regPoly 7 |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
gle). |
| 200 |
septagon :: (InSpace V2 n t, TrailLike t) => n -> t |
| 201 |
septagon = heptagon |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
octagon :: (InSpace V2 n t, TrailLike t) => n -> t |
| 208 |
octagon = regPoly 8 |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
nonagon :: (InSpace V2 n t, TrailLike t) => n -> t |
| 215 |
nonagon = regPoly 9 |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
decagon :: (InSpace V2 n t, TrailLike t) => n -> t |
| 222 |
decagon = regPoly 10 |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
hendecagon :: (InSpace V2 n t, TrailLike t) => n -> t |
| 229 |
hendecagon = regPoly 11 |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 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 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 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 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 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) |