Bitmap/Bresenham's line algorithm: Difference between revisions

m (→‎version 1: changed wording in the REXX section header.)
Line 2,988:
 
=={{header|Prolog}}==
 
Works with SWI-prolog.
 
<lang Prolog>
:- use_module(library(pce)bitmap).
:- use_module(bitmapIO).
lindraw(X1,Y1,X2,Y2):-
:- use_module(library(clpfd)).
new(Win,window("Line")),
 
new(Pix,pixmap(@nil,black,white,X2+30,Y2+30)),
draw_recursive_line(NPict,Pict,Color,X,X2X,DX_DX,DY_DY,Y,Y2Y,C_E,Sx_Sx,Sy_Sy):-
send(Win,size,size(400,400)),
set_pixel0(NPict,Pict,[X,Y],Color).
draw_line(Pix,X1,Y1,X2,Y2),
draw_recursive_line(NPict,Pict,X1Color,X,X2,DX,DY,Y1Y,Y2,DE,Sx,Sy).:-
new(Bmp,bitmap(Pix)),
set_pixel0(TPict,Pict,[X,Y],Color),
send(Win,display,Bmp,point(0,0)),
E2 #= 2*E,
send(Win,open).
% because we can't accumulate error we set Ey or Ex to 1 or 0
% depending on whether we need to add dY or dX to the error term
( X\E2 >==0 DY ->
Ey = 1, NX #= X + Sx;
Ey = 0, NX = X),
( XE2 =<0 DX ->
Ex = 1, NY #= Y + Sy;
Ex = 0, NY = Y),
NE #= E + DX*Ex + DY*Ey,
draw_recursive_line(PictNPict,X0TPict,Color,NX,X2,DX,DY,Y1NY,Y2,C2NE,Sx,Sy).
 
draw_line(PixNPict,Pict,Color,X1,Y1,X2,Y2),:-
draw_recursive_line(_Pict,X,X,_DX,_DY,Y,Y,_D,_Sx,_Sy).%Don't iterate if X and X2 are the same number
DYDeltaY is#= abs(Y2-Y1),
draw_recursive_line(Pict,X,X2,DX,DY,Y,Y2,C,Sx,Sy):-
DXDeltaX is#= abs(X2-X1),
( C>0->%If the difference is greater than one, add Y one to Y.
( DeltaY < 0 -> Sy = -1; Sy = 1),
Y1 is Y+Sy,
( DeltaX < 0 -> Sx = -1; Sx = 1),
send(Pict,pixel(X,Y1,colour(black))),
DX #= abs(DeltaX),
C2 is C+(2*DY-2*DX);
DY #= -1*abs(DeltaY),
Y1 is Y,
E #= DY+DX,
send(Pict,pixel(X,Y,colour(black))),
draw_recursive_line(NPict,Pict,Color,X1,X2,DX,DY,Y1,Y2,E,Sx,Sy).
C2 is C+(2*DY)),
X0 is X+Sx,%The next iteration
draw_recursive_line(Pict,X0,X2,DX,DY,Y1,Y2,C2,Sx,Sy).
isneg(X,O):-
( X<0->
 
O is -1;
( X\==0->
O is 1;
O is 0)).
 
init:-
draw_line(Pict,X1,Y1,X2,Y2):-
new_bitmap(B,[100,100],[255,255,255]),
DY is abs(Y2-Y1),
draw_line(NB,B,[0,0,0],2,2,10,90),
DX is abs(X2-X1),
write_ppm_p6('line.ppm',NB).
isneg(DX,Sx),
isneg(DY,Sy),
D = 2*DY-DX,%The slope of the line
draw_recursive_line(Pict,X1,X2,DX,DY,Y1,Y2,D,Sx,Sy).
</lang>
 
Anonymous user