brunogsimoes
Computer
- Dec 29, 2004
- 1
the code splits an matrix into an sets of zones.
e.g the X = 1 and Y = 1 is ID = X + Y * 10 = 11 and it is an corner so we add it to corners list.
We repete steps until the end of the matrix
how can i improve this code?
split :-
retractall(corner(_)),
retractall(outer_corner(_)),
retractall(outer_corner2(_)),
retractall(edge(_)),
retractall(inner_square(_)),
retractall(outer_square(_)),
dimension(Dim), % Dimension of matrix
corners(Dim),
cbz(1, 1, Dim).
corners(Dim) :-
ID1 is 11,
ID2 is 10 + Dim,
ID3 is 1 + Dim * 10,
ID4 is Dim + Dim * 10,
assertz(corner(ID1)),
assertz(corner(ID2)),
assertz(corner(ID3)),
assertz(corner(ID4)).
cbz(_, Y, Dim) :- Y is Dim + 1, !.
cbz(X, Y, Dim) :-
ID is X + Y * 10,
corner(ID), !,
increment(X, Y, Dim, 1, NewX, NewY), % Inc. coords by 1
cbz(NewX, NewY, Dim), !.
cbz(X, Y, Dim) :-
ID is X + Y * 10,
ID1 is ID + 1,
ID2 is ID - 1,
ID3 is ID + 10,
ID4 is ID - 10,
(corner(ID1);
corner(ID2);
corner(ID3);
corner(ID4)), !,
assertz(outer_corner(ID)),
increment(X, Y, Dim, 1, NX,NY),
cbz(NX, NY, Dim), !.
cbz(X, Y, Dim) :-
ID is X + Y * 10,
ID1 is ID + 11,
ID2 is ID - 11,
ID3 is ID + 9,
ID4 is ID - 9,
(corner(ID1);
corner(ID2);
corner(ID3);
corner(ID4)), !,
assertz(outer_corner2(ID)),
increment(X, Y, Dim, 1 ,NX,NY),
cbz(NX, NY, Dim), !.
cbz(X, Y, Dim) :-
X > 2, X < Dim - 1,
( Y = 1 ; Y = Dim ), !,
ID is X + Y * 10,
assertz(edge(ID)),
increment(X,Y,Dim,1,NX,NY),
cbz(NX, NY, Dim), !.
cbz(X, Y, Dim) :-
Y > 2, Y < Dim - 1,
( X = 1 ; X = Dim ), !,
ID is X + Y * 10,
assertz(edge(ID)),
increment(X,Y,Dim,1,NX,NY),
cbz(NX, NY, Dim), !.
cbz(X, Y, Dim) :-
X > 2, X < Dim - 1,
Y > 2, Y < Dim - 1, !,
ID is X + Y * 10,
assertz(inner_square(ID)),
increment(X,Y,Dim,1,NX,NY),
cbz(NX, NY, Dim), !.
cbz(X, Y, Dim) :-
X > 2, X < 7, ( Y = 2 ; Y = 7 ),
ID is X + Y * 10,
assertz(outer_square(ID)),
increment(X,Y,Dim,1,NX,NY),
cbz(NX, NY, Dim), !.
cbz(X, Y, Dim) :-
Y > 2, Y < 7, ( X = 2 ; X = 7 ),
ID is X + Y * 10,
assertz(outer_square(ID)),
increment(X,Y,Dim,1,NX,NY),
cbz(NX, NY, Dim), !.