line-drawing 0.3.0.0 → 0.4.0.0
raw patch · 4 files changed
+36/−13 lines, 4 filesdep +QuickCheckPVP ok
version bump matches the API change (PVP)
Dependencies added: QuickCheck
API changes (from Hackage documentation)
Files
- changes.txt +6/−0
- line-drawing.cabal +2/−1
- src/Line/Draw.hs +17/−11
- test/Line/DrawSpec.hs +11/−1
changes.txt view
@@ -1,3 +1,9 @@+0.4.0.0+-------++- fixes "third quadrant" bug.+- Released Sun 10 Nov 2019 19:14:01 CET.+ 0.3.0.0 -------
line-drawing.cabal view
@@ -1,6 +1,6 @@ cabal-version: >=1.10 name: line-drawing-version: 0.3.0.0+version: 0.4.0.0 synopsis: raster line drawing description: Line drawing algorithms to approximate a line segment on discrete graphical media (raster).@@ -30,6 +30,7 @@ main-is: Test.hs build-depends: base == 4.* , hspec == 2.7.*+ , QuickCheck == 2.13.* other-modules: Line.Draw Line.DrawSpec type: exitcode-stdio-1.0
src/Line/Draw.hs view
@@ -37,21 +37,23 @@ | m1Check = bresenhamBase x y | otherwise = let x' = T.swap x y' = T.swap y- in map (T.swap) (bresenhamBase x' y')+ in map T.swap (bresenhamBase x' y') where m1Check :: Bool- m1Check = abs (x2 - x1) >= abs (y2 - y1)+ m1Check = abs (x2 - x1) > abs (y2 - y1) ----------------- -- ANCILLARIES -- ----------------- +-- bresenhamBase is only valid when y-distance is greater than x-one bresenhamBase :: forall a . Integral a => (a, a) -> (a, a) -> [(a, a)] bresenhamBase (x1, y1) (x2, y2) = let -- first treshold ti = 2 * ady - adx in L.unfoldr f (x1, y1, ti)+ -- unfoldr :: (b -> Maybe (a, b)) -> b -> [a] where -- slope dx, dy, adx, ady :: a@@ -63,23 +65,27 @@ -- sign of increment ix, iy :: a -> a ix | dx > 0 = (+1)- | otherwise = (subtract 1)+ | otherwise = subtract 1 iy | dy > 0 = (+1)- | otherwise = (subtract 1)+ | otherwise = subtract 1 -- step function, takes (x, y, treshold)- -- type State = (Int, Int, Int) -- curr x, curr y, treshold f :: (a, a, a) -> Maybe ((a, a), (a, a, a))- f (cx, cy, t)- | abs cx > abs x2 = Nothing+ f (wx, wy, t)+ -- pos dx and neg dx have different reach conditions+ -- can't use abs wx > abs x2+ | dx >= 0 &&+ wx > x2 = Nothing+ | dx < 0 &&+ wx < x2 = Nothing | otherwise =- let cx' = ix cx+ let wx' = ix wx - cy' | t > 0 = iy cy- | otherwise = cy+ wy' | t > 0 = iy wy+ | otherwise = wy t' | t > 0 = t - 2 * adx + 2 * ady | otherwise = t + 2 * ady- in Just ((cx, cy), (cx', cy', t'))+ in Just ((wx, wy), (wx', wy', t'))
test/Line/DrawSpec.hs view
@@ -3,6 +3,7 @@ import Line.Draw import Test.Hspec+import Test.QuickCheck main :: IO ()@@ -11,7 +12,7 @@ spec :: Spec spec = do - describe "bresenham" $ do+ describe "bresenham (hspec)" $ do it "rasterises a simple line" $ bresenham (0 :: Int, 0) (4, 4) `shouldBe` [(0,0), (1,1), (2,2), (3,3), (4,4)]@@ -36,4 +37,13 @@ it "returns a singleton list if start and end are the same" $ bresenham (2 :: Int, 2) (2, 2) `shouldBe` [(2,2)]+ it "does not get confused with points on second quadrant" $+ bresenham (13 :: Int, 17) (12, 18) `shouldBe`+ [(13,17), (12,18)] + describe "bresenham (quickcheck)" $ do+ it "does never return an empty list" $ property $+ \(p1, p2) -> length (bresenham (p1 :: (Int, Int)) p2) /= 0+ it "is the same length with args flipped" $ property $+ \(p1, p2) -> length (bresenham (p1 :: (Int, Int)) p2) ==+ length (bresenham p2 p1)