Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do scaling on a Path before constructing PathLike in 'rect' (fixes #43) #70

Merged
merged 1 commit into from
Jan 1, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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