Prolog找到变量

时间:2013-04-25 08:24:40

标签: prolog

% check if 2 squares colide by sides
col( X, Y, S, X2, Y2, S2):-
    EndX is X + S, EndY is Y + S,
    EndX2 is X2 + S2, EndY2 is Y2 + S2,
    (
    ( EndX =:= X2, ( (Y >= Y2, Y =< EndY2);(EndY >= Y2, EndY =< EndY2))); % left-l side
    ( X =:= X2, ( (Y >= Y2, Y =< EndY2);(EndY >= Y2, EndY =< EndY2))); % left-r side
    ( EndX =:= EndX2, ( (Y >= Y2, Y =< EndY2);(EndY >= Y2, EndY =< EndY2))); % right-l side
    ( X =:= EndX2, ( (Y >= Y2, Y =< EndY2);(EndY >= Y2, EndY =< EndY2))); % right-r side    
    ( EndY =:= Y2, ( (X >= X2, X =< EndX2);(EndX >= X2, EndX =< EndX2))); % top-t side
    ( Y =:= Y2, ( (X >= X2, X =< EndX2);(EndX >= X2, EndX =< EndX2))); % top-b side
    ( EndY =:= EndY2, ( (X >= X2, X =< EndX2);(EndX >= X2, EndX =< EndX2))); % bottom-t side
    ( Y =:= EndY2, ( (X >= X2, X =< EndX2);(EndX >= X2, EndX =< EndX2))) % bottom-b side
    ).

只有当我设置所有变量时,它才能正常工作,但我想问一下如何更改它以便我可以使用: col( 0,0,3,1,2,S).甚至col( 0, 0, 3, X,Y, S). 它甚至可能吗?

1 个答案:

答案 0 :(得分:1)

我想你可以试试library(clpqr)

:- [library(clpr)].

col( X, Y, S, X2, Y2, S2):- {
    EndX = X + S, EndY = Y + S,
    EndX2 = X2 + S2, EndY2 = Y2 + S2,
    (
    ( EndX = X2, ( (Y >= Y2, Y =< EndY2);(EndY >= Y2, EndY =< EndY2))); % left-l side
    ( X = X2, ( (Y >= Y2, Y =< EndY2);(EndY >= Y2, EndY =< EndY2))); % left-r side
    ( EndX = EndX2, ( (Y >= Y2, Y =< EndY2);(EndY >= Y2, EndY =< EndY2))); % right-l side
    ( X = EndX2, ( (Y >= Y2, Y =< EndY2);(EndY >= Y2, EndY =< EndY2))); % right-r side
    ( EndY = Y2, ( (X >= X2, X =< EndX2);(EndX >= X2, EndX =< EndX2))); % top-t side
    ( Y = Y2, ( (X >= X2, X =< EndX2);(EndX >= X2, EndX =< EndX2))); % top-b side
    ( EndY = EndY2, ( (X >= X2, X =< EndX2);(EndX >= X2, EndX =< EndX2))); % bottom-t side
    ( Y = EndY2, ( (X >= X2, X =< EndX2);(EndX >= X2, EndX =< EndX2))) % bottom-b side
    )
                }.

试验:

?- col( 0,0,3,1,2,S).
S = 2.0 ;
false.

有更多变量,你需要处理范围:

?- col( 0, 0, 3, X,Y, S).
X = 3.0,
{Y=< -0.0, _G6533>=0.0, S= -Y+_G6533, _G6550=3.0-Y+_G6533} ;
X = 3.0,
{Y=<3.0, _G8171>=3.0, S= -Y+_G8171, _G8188=3.0-Y+_G8171} ;
X = 0.0,
{Y=< -0.0, _G10154>=0.0, S= -Y+_G10154, _G10171= -Y+_G10154} .
...