
% shape(S)
% --------
% succeeds iff S is a valid shape
shape(circle(X,Y,Z)) :- circle(X,Y,Z).
shape(box(L,R,T,B)) :- box(L,R,T,B).

% circle(X,Y,R)
% -------------
% succeeds iff the item represents a valid circle
%    x,y represents the centrepoint of a circle on an x,y plane
%    r represents the radius of the circle
circle(X,Y,R) :- number(X), number(Y), number(R), R > 0.

% box(Left,Right,Top,Bottom)
% --------------------------
% succeeds iff the item represents a valid box
%   where the box edges are parallel to the x/y planes
%   left,right represent the x coordinates of the left/right edges
%   top,bottom represent the y coordinates of the top/bottom edges
box(Left,Right,Top,Bottom) :-
   number(Left), number(Right), Left < Right,
   number(Top), number(Bottom), Bottom < Top.

% area(S,A)
% ---------
% succeeds iff A is the area of shape S
area(circle(_,_,R),A) :- Area is 3.14 * R * R, A = Area.
area(box(L,R,T,B),A) :- H is R - L, V is T - B, Area is H * V, A = Area.

% distance(X1,Y1,X2,Y2,D)
% -----------------------
% succeeds iff D can be unified with the distance between the two points
distance(X1,Y1,X2,Y2,D) :- number(X1), number(X2), number(Y1), number(Y2),
   Dist is sqrt((X1-X2)*(X1-X2) + (Y1-Y2)*(Y1-Y2)), D = Dist.

% distance(C1, C2, D)
% ---------------------
% succeeds iff D can be unified with the distance between the centres of the two circles
distance(circle(X1,Y1,R1),circle(X2,Y2,R2),D) :-
   circle(X1,Y1,R1), circle(X2,Y2,R2), distance(X1,Y1,X2,Y2,D).

% overlap(C1,C2)
% --------------
% succeeds iff the two circles overlap
overlap(C1, C2) :- C1 = circle(_,_,R1), C2 = circle(_,_,R2),
    distance(C1,C2,D), D =< (R1+R2).

