Replace bresenham with digitalLine

This commit is contained in:
jgk
2021-03-16 00:34:54 +01:00
parent f939535657
commit 24aede4cac
2 changed files with 18 additions and 5 deletions
+13
View File
@@ -297,6 +297,7 @@ divideLine x a b = take 5000 $ map (\i -> a +.+ (i / (fromIntegral numPoints) *.
-- pulled the following from the haskell wiki
-- it seems to produce an infinite loop sometimes
bresenham :: (Int,Int) -> (Int,Int) -> [(Int,Int)]
{-# INLINE bresenham #-}
bresenham pa@(xa,ya) pb@(xb,yb) = map maySwitch . unfoldr go $ (x1,y1,0)
@@ -316,6 +317,18 @@ bresenham pa@(xa,ya) pb@(xb,yb) = map maySwitch . unfoldr go $ (x1,y1,0)
then (yTemp+ystep,tempError-deltax)
else (yTemp,tempError)
digitalLine :: (Int,Int) -> (Int,Int) -> [(Int,Int)]
digitalLine (x1,y1) (x2,y2)
| abs (x1-x2) > abs (y1-y2) = [ (x,( (y1-y2) * x + x1*y2 - x2*y1) `rdiv` (x1-x2) )
| x <- intervalList x1 x2 ]
| otherwise = [ ( ((x1-x2) * y + y1*x2 - y2*x1) `rdiv` (y1-y2) , y)
| y <- intervalList y1 y2 ]
where rdiv a b = round $ fromIntegral a / fromIntegral b
intervalList :: Int -> Int -> [Int]
intervalList x y
| y >= x = [x .. y]
| otherwise = reverse [y..x]
divideCircle :: Float -> Point2 -> Float -> [Point2]
divideCircle x cen rad = map (cen +.+) $ nPointsOnCirc n rad