Swi Prolog- Syntax Error, Operator Priority Clash
I'm Trying to Create a Function That Checks If a Square Is Intersecting a Rectangle. I Have Multiple Test Cases, Just Showing First One, but Even for the First...
I'm trying to create a function that checks if a square is intersecting a rectangle. I have multiple test cases, just showing first one, but even for the first test case I keep getting an "operator priority clash" error. This is resulting in every case being returned as false, even though I double checked my math and it should return true for this first case. What am I doing wrong?
intersect(
square(point2d(X1,Y1),LENGTH),
rectangle(point2d(X2,Y2),point2d(X3,Y3))) :-
X1 =< X2 =< (X1 + LENGTH),
(Y1-LENGTH) =< Y2 =< Y1.
1 Answer
Arithmetic operators behaviour is not specific of SWI-Prolog. As you may expect, they are binary built in predicates that evaluate their arguments as arithmetic expressions.
You have to explicit the range checking: for instance
intersect(
square(point2d(X1,Y1),LENGTH),
rectangle(point2d(X2,Y2),point2d(X3,Y3))
) :-
X1 =< X2, X2 =< (X1 + LENGTH), (Y1-LENGTH) =< Y2, Y2 =< Y1.