Search code examples
haskellhaskell-diagrams

How to make line as thick as circle?


I want to make a rounded line with a width equal to the diameter of a circle. After much trial and error this seems to produce the wanted output.

import Diagrams.Prelude
import Diagrams.Backend.Cairo.CmdLine
main = mainWith example

path = fromVertices [p2 (0,0), p2 (3,0)] # lw 80

example :: Diagram B
example = atPoints [p2 (0,0)] n
  <> (path # lineCap LineCapRound  . lineJoin LineJoinRound)
  <> atPoints [p2 (3,0)] n
  <> square 10 # lw none # fc white
 where
   n =[circle 1 # fc green # lw none]

However it feels wrong. I would expect lw 2 to correspond to circle 1 because 2 is twice the radius, but certainly not lw 80?!
Why does it work with 80? Assumning I missed something, how does one make a rounded line as wide as a circle?


Solution

  • You want lwL, which stands for "line weight, local", and uses the same units for line thickness as for length.

    There are more details in the manual, here is a relevant excerpt:

    local units are the most straightforward to explain. Values in local units are interpreted in the context of the local vector space, just as most other length measurements (e.g. arguments to functions like circle and square). For example, square 1 # lwL 0.2 specifies a square which is drawn with lines one fifth as wide as its sides are long—and will always be, even if it is scaled: the line width scales right along with the square. (The L in lwL stands for "Local".)