Bitmap/Bresenham's line algorithm: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: minor tidy)
Line 2,795: Line 2,795:
=={{header|Phix}}==
=={{header|Phix}}==
Modified copy of [[Bitmap/Bresenham%27s_line_algorithm#Euphoria|Euphoria]], with a bigger bitmap and a simpler pattern.
Modified copy of [[Bitmap/Bresenham%27s_line_algorithm#Euphoria|Euphoria]], with a bigger bitmap and a simpler pattern.
Requires new_image() from [[Bitmap#Phix|Bitmap]], write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write_a_PPM_file]].
Requires new_image() from [[Bitmap#Phix|Bitmap]], write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write_a_PPM_file]]. <br>
Included as demo\rosetta\Bresenham_line.exw, results may be verified with demo\rosetta\viewppm.exw
Note that demo\rosetta\Bresenham_line.exw is just the last 6 lines below preceded by include ppm.e since that contains bresLine() which is also used by several other examples, and covers the above requirements, as shown commented out. Results may be verified with demo\rosetta\viewppm.exw
<lang Phix>-- demo\rosetta\Bresenham_line.exw (runnable version)
<lang Phix>function bresLine(sequence screenData, integer x0, integer y0, integer x1, integer y1, integer colour)
-- The line algorithm
integer deltaX = abs(x1-x0),
deltaY = abs(y1-y0),
stepX = iff(x0<x1,1,-1),
stepY = iff(y0<y1,1,-1),
lineError = iff(deltaX>deltaY,deltaX,-deltaY),
prevle


global function bresLine(sequence image, integer x0, y0, x1, y1, colour)
lineError = round(lineError/2,1)
while 1 do
-- The line algorithm
if x0>=1 and x0<=length(screenData)
integer dimx = length(image),
and y0>=1 and y0<=length(screenData[x0]) then
dimy = length(image[1]),
screenData[x0][y0] = colour
deltaX = abs(x1-x0),
deltaY = abs(y1-y0),
stepX = iff(x0<x1,1,-1),
stepY = iff(y0<y1,1,-1),
lineError = iff(deltaX>deltaY,deltaX,-deltaY),
prevle
lineError = round(lineError/2, 1)
while true do
if x0>=1 and x0<=dimx
and y0>=1 and y0<=dimy then
image[x0][y0] = colour
end if
end if
if x0=x1 and y0=y1 then exit end if
if x0=x1 and y0=y1 then exit end if
Line 2,823: Line 2,826:
end if
end if
end while
end while
return screenData
return image
end function
end function

--include ppm.e -- red, green, blue, white, new_image(), write_ppm(), bresLine() (as distributed, instead of the above)


sequence screenData = new_image(400,300,black)
sequence screenData = new_image(400,300,black)