Reputation: 1
Any examples on how to create rules in prolog for equilateral and isosceles triangle in 2d point?
Start with these:
equilateral(point2d(x,y), point2d(x,y), point2d(x,y))
isosceles(point2d(x,y), point2d(x,y), point2d(x,y))
Upvotes: 0
Views: 346
Reputation: 2662
To check if the triangle is equilateral, it's quite simple. To check if it is isosceles, it's a little less trivial. Here my solution:
seg_length(X1,Y1,X2,Y2,D):-
D is sqrt(((X2-X1)**2)+((Y2-Y1)**2)).
equilateral(point2d(X1,Y1), point2d(X2,Y2), point2d(X3,Y3)):-
seg_length(X1,Y1,X2,Y2,D),
seg_length(X2,Y2,X3,Y3,D),
seg_length(X3,Y3,X1,Y1,D).
isosceles(point2d(X1,Y1), point2d(X2,Y2), point2d(X3,Y3)):-
seg_length(X1,Y1,X2,Y2,D1),
seg_length(X2,Y2,X3,Y3,D2),
seg_length(X3,Y3,X1,Y1,D3),
L = [D1,D2,D3],
sort(L,LS),
length(L,LenL),
length(LS,LenLS),
LenL \== LenLS.
To check if at least two sides are equal, i use sort/2
which removes duplicates from the list. If the two lists have the same lenght, it means that all three sides are different and so the triangle is not isosceles.
Upvotes: 1