line-drawing 0.1.0.0 → 0.2.0.0
raw patch · 5 files changed
+31/−22 lines, 5 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Line.Draw: type Coords = (Int, Int)
- Line.Draw: bresenham :: Coords -> Coords -> [Coords]
+ Line.Draw: bresenham :: Integral a => (a, a) -> (a, a) -> [(a, a)]
Files
- README +1/−0
- changes.txt +6/−0
- line-drawing.cabal +3/−2
- src/Line/Draw.hs +12/−14
- test/Line/DrawSpec.hs +9/−6
+ README view
@@ -0,0 +1,1 @@+Various https://en.wikipedia.org/wiki/Line_drawing_algorithm
changes.txt view
@@ -1,3 +1,9 @@+0.2.0.0+-------++- Made `bresenham` polymorphic.+- Released Sun 10 Nov 2019 15:13:29 CET.+ 0.1.0.0 -------
line-drawing.cabal view
@@ -1,6 +1,6 @@ cabal-version: >=1.10 name: line-drawing-version: 0.1.0.0+version: 0.2.0.0 synopsis: raster line drawing description: Line drawing algorithms to approximate a line segment on discrete graphical media (raster).@@ -14,7 +14,8 @@ copyright: © 2019 Francesco Ariis category: Graphics build-type: Simple-extra-source-files: changes.txt+extra-source-files: changes.txt,+ README library exposed-modules: Line.Draw
src/Line/Draw.hs view
@@ -1,4 +1,4 @@---------------------------------------------------------------------------------+------------------------------------------------------------------------------- -- | -- Module : Line.Draw -- Copyright : (C) 2019 Francesco Ariis@@ -18,24 +18,20 @@ -- [(0,0), (1,0), (2,1), (3,1), (4,2)] -- @ -----------------------------------------------------------------------------------+------------------------------------------------------------------------------- -module Line.Draw (- Coords,- bresenham- )+{-# Language ScopedTypeVariables #-}++module Line.Draw ( bresenham ) where import qualified Data.List as L import qualified Data.Tuple as T -type Coords = (Int, Int)-type State = (Int, Int, Int)- -- curr x, curr y, treshold -- | Rasterising a line using -- <https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm Bresenham's algorithm>.-bresenham :: Coords -> Coords -> [Coords]+bresenham :: Integral a => (a, a) -> (a, a) -> [(a, a)] bresenham x@(x1, y1) y@(x2, y2) | m1Check = bresenhamBase x y | otherwise = let x' = T.swap x@@ -50,28 +46,30 @@ -- ANCILLARIES -- ----------------- -bresenhamBase :: Coords -> Coords -> [Coords]+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) where -- slope- dx, dy, adx, ady :: Int+ dx, dy, adx, ady :: a dx = x2 - x1 dy = y2 - y1 adx = abs dx ady = abs dy -- sign of increment- ix, iy :: Int -> Int+ ix, iy :: a -> a ix | dx > 0 = (+1) | otherwise = (subtract 1) iy | dy > 0 = (+1) | otherwise = (subtract 1) -- step function, takes (x, y, treshold)- f :: State -> Maybe (Coords, State)+ -- 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 | otherwise =
test/Line/DrawSpec.hs view
@@ -13,21 +13,24 @@ describe "bresenham" $ do it "rasterises a simple line" $- bresenham (0, 0) (4, 4) `shouldBe`+ bresenham (0 :: Int, 0) (4, 4) `shouldBe` [(0,0), (1,1), (2,2), (3,3), (4,4)]+ it "rasterises a simple line starting from (1, 1)" $+ bresenham (1 :: Int, 1) (4, 4) `shouldBe`+ [(1,1), (2,2), (3,3), (4,4)] it "rasterises a line" $- bresenham (0, 0) (4, 2) `shouldBe`+ bresenham (0 :: Int, 0) (4, 2) `shouldBe` [(0,0), (1,0), (2,1), (3,1), (4,2)] it "rasterises a line, quadrant 2, m<1" $- bresenham (0, 0) (-4, 2) `shouldBe`+ bresenham (0 :: Int, 0) (-4, 2) `shouldBe` [(0,0), (-1,0), (-2,1), (-3,1), (-4,2)] it "rasterises a line, quadrant 3, m<1" $- bresenham (0, 0) (-4, -2) `shouldBe`+ bresenham (0 :: Int, 0) (-4, -2) `shouldBe` [(0,0), (-1,0), (-2,-1), (-3,-1), (-4,-2)] it "rasterises a line, quadrant 1, m>1" $- bresenham (0, 0) (2, 4) `shouldBe`+ bresenham (0 :: Int, 0) (2, 4) `shouldBe` [(0,0), (0,1), (1,2), (1,3), (2,4)] it "rasterises a line, quadrant 4, m>1" $- bresenham (0, 0) (2, -4) `shouldBe`+ bresenham (0 :: Int, 0) (2, -4) `shouldBe` [(0,0), (0,-1), (1,-2), (1,-3), (2,-4)]