-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack_Potential.js
168 lines (158 loc) · 4.82 KB
/
Stack_Potential.js
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
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
var array = [];
var w = canvas.width;
var h = canvas.height;
var i = 10;
var j = 0;
var size = 0;
var x = w / 2 + 5;
var y = h - 30;
var x_new = w / 2 - 55;
var y_new = h - 30;
var move = 50;
writeMessage(canvas, "Ф(i) = количество элементов в iый момент", w - 400, 50, "#000000", '15pt Calibri');
writeMessage(canvas, "Реальная стоимость операций:", w - 325, 70, "#000000", '15pt Calibri');
writeMessage(canvas, "* push: 1", w - 240, 90, "#000000");
writeMessage(canvas, "* pop(n): n", w - 240, 110, "#000000");
function draw_square(canvas, num, x, y, x_new, y_new) {
var ctx = canvas.getContext('2d');
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + move, y);
ctx.lineTo(x + move, y - move);
ctx.lineTo(x, y - move);
ctx.lineTo(x, y);
ctx.clearRect(w / 2 - 650, h / 2 - 35, 400, 20);
var mess = "c'= 1 + "
writeMessage(canvas, mess, w / 2 - 650, h / 2, "#000000");
mess = "Ф(стало)"
writeMessage(canvas, mess, w / 2 - 590, h / 2, "red");
mess = " - "
writeMessage(canvas, mess, w / 2 - 515, h / 2, "black");
mess = "Ф(было)"
writeMessage(canvas, mess, w / 2 - 500, h / 2, "green");
mess = " = 1 + "
writeMessage(canvas, mess + size + " - " + (size - 1) + " = " + 2, w / 2 - 425, h / 2, "#000000");
writeMessage(canvas, num, x, y, "black")
ctx.stroke();
y_new = make_equal(canvas, x, y, x_new, y_new, 1);
return {
x: x,
y: y - move,
xn: x_new,
yn: y_new
};
}
function insert() {
var num = document.getElementById("number").value
if (parseInt(num) == num) {
if (num >= 10 || num < 0) {
alert("число не удовлетворяет ограничениям");
return;
}
if (size > 12) {
alert("не достаточно места");
} else {
++size;
array.push(num);
var res = draw_square(canvas, num, x, y, x_new, y_new);
x = res.x;
y = res.y;
x_new = res.xn;
y_new = res.yn;
}
} else {
alert("число не удовлетворяет ограничениям");
}
document.getElementById("number").value = "";
}
function make_equal(canvas, x, y, x_new, y_new, from = 0) {
var temp = y
while (y_new < temp) {
if (size == 1 && from == 1) {
ctx.clearRect(x_new - 1, y_new - 1, move + 2, move + 2);
} else {
ctx.clearRect(x_new - 1, y_new - 1, move + 2, move);
}
y_new += move;
}
while (y_new > temp) {
ctx.strokeStyle = 'green';
ctx.beginPath();
ctx.moveTo(x_new, y_new);
ctx.lineTo(x_new + move, y_new);
ctx.lineTo(x_new + move, y_new - move);
ctx.lineTo(x_new, y_new - move);
ctx.lineTo(x_new, y_new);
if (from == 1) {
writeMessage(canvas, array[size - 2], x_new, y_new, "black")
} else {
writeMessage(canvas, array[size - 1], x_new, y_new, "black")
}
ctx.stroke();
y_new -= move;
}
return y_new;
}
function POP() {
var num = document.getElementById("clearButton").value
if (parseInt(num) == num) {
if (size < num) {
alert("Не достаточно элементов в стеке")
} else {
y_new = make_equal(canvas, x, y, x_new, y_new);
var temp = y;
var num_temp = num;
var prev_sz = size;
while (num != 0) {
var res = remove_square(canvas, num, x, temp, x_new, y_new);
temp = temp + move;
num -= 1;
array.pop();
}
ctx.clearRect(w / 2 - 650, h / 2 - 35, 400, 20);
var mess = "c' = "
writeMessage(canvas, mess + num_temp + " + ", w / 2 - 650, h / 2, "#000000");
mess = "Ф(стало)"
writeMessage(canvas, mess, w / 2 - 585, h / 2, "red");
mess = " - "
writeMessage(canvas, mess, w / 2 - 509, h / 2, "black");
mess = "Ф(было)"
writeMessage(canvas, mess, w / 2 - 493, h / 2, "green");
mess = " = "
writeMessage(canvas, mess + num_temp + " + " + size + " - " + prev_sz + " = " + 0, w / 2 - 420, h / 2, "#000000");
x = res.x;
y = res.y;
x_new = res.xn;
y_new = res.yn
}
} else {
alert("число не удовлетворяет ограничениям");
}
document.getElementById("clearButton").value = "";
}
function remove_square(canvas, num, x, y, x_new, y_new) {
var ctx = canvas.getContext('2d');
if (size == 1) {
ctx.clearRect(x - 1, y - 1, move + 2, move + 2);
} else {
ctx.clearRect(x - 1, y - 1, move + 2, move);
}
y = y + move;
size -= 1;
return {
x: x,
y: y,
xn: x_new,
yn: y_new
};
}
function writeMessage(canvas, message, x, y, colour, font = '15pt Calibri') {
var ctx = canvas.getContext('2d');
ctx.font = font;
ctx.fillStyle = colour;
ctx.fillText(message, x + move / 2 - 5, y - move / 2 + 5);
}