-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
63 lines (53 loc) · 1.96 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
var Packet = require('./Packet');
var Cable = require('./Cable');
var Computer = require('./Computer');
var topology = document.getElementById('topology');
function generateCirce(target, computersNum, cableLength) {
function rotate(point, angle) {
var matrix = [
Math.cos(angle), -Math.sin(angle),
Math.sin(angle), Math.cos(angle)
];
var x = matrix[0] * point.x + matrix[1] * point.y;
var y = matrix[2] * point.x + matrix[3] * point.y;
return {x, y};
}
var nodes = [];
var node;
var tmp;
var centerX = target.clientWidth / 2;
var centerY = target.clientHeight / 2;
var angle = (2 * Math.PI) / (computersNum*cableLength);
var radius = target.clientHeight / 4;
var startingPoint = {x: radius, y: radius};
var rotatedPoint;
for(let i = 0; i < computersNum*cableLength; i += 1) {
rotatedPoint = rotate(startingPoint, i * angle);
rotatedPoint.x += centerX;
rotatedPoint.y += centerY;
tmp = document.createElement(i % cableLength == 0 ? 'computer' : 'cable');
tmp.style.left = `${rotatedPoint.x}px`;
tmp.style.top = `${rotatedPoint.y}px`;
target.appendChild(tmp);
node = i % cableLength == 0 ? new Computer(tmp, `PC${i}`) : new Cable(tmp);
nodes.push(node);
}
for(let i = 0; i < nodes.length - 1; i += 1) {
nodes[i].join(nodes[i+1], 0);
nodes[i+1].join(nodes[i], 1);
}
nodes[nodes.length-1].join(nodes[0], 0);
nodes[0].join(nodes[nodes.length-1], 1);
for(const me of nodes) {
if(me instanceof Computer) {
for(const node of nodes) {
if(node instanceof Computer && node !== me) {
me.addDestination(node);
}
}
}
}
}
var numComputers = prompt("Ile komputerów?", 8);
var numCable = prompt("Jaka długość kabla?", 8);
generateCirce(topology, numComputers, numCable);