Skip to content

Instantly share code, notes, and snippets.

@reinh
Last active December 23, 2015 23:29
Show Gist options
  • Save reinh/6710417 to your computer and use it in GitHub Desktop.
Save reinh/6710417 to your computer and use it in GitHub Desktop.
-- | A scan-conversion algorithm for drawing lines on integral coordinate
-- systems. Returns the list of coordinates that should be filled to draw
-- the line from point to point.
line :: (Integral a) => V2 a -> V2 a -> [V2 a]
line p p' = fmap near [0..end] where
end = case abs (p - p') of V2 dx dy -> max dx dy
near i = round <$> lerp (i % end)
(fmap fromIntegral p')
(fmap fromIntegral p)
-- From Linear.Vector
-- | Linearly interpolate between two vectors.
lerp :: Num a => a -> f a -> f a -> f a
lerp alpha u v = alpha *^ u ^+^ (1 - alpha) *^ v
> printLine $ line (V2 0 0) (V2 13 25)
◼ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◼ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◼ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◼ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◼ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◼ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◼ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◼ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◼ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◼ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◼ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◼ ◻ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◼ ◻ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◻ ◼ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◻ ◼ ◻ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◼ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◼ ◻ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◼ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◼ ◻ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◼ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◼ ◻ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◼ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◼ ◻ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◼ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◼ ◻
◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◻ ◼

Scan-converted Line

Computer Graphics – Practices and Principles 2ed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment