/** gridline.pl Metaphysics > Exposition Description From quote "first half: describe single walking". Draws a line on a grid from one cell to another, describing walking as an example of an exposition. ?- gridline1(0,0,0,3). * * * * true. ?- gridline1(0,0,1,3). * * * * true. ?- gridline1(0,0,3,3). * * * * true. ?- gridline1(0,0,3,1). ** ** true. ?- gridline1(0,0,3,0). **** true. ?- gridline1(0,0,3,-1). ** ** true. ?- gridline1(0,0,3,-3). * * * * true. ?- gridline1(0,0,1,-3). * * * * true. ?- gridline1(0,0,0,-3). * * * * true. ?- gridline1(0,0,-1,-3). * * * * true. ?- gridline1(0,0,-3,-3). * * * * true. ?- gridline1(0,0,-3,-1). ** ** true. ?- gridline1(0,0,-3,0). **** true. ?- gridline1(0,0,-3,1). ** ** true. ?- gridline1(0,0,-3,3). * * * * true. ?- gridline1(0,0,-1,3). * * * * true. **/ %% Graphs (X1,Y1) to (X2,Y2) gridline1(X1,Y1,X2,Y2) :- sortbyx(X1,Y1,X2,Y2,XA1,YA1,XA2,YA2), equation(XA1,YA1,XA2,YA2,M,C), gridline2(XA1,YA1,XA2,YA2,M,C,Grid1), writeln(Grid1), sort(YA1,YA2,YB1,YB2), print(XA1,YB1,XA2,YB2,Grid1,_Grid2),!. %% Sorts (X1,Y1) and (X2,Y2) by X sortbyx(X1,Y1,X2,Y2,X1,Y1,X2,Y2) :- X2 >= X1. sortbyx(X1,Y1,X2,Y2,X2,Y2,X1,Y1) :- X2 < X1. %% Finds the rise and run of (X1,Y1) and (X2,Y2) equation(X1,Y1,X2,Y2,M,C) :- DY is Y2-Y1, DX is X2-X1, equation2(DY,DX,M,Y1,X1,C). %% Finds the gradient m and y-intercept c of (X1,Y1) and (X2,Y2) equation2(_DY,0,999999999,_Y1,X1,X1) :- !. equation2(DY,DX,M,Y1,X1,C) :- M is DY/DX, C is Y1-M*X1. %% Finds the graph of the line connecting the two points. It does this by finding the graph flipped in the y=x line if the gradient m is greater than 1 or less than -1, so that the graph is not disjointed gridline2(X1,_Y1,X2,_Y2,M,C,Grid) :- M =< 1, M >= -1, gridline3(X1,X2,M,C,[],Grid). gridline2(X1,Y1,_X2,Y2,M,_C,Grid) :- (M > 1; M < -1), M2 is 1/M, sort(Y1,Y2,YA1,YA2), C2 is X1-M2*Y1, gridline3(YA1,YA2,M2,C2,[],Grid1), flipxy(Grid1,[],Grid). %% Sorts Y1 and Y2 sort(Y1,Y2,Y1,Y2) :- Y1=Y2. %% Plots a point at each x-value of the graph gridline3(X1,X2,_M,_C,Grid,Grid) :- X1 is X2+1. gridline3(X1,X2,M,C,Grid1,Grid2) :- Y is round(M*X1+C), Coord = [X1,Y], append(Grid1,[Coord],Grid3), X3 is X1+1, gridline3(X3,X2,M,C,Grid3,Grid2). %% Flips the graph in the y=x line flipxy([],Grid,Grid) :- !. flipxy(Grid1,Grid2,Grid3) :- Grid1 = [Coord1 | Coords], Coord1 = [X, Y], Coord2 = [Y, X], append(Grid2,[Coord2],Grid4), flipxy(Coords,Grid4,Grid3). %% Prints the graph from the top row to the bottom row print(_X1,Y1,_X2,YA1,Grid,Grid) :- Y11 is Y1,YA1 < Y11,!. print(X1,Y1,X2,Y2,Grid1,Grid2) :- printline(X1,X2,Y2,Grid1,Grid3), writeln(''), YA2 is Y2-1, print(X1,Y1,X2,YA2,Grid3,Grid2). %% Prints the current row printline(XA2,X2,_Y,Grid,Grid) :- XA2 is X2+3, !. printline(X1,X2,Y,Grid1,Grid2) :- member([X1,Y],Grid1), delete(Grid1,[X1,Y],Grid3), write('*'), XA1 is X1+1, printline(XA1,X2,Y,Grid3,Grid2). printline(X1,X2,Y,Grid1,Grid2) :- not(member([X1,Y],Grid1)), write(' '), XA1 is X1+1, printline(XA1,X2,Y,Grid1,Grid2).