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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With