-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
247 lines (208 loc) · 6.32 KB
/
main.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
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
245
246
247
const canvas = document.getElementById('canv');
const coordsDisplay = document.getElementById('coords');
const ctx = canvas.getContext('2d');
const pole=new Image();
pole.src="pole.png";
var i=0;
const g = new Transformer(460,356);
canvas.addEventListener('click', (event) => {
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
showAddDiv(rect,x,y);
});
function showAddDiv(rect,x,y){
let add=document.getElementsByClassName("addpoint")[0];
let btn=document.getElementsByClassName("addbtn")[0];
add.style.display="flex";
add.style.left= `${rect.left + x}px`;
add.style.top= `${rect.top + y}px`;
btn.onclick=()=>closeAddDiv(rect,x,y);
}
function closeAddDiv(rect,x,y){
let add=document.getElementsByClassName("addpoint")[0];
coordsDisplay.textContent = `X: ${x} Y: ${y}`;
g.insertVertex("t"+i,x,y,createDiv(rect,x,y,i));
i++;
new Promise((resolve) => {
drawGraph();
resolve();
}).then(()=>{
createGraphReprentations();
});
add.style.display="none";
}
function createDiv(rect,x,y,i)
{
let div=document.createElement('div');
div.className="pole";
div.style.left= `${rect.left + x}px`;
div.style.top= `${rect.top + y}px`;
let btncont=document.createElement('div');
btncont.className="pole_before";
btncont.id="t"+i;
btncont.style.display="none";
let sp=document.createElement('div');
sp.className="stoppower";
sp.innerHTML="stop power"
div.onclick=()=>{
btncont.style.display="flex";
}
btncont.appendChild(sp);
sp.onclick=()=>{
setPower("t"+i,0);
};
div.appendChild(btncont);
document.body.appendChild(div);
return div;
}
// Resize canvas to full screen
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
drawGraph(); // Redraw the graph after resizing
}
// Draw X and Y axis
function drawAxes() {
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
// X-axis
ctx.beginPath();
ctx.moveTo(0, centerY);
ctx.lineTo(canvas.width, centerY);
ctx.strokeStyle = 'gray';
ctx.stroke();
ctx.closePath();
// Y-axis
ctx.beginPath();
ctx.moveTo(centerX, 0);
ctx.lineTo(centerX, canvas.height);
ctx.strokeStyle = 'gray';
ctx.stroke();
ctx.closePath();
}
// Plot points based on given coordinates
function plotPoint(x, y, color = 'blue') {
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const scale = 20; // scale factor to adjust the unit size
// Translate coordinates to canvas space
const canvasX = centerX + x * scale;
const canvasY = centerY - y * scale;
// Draw point
g.setorigin(canvasX,canvasY);
ctx.beginPath();
ctx.arc(canvasX, canvasY, 5, 0, 2 * Math.PI, false);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
// Draw the graph with axes and points
function drawGraph() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear canvas
drawAxes();
plotPoint(0, 0, 'red');
}
function createGraphReprentations(){
const context = canvas.getContext('2d');
console.log(g.outgoing);
for(let [vertex,neighbor] of g.outgoing)
{
console.log('yoo',vertex);
context.fillStyle = '#0a9396';
context.beginPath();
context.arc(vertex.position[0], vertex.position[1], 5, 0, Math.PI * 2);
context.fill();
}
drawEdges();
}
function drawEdges(){
for(let [vertex,neighbor] of g.outgoing)
{
for(let [v,edge] of neighbor)
{
ctx.moveTo(vertex.position[0],vertex.position[1]);
ctx.lineTo(v.position[0],v.position[1]);
ctx.strokeStyle = "#343a40";
ctx.lineWidth = 2;
ctx.stroke();
ctx.closePath();
}
}
}
function transmit(){
let sortedDistance=g.transmitPower();
let delay=0;
for (const [vertex,dis] of sortedDistance) {
vertex.power = 250;
setTimeout(()=>{
drawPoint(vertex.position[0],vertex.position[1],"gold");
vertex.representative.style.borderColor="#b69121";
},500+(500*delay));
delay++;
}
}
function setPower(name,power){
let ver=g.dfs(name);
console.log(name);
let delay=0;
ver.forEach((vertex)=>{
setTimeout(()=>{
vertex.power=power;
drawPoint(vertex.position[0],vertex.position[1],"red");
vertex.representative.querySelectorAll('.pole_before').forEach((v)=>{
v.style.display="none";
});
vertex.representative.style.borderColor="#ffffff25";
},500+(500*delay));
delay++;
});
}
function FindError(){
let v=g.checkerror("power station");
let delay=0;
let prev=[];
for(let vertex of v)
{
if(vertex.power==0)
{
setTimeout(()=>{
drawPoint(vertex.position[0],vertex.position[1],"white");
vertex.representative.style.borderColor="white";
},500+(delay*500));
setTimeout(()=>{
let prevvertex=prev.shift();
drawPoint(prevvertex.position[0],prevvertex.position[1],"gold");
vertex.representative.style.borderColor="#b69121";
},500);
break;
}
else{
setTimeout(()=>{
drawPoint(vertex.position[0],vertex.position[1],"green");
vertex.representative.style.borderColor="green";
prev.push(vertex);
if (prev.length != 1 ){
setTimeout(()=>{
let prevvertex=prev.shift();
drawPoint(prevvertex.position[0],prevvertex.position[1],"gold");
vertex.representative.style.borderColor="#b69121";
},500);
}
},500+(500*delay));
delay++;
}
}
}
function showPoleOptions(event){
alert(event.target.id);
}
function drawPoint(x,y,color){
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(x,y, 5, 0, Math.PI * 2);
ctx.fill();
}
// Initial setup and event listeners
window.addEventListener('resize', resizeCanvas); // Handle window resize
resizeCanvas(); // Set initial canvas size