# Рисование

## Задача 1

Напишем программу для рисования трёх прямоугольников.

Прямоугольники соединены стрелками.

```
:- pce_global(@in_out_link, make_in_out_link).
make_in_out_link(L) :- new(L, link(in, out, line(arrows := second))).

true.

createImage :- new(P, picture('My Image')), addBoxes(P).

addOneBox(P, X, Y, W, H, B) :- send(P, display, new(B, box(W, H)), point(X, Y)), true.

addBoxes(P) :- send(P, open),
               addOneBox(P, 100, 50, 100, 200, B1),
               addOneBox(P, 300, 50, 100, 200, B2),
               addOneBox(P, 500, 50, 100, 200, B3),
               connectBoxes(B1, B2),
               connectBoxes(B2, B3).

connectBoxes(BOX_1, BOX_2) :- send(BOX_1, handle, handle(w/2, h/2, in)),
                              send(BOX_2, handle, handle(0, 0, out)),
                              send_list([BOX_1, BOX_2], recogniser, new(move_gesture)),
                              send(BOX_1, connect, BOX_2, @in_out_link).

start :- createImage.
```

Запускаем программу.

```
start.
```

## Задача 2

Создадим программу для рисования схемы.

Создадим файл **module.pl** с описанием элементов схемы и их связями.

Пишем в файле:

```
:- module(module, []).

element(0, 1, 1180, 150, 150).
element(1, 1, 2740, 250, 50).
element(2, 1, 1270, 350, 250).

element(3, 2, 4500, 50, 150).
element(4, 2, 3500, 250, 250).                         

connected(0, 3, 0).
connected(1, 0, 1).
connected(2, 0, 4).
connected(3, 4, 2).
```

Создадим файл **myprog.pl** с основным кодом программы.

Пишем в файле:

```
:- pce_global(@in_out_link, make_in_out_link), use_module(module, []).

listing.
:- dynamic a/2.

true.

savePair(K, V) :- assert(a(K, V)); true.

make_in_out_link(L) :- new(L, link(in, out, line(arrows := second))).

addOneResistor(P, X, Y, B) :- send(P, display, new(B, box(50, 50)), point(X, Y)), true.

addOneCondensator(P, X, Y, B) :- send(P, display, new(B, box(50, 50)), point(X, Y)),
                                 X1 is (X + 10),
                                 Y1 is (Y + 10),
                                 send(P, display, new(B_11, box(10, 30)), point(X1, Y1)), 
                                 X2 is (X + 30),
                                 Y2 is (Y + 10),
                                 send(P, display, new(B_22, box(10, 30)), point(X2, Y2)), 
                                 true.

addOneBox(P, X, Y, B, TYPE, VALUE) :- YY is (Y - 25), writeLabel(P, X, YY, VALUE), TYPE =:= 1, addOneResistor(P, X, Y, B); addOneCondensator(P, X, Y, B).

connectBoxes(BOX_1, BOX_2) :- send(BOX_1, handle, handle(w, h/2, in)),
                              send(BOX_2, handle, handle(0, h/2, out)),
                              send_list([BOX_1, BOX_2], recogniser, new(move_gesture)),
                              send(BOX_1, connect, BOX_2, @in_out_link).

writeElement(NUMBER, TYPE, VALUE, X, Y, P) :- write("Number: "), write(NUMBER), write("  "),
                                           write("Type: "), writeType(TYPE), write("  "),
                                           write("Value: "), write(VALUE), write("  "),
                                           write("Position: "), write("("), write(X), write(", "), write(Y), write(")"), nl, 
                                           addOneBox(P, X, Y, B, TYPE, VALUE),
                                           savePair(NUMBER, B),
                                           true.

writeType(T) :- T =:= 1, write("Resistor "); write("Condensator ").

writeAllElements(N, P) :- module:element(N, TYPE, VALUE, X, Y), writeElement(N, TYPE, VALUE, X, Y, P), NN is (N + 1), writeAllElements(NN, P); true.

writeAllElementsWithLabel(P) :- nl, write("Elements:"), nl, nl, writeAllElements(0, P), nl.

writeAllConnections(N, P) :- module:connected(N, NUMBER_1, NUMBER_2),
                          module:element(NUMBER_1, TYPE_1, VALUE_1, X_1, Y_1),
                          module:element(NUMBER_2, TYPE_2, VALUE_2, X_2, Y_2),
                          renderConnection(NUMBER_1, NUMBER_2),
                          writeType(TYPE_1), write(NUMBER_1), write(" connected with "), writeType(TYPE_2), write(NUMBER_2), nl,
                          NN is (N + 1),
                          writeAllConnections(NN, P); true.

renderConnection(N1, N2) :- a(N1, B1), a(N2, B2),
                            connectBoxes(B1, B2);
                            true.

writeAllConnectionsWithLabel(P) :- write("Connections:"), nl, nl, writeAllConnections(0, P), nl, nl.

createImage(P) :- new(P, picture('Content')); true.

writeLabel(P, X, Y, TEXT) :- send(P, display, new(LABEL, label_box(TEXT)), point(X, Y)), true.

start :- createImage(P), send(P, open), writeAllElementsWithLabel(P), writeAllConnectionsWithLabel(P).

run :- start.
```

Запускаем программу.

```
swipl -s myprog.pl
```

Далее прописываем:

```
run.
```
