Loading [MathJax]/jax/output/HTML-CSS/jax.js

Lesson goal: Looking for rectangles

Previous: Looking for right triangles | Home | Next: Guess your own trendline

Continuing to work with the set of (x,y) points from this lesson, and in the spirit of this lesson let's look for a rectangle amongst the points.

A rectangle needs four points and four lines, with pairs of connecting lines being perpendicular.

Take a look at the goal below.

Now you try. Run the goal and see if Prolog finds a rectangle in the points.

Type your code here:

32
 
1
point(-4,4).
2
point(-3,2).
3
point(-1,1).
4
point(0,0).
5
point(-3,0).
6
point(1,3).
7
point(3,3).
8
point(-4,-4).
9
point(3,-3).
10
11
line([(X1,Y1),(X2,Y2)]) :- point(X1,Y1), point(X2,Y2), [X1,Y1] \= [X2,Y2].
12
13
horiz([(_,Y),(_,Y)]).
14
15
vert([(X,_),(X,_)]).
16
17
is_neg1(X) :- D is abs(X+1), D < 0.1.
18
19
perp(L1,L2) :- horiz(L1), vert(L2).
20
perp(L1,L2) :- vert(L1), horiz(L2).
21
perp(L1,L2) :- \+ vert(L1), \+ vert(L2), slope(L1,M1), slope(L2,M2), P is M1*M2, is_neg1(P).
22
23
slope([(X,_),(X,_)],infinite) :- !.
24
slope([(X1,Y1),(X2,Y2)],M) :- M is (Y2-Y1)/(X2-X1).
25
26
goal:   line([P1,P2]), 
27
        line([P2,P3]), 
28
        line([P3,P4]), 
29
        line([P4,P1]), 
30
        perp([P1,P2],[P2,P3]), 
31
        perp([P2,P3],[P3,P4]), 
32
        perp([P3,P4],[P4,P1]).

See your results here: