Bitmap/Bresenham's line algorithm: Difference between revisions

Content deleted Content added
→‎{{header|Prolog}}: Corrected abbreviated code
→‎{{header|Prolog}}: Fixed small bug
Line 1,640: Line 1,640:
send(Win,open).
send(Win,open).


draw_recursive_line(_Pict,X,X,_DX,_DY,_Y,_D).%Don't iterate if X and X2 are the same number
draw_recursive_line(_Pict,X,X,_DX,_DY,Y,Y,_D,_Sx,_Sy).%Don't iterate if X and X2 are the same number
draw_recursive_line(Pict,X,X2,DX,DY,Y,C):-
draw_recursive_line(Pict,X,X2,DX,DY,Y,Y2,C,Sx,Sy):-
( C>0->%If the difference is greater than one, add Y one to Y.
( C>0->%If the difference is greater than one, add Y one to Y.
Y1 is Y+1,
Y1 is Y+Sy,
send(Pict,pixel(X,Y1,colour(black))),
send(Pict,pixel(X,Y1,colour(black))),
C2 is C+(2*DY-2*DX);
C2 is C+(2*DY-2*DX);
Line 1,649: Line 1,649:
send(Pict,pixel(X,Y,colour(black))),
send(Pict,pixel(X,Y,colour(black))),
C2 is C+(2*DY)),
C2 is C+(2*DY)),
X0 is X+1,%The next iteration
X0 is X+Sx,%The next iteration
draw_recursive_line(Pict,X0,X2,DX,DY,Y1,C2).
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)).

draw_line(Pict,X1,Y1,X2,Y2):-
draw_line(Pict,X1,Y1,X2,Y2):-
DY is Y2-Y1,
DY is abs(Y2-Y1),
DX is X2-X1,
DX is abs(X2-X1),
isneg(DX,Sx),
isneg(DY,Sy),
D = 2*DY-DX,%The slope of the line
D = 2*DY-DX,%The slope of the line
draw_recursive_line(Pict,X1,X2,DX,DY,Y1,D).
draw_recursive_line(Pict,X1,X2,DX,DY,Y1,Y2,D,Sx,Sy).
</lang>
</lang>