Skip to content

Commit

Permalink
Merge pull request #70 from diagrams/issue43
Browse files Browse the repository at this point in the history
do scaling on a Path before constructing PathLike in 'rect' (fixes #43)
  • Loading branch information
fryguybob committed Jan 1, 2013
2 parents 3f3b646 + 479821b commit e7b3d92
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Diagrams/TwoD/Shapes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,32 @@ unitSquare = polygon with { polyType = PolyRegular 4 (sqrt 2 / 2)
-- | A square with its center at the origin and sides of the given
-- length, oriented parallel to the axes.
square :: (PathLike p, Transformable p, V p ~ R2) => Double -> p
square d = unitSquare # scale d
square d = rect d d

-- | @rect w h@ is an axis-aligned rectangle of width @w@ and height
-- @h@, centered at the origin.
rect :: (PathLike p, Transformable p, V p ~ R2) => Double -> Double -> p
rect w h = unitSquare # scaleX w # scaleY h
rect w h = pathLike p True (trailSegments t)
where
r = unitSquare # scaleX w # scaleY h
(p,t) = head . pathTrails $ r

-- The above may seem a bit roundabout. In fact, we used to have
--
-- rect w h = unitSquare # scaleX w # scaleY h
--
-- since unitSquare can produce any PathLike. The current code
-- instead uses (unitSquare # scaleX w # scaleY h) to specifically
-- produce a Path, which is then deconstructed and passed into
-- 'pathLike' to create any PathLike.
--
-- The difference is that while scaling by zero works fine for
-- Path it does not work very well for, say, Diagrams (leading to
-- NaNs or worse). This way, we force the scaling to happen on a
-- Path, where we know it will behave properly, and then use the
-- resulting geometry to construct an arbitrary PathLike.
--
-- See https://github.com/diagrams/diagrams-lib/issues/43 .

------------------------------------------------------------
-- Regular polygons
Expand Down

0 comments on commit e7b3d92

Please sign in to comment.