Anonymous user
Bitmap/Bresenham's line algorithm: Difference between revisions
→{{header|Prolog}}
m (→version 1: changed wording in the REXX section header.) |
|||
Line 2,988:
=={{header|Prolog}}==
<lang Prolog>
:- use_module(
:- use_module(bitmapIO).
:- use_module(library(clpfd)).
set_pixel0(NPict,Pict,[X,Y],Color).
draw_line(Pix,X1,Y1,X2,Y2),▼
set_pixel0(TPict,Pict,[X,Y],Color),
E2 #= 2*E,
% 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
Ey = 1, NX #= X + Sx;
Ey = 0, NX = X),
Ex = 1, NY #= Y + Sy;
Ex = 0, NY = Y),
NE #= E + DX*Ex + DY*Ey,
▲draw_recursive_line(Pict,X,X2,DX,DY,Y,Y2,C,Sx,Sy):-
( DeltaY < 0 -> Sy = -1; Sy = 1),
( DeltaX < 0 -> Sx = -1; Sx = 1),
DX #= abs(DeltaX),
DY #= -1*abs(DeltaY),
E #= DY+DX,
draw_recursive_line(NPict,Pict,Color,X1,X2,DX,DY,Y1,Y2,E,Sx,Sy).
▲ draw_recursive_line(Pict,X0,X2,DX,DY,Y1,Y2,C2,Sx,Sy).
▲ ( X<0->
▲ ( X\==0->
init:-
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).
▲ draw_recursive_line(Pict,X1,X2,DX,DY,Y1,Y2,D,Sx,Sy).
</lang>
|