0
% 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). 可能吗?

4

1 回答 1

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} .
...
于 2013-04-25T08:50:31.250 回答