⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 squares.pl

📁 PRl教学程序 PRl教学程序 PRl教学程序
💻 PL
字号:
/********************************************************************
  CGLIB: Constraint-based Graphical Programming in B-Prolog
  %
  draw a chessboard of squares
*********************************************************************/
go:-
    squares(Os),
    cgJava(squares,Os).

squares(Spirals):-
    N = 4,
    length(Squares,N),
    createSquares(N,Squares),
    cgGrid(Squares),
    listlist2list(Squares,SquaresList),
    cgPack(SquaresList), % pack the bounding squares first
    spirals(Squares,2,8,Spirals),!.

listlist2list([],List):-List=[].
listlist2list([X|Xs],List):-
    listlist2list(Xs,List1),
    append(X,List1,List).
    
createSquares(N,[]).
createSquares(N,[X|Xs]):-
    length(X,N),
    cgSquares(X),
    cgSame(X,width,Width),Width #= 50,
    cgSame(X,fill,0),
    createSquares(N,Xs).

spirals([],I,J,Spirals):-Spirals=[].
spirals([List|Lists],I,J,Spirals):-
    spirals1(List,I,J,Spirals,SpiralsR),
    spirals(Lists,J,I,SpiralsR).
    
spirals1([],I,J,Spirals,SpiralsR):-Spirals=SpiralsR.
spirals1([S|List],I,J,Spirals,SpiralsR):-
    spiral(10,I,J,S,Spirals,Spirals1),
    spirals1(List,J,I,Spirals1,SpiralsR).

spiral(N,I,J,R,Spirals,SpiralsR):-
    Spirals=[P|Spirals1],
    rectToPoly(R,P),
    cgPack(P),
    spiral1(N,I,J,P,Spirals1,SpiralsR).

spiral1(N,I,J,P,Spirals,SpiralsR):-N=:=0,!,Spirals=SpiralsR.
spiral1(N,I,J,P,Spirals,SpiralsR):-
    Spirals=[NewP|Spirals1],
    rotatePoly(P,I,J,NewP),
    cgPack(NewP),
    N1 is N-1,
    spiral1(N1,I,J,NewP,Spirals1,SpiralsR).
    
rectToPoly(S,P):-
    cgPolygon(P), P^n #= 4, P^fill #= 0,
    P^point(0) #= S^leftTopPoint,
    P^point(1) #= S^leftBottomPoint,
    P^point(2) #= S^rightBottomPoint,
    P^point(3) #= S^rightTopPoint.

rotatePoly(P,I,J,NewP):-
    cgPolygon(NewP),
    NewP^n #= 4,
    NewP^fill #=0,
    NewP^x(0) #= I*P^x(0)//10 + J*P^x(1)//10,
    NewP^y(0) #= I*P^y(0)//10 + J*P^y(1)//10,
    NewP^x(1) #= I*P^x(1)//10 + J*P^x(2)//10,
    NewP^y(1) #= I*P^y(1)//10 + J*P^y(2)//10,
    NewP^x(2) #= I*P^x(2)//10 + J*P^x(3)//10,
    NewP^y(2) #= I*P^y(2)//10 + J*P^y(3)//10,
    NewP^x(3) #= I*P^x(3)//10 + J*P^x(0)//10,
    NewP^y(3) #= I*P^y(3)//10 + J*P^y(0)//10.
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -