/
list.cpp
244 lines (208 loc) · 6.25 KB
/
list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "visualize.h"
#include <cairomm/context.h>
#include <iostream>
const int List::nodes_size = 100;
List::List() {
list_x = 50;
list_y = 50;
out_x = list_x;
out_y = list_y + 100;
head = NULL;
nodes = new ListNode*[nodes_size];
currentNodes = 0;
}
List::~List() {
}
void List::newNode(ListNode * node) {
nodes[currentNodes++] = node;
}
void List::removeFromArray(ListNode * node) {
for (int i = 0; i < currentNodes; i++) {
if (nodes[i] == node) {
for (int j = i; j < currentNodes - 1; j++) {
nodes[j] = nodes[j + 1];
}
break;
}
}
currentNodes--;
}
void List::draw(const Cairo::RefPtr<Cairo::Context> & cr) {
// update the positions of the list
arrange_nodes();
// mark the array as unprinted
for (int i = 0; i < currentNodes; i++) {
nodes[i]->printed = false;
}
// print the head of the list
draw_labels(cr);
// print the nodes in the list
draw_connected(cr);
// print nodes disconnected from the list
draw_disconnected(cr);
}
// place the nodes that are part of the list in a single
// horizontal line
void List::arrange_nodes() {
ListNode * n = head;
int xTmp = list_x;
int yTmp = list_y;
while (n != NULL) {
xTmp += ListNode::padding;
n->x = xTmp;
n->y = yTmp;
n = n->next;
}
}
// draw the arrows from a specific node
void List::draw_arrows(const Cairo::RefPtr<Cairo::Context> & cr, ListNode * node) {
std::cout << "other test" << std::endl;
if (node->next != NULL) {
draw_arrow_helper(cr, node->x + ListNode::field_w - 10, node->y + (3 * ListNode::field_h) / 2, node->next->x - 10, node->next->y + ListNode::field_h);
} else {
draw_null_arrow(cr, node->x + ListNode::field_w - 10, node->y + (3 * ListNode::field_h) / 2, true);
}
}
void List::draw_arrow_helper(const Cairo::RefPtr<Cairo::Context> & cr, int start_x, int start_y, int end_x, int end_y) {
double m = (end_y - start_y) / (double ) (end_x - start_x);
double theta = atan(m);
int hyp = 5;
double angle;
if (end_x > start_x) {
angle = 5.0 * 3.14159 / 6.0;
} else {
angle = 3.14159 / 6.0;
}
cr->set_line_width(2.0);
// arrow body
cr->move_to(start_x, start_y);
cr->line_to(end_x, end_y);
// arrow head
cr->line_to(end_x + hyp * cos(theta + angle), end_y + hyp * sin(theta + angle));
cr->line_to(end_x + hyp * cos(theta - angle), end_y + hyp * sin(theta - angle));
cr->line_to(end_x, end_y);
cr->stroke();
}
void List::draw_null_arrow(const Cairo::RefPtr<Cairo::Context> & cr, int start_x, int start_y, bool right) {
int dir = right ? 1 : -1;
// draw the body of the arrow
cr->set_line_width(2.0);
cr->move_to(start_x, start_y);
cr->line_to(start_x + (35 * dir), start_y);
cr->line_to(start_x + (35 * dir), start_y + 10);
cr->stroke();
// draw the horizontal lines
cr->move_to(start_x + (25 * dir), start_y + 10);
cr->line_to(start_x + (45 * dir), start_y + 10);
cr->stroke();
cr->move_to(start_x + (30 * dir), start_y + 15);
cr->line_to(start_x + (40 * dir), start_y + 15);
cr->stroke();
cr->move_to(start_x + (34 * dir), start_y + 20);
cr->line_to(start_x + (36 * dir), start_y + 20);
cr->stroke();
}
// draw labels and connect them to the structure with arrows
void List::draw_labels(const Cairo::RefPtr<Cairo::Context> & cr) {
draw_label_helper(cr, head, "head", list_x, list_y, RIGHT);
}
void List::draw_label_helper(const Cairo::RefPtr<Cairo::Context> & cr, ListNode * label, const char * text, int x, int y, LabelArrowPos arrowPos) {
cr->set_source_rgb(0.0, 0.0, 0.0);
Pango::FontDescription font;
font.set_family("Monospace");
font.set_weight(Pango::WEIGHT_BOLD);
Glib::RefPtr<Pango::Layout> layout = create_pango_layout(text);
layout->set_font_description(font);
int text_w, text_h;
layout->get_pixel_size(text_w, text_h);
cr->move_to(x, y);
layout->show_in_cairo_context(cr);
if (label != NULL) {
if (arrowPos == RIGHT) {
draw_arrow_helper(cr, x + text_w + 10, y + text_h / 2, label->x - 10, label->y + ListNode::field_h);
} else {
draw_arrow_helper(cr, x - 10, y + text_h / 2, label->x + ListNode::field_w + 10, label->y + ListNode::field_h);
}
} else {
if (arrowPos == RIGHT) {
draw_null_arrow(cr, x + text_w + 10, y + text_h / 2, true);
} else {
draw_null_arrow(cr, x - 10, y + text_h / 2, false);
}
}
}
// draw nodes which are fully attached to the list
void List::draw_connected(const Cairo::RefPtr<Cairo::Context> & cr) {
ListNode * n = head;
while (n != NULL) {
n->draw(cr);
n->printed = true;
draw_arrows(cr, n);
n = n->next;
}
}
// draw nodes which may point to the list but are no fully attached to it
void List::draw_disconnected(const Cairo::RefPtr<Cairo::Context> & cr) {
int xTmp = out_x;
int yTmp = out_y;
for (int i = 0; i < currentNodes; i++) {
if (!nodes[i]->printed) {
nodes[i]->y = yTmp;
nodes[i]->x = xTmp;
nodes[i]->draw(cr);
draw_arrows(cr, nodes[i]);
xTmp += 2 * ListNode::padding;
}
}
}
const int ListNode::field_w = 50;
const int ListNode::field_h = 20;
const int ListNode::padding = 100;
ListNode::ListNode() {
}
ListNode::ListNode(List * list) {
this->next = NULL;
this->printed = false;
this->list = list;
list->newNode(this);
}
ListNode::ListNode(List * list, int data) {
this->data = data;
this->next = NULL;
this->printed = false;
this->list = list;
list->newNode(this);
this->numFields = 2;
}
ListNode::~ListNode() {
list->removeFromArray(this);
}
void ListNode::draw(const Cairo::RefPtr<Cairo::Context> & cr) {
cr->set_source_rgb(1.0, 1.0, 1.0);
draw_node(cr, x, y);
cr->set_source_rgb(0.0, 0.0, 0.0);
draw_text(cr, x, y);
}
void ListNode::draw_text(const Cairo::RefPtr<Cairo::Context> & cr, int x, int y) {
char * str = new char[10];
sprintf(str, "%d", data);
Pango::FontDescription font;
font.set_family("Monospace");
font.set_weight(Pango::WEIGHT_BOLD);
Glib::RefPtr<Pango::Layout> layout = create_pango_layout(str);
layout->set_font_description(font);
int text_w, text_h;
layout->get_pixel_size(text_w, text_h);
cr->move_to(x + (field_w - text_w) / 2, y + (field_h - text_h) / 2);
layout->show_in_cairo_context(cr);
}
void ListNode::draw_node(const Cairo::RefPtr<Cairo::Context> & cr, int x, int y) {
cr->rectangle(x, y, field_w, field_h * numFields);
cr->fill();
cr->set_source_rgb(0.0, 0.0, 0.0);
cr->set_line_width(2.0);
for (int i = 0; i < numFields; i++) {
cr->rectangle(x, y + (i * field_h), field_w, field_h);
cr->stroke();
}
}