Skip to content

Commit

Permalink
Use trailPoints instead of trailVertices to match number of ends with…
Browse files Browse the repository at this point in the history
… trailSegments.

Fixes diagrams#263.
  • Loading branch information
robx committed Feb 14, 2016
1 parent c2f45c9 commit 8c3b7ad
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
11 changes: 11 additions & 0 deletions diagrams-lib.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,14 @@ Library
RankNTypes, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell,
TypeOperators, TypeSynonymInstances, UndecidableInstances, ViewPatterns,
LambdaCase

test-suite tests
type: exitcode-stdio-1.0
main-is: Test.hs
other-modules: Diagrams.TwoD.OffsetTest
hs-source-dirs: test
build-depends: base >= 4.2 && < 4.9,
tasty >= 0.10,
tasty-hunit >= 0.9.2,
diagrams-lib
default-language: Haskell2010
4 changes: 2 additions & 2 deletions src/Diagrams/TwoD/Offset.hs
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ offsetTrail' opts r t = joinSegments eps j isLoop (opts^.offsetMiterLimit) r end
where
eps = opts^.offsetEpsilon
offset = map (bindLoc (offsetSegment eps r)) . locatedTrailSegments
ends | isLoop = (\(a:as) -> as ++ [a]) . trailVertices $ t
| otherwise = tail . trailVertices $ t
ends | isLoop = (\(a:as) -> as ++ [a]) . trailPoints $ t
| otherwise = tail . trailPoints $ t
j = fromLineJoin (opts^.offsetJoin)

isLoop = withTrail (const False) (const True) (unLoc t)
Expand Down
48 changes: 48 additions & 0 deletions test/Diagrams/TwoD/OffsetTest.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Diagrams.TwoD.OffsetTest
(
tests
) where

import Test.Tasty (TestTree)
import Test.Tasty.HUnit

import Diagrams.Prelude
import Diagrams.TwoD.Offset

tests :: [TestTree]
tests =
[ testCase "line"
(offsetTrailVertices
[p2 (0, 0), p2 (1, 0)]
[p2 (0, -1), p2 (1, -1)])
, testCase "square"
(offsetTrailVertices
[p2 (0, 0), p2 (1, 0), p2 (1, 1), p2 (0, 1), p2 (0, 0)]
[p2 (0, -1), p2 (2, -1), p2 (2, 2), p2 (-1, 2), p2 (-1, 0)])
, testCase "square loop"
(offsetTrailLoopVertices
[p2 (0, 0), p2 (1, 0), p2 (1, 1), p2 (0, 1), p2 (0, 0)]
[p2 (2, -1), p2 (2, 2), p2 (-1, 2), p2 (-1, -1)])
, testCase "redundant line"
(offsetTrailVertices
[p2 (0, 0), p2 (0.5, 0), p2 (1, 0)]
[p2 (0, -1), p2 (1, -1)])
, testCase "redundant square"
(offsetTrailVertices
[p2 (0, 0), p2 (1, 0), p2 (1, 0.5), p2 (1, 1), p2 (0, 1), p2 (0, 0)]
[p2 (0, -1), p2 (2, -1), p2 (2, 2), p2 (-1, 2), p2 (-1, 0)])
, testCase "redundant square loop"
(offsetTrailLoopVertices
[p2 (0, 0), p2 (1, 0), p2 (1, 0.5), p2 (1, 1), p2 (0, 1), p2 (0, 0)]
[p2 (2, -1), p2 (2, 2), p2 (-1, 2), p2 (-1, -1)])
]

offsetTrailVertices :: [Point V2 Double] -> [Point V2 Double] -> Assertion
offsetTrailVertices orig off =
(trailVertices . offsetTrail 1 . fromVertices $ orig) @?= off

offsetTrailLoopVertices :: [Point V2 Double] -> [Point V2 Double] -> Assertion
offsetTrailLoopVertices orig off =
(trailVertices . offsetTrail 1 . loopTrailFromVertices $ orig) @?= off
where
loopTrailFromVertices = (`at` origin) . wrapTrail . glueLine . lineFromVertices
10 changes: 10 additions & 0 deletions test/Test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Test.Tasty (defaultMain, testGroup, TestTree)

import qualified Diagrams.TwoD.OffsetTest as TwoD.OffsetTest

tests :: TestTree
tests = testGroup "unit tests"
[ testGroup "TwoD.Offset" TwoD.OffsetTest.tests ]

main :: IO ()
main = defaultMain tests

0 comments on commit 8c3b7ad

Please sign in to comment.