How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the Elm programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the Elm programming language
Table of Contents
Problem Statement
Using the data storage type defined on the Bitmap page for raster graphics images, draw a line given two points with Bresenham's line algorithm.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the Elm programming language
Source code in the elm programming language
-- Brensenham Line Algorithm
type alias Position =
{x: Int, y: Int}
type alias BresenhamStatics =
{ finish : Position
, sx : Int
, sy : Int
, dx : Float
, dy : Float
}
line : Position -> Position -> List Position
line p q =
let
dx = (toFloat << abs) (q.x - p.x)
dy = (toFloat << abs) (q.y - p.y)
sx = if p.x < q.x then 1 else -1
sy = if p.y < q.y then 1 else -1
error =
(if dx > dy then dx else -dy) / 2
statics =
BresenhamStatics q sx sy dx dy
in
bresenhamLineLoop statics error p []
bresenhamLineLoop : BresenhamStatics -> Float -> Position -> List Position -> List Position
bresenhamLineLoop statics error p positions =
let
positions_ = p :: positions
{sx, sy, dx, dy, finish} = statics
in
if (p.x == finish.x) && (p.y == finish.y) then
positions_
else
let
(dErrX, x) =
if error > -dx then (-dy, sx + p.x)
else (0, p.x)
(dErrY, y) =
if error < dy then (dx, sy + p.y)
else (0, p.y)
error_ = error + dErrX + dErrY
in
bresenhamLineLoop statics error_ (Position x y) positions_
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort with shifting bounds step by step in the VBA programming language
You may also check:How to resolve the algorithm Random numbers step by step in the ReScript programming language
You may also check:How to resolve the algorithm Word wheel step by step in the AWK programming language
You may also check:How to resolve the algorithm Comments step by step in the Lingo programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the Neko programming language