×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Prolog help

Prolog help

Prolog help

(OP)

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), !.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources