-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
183 lines (143 loc) · 5.15 KB
/
Copy pathsketch.js
File metadata and controls
183 lines (143 loc) · 5.15 KB
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
// sketch.js
let nodeCountInput, regenerateButton;
let nodes = []; // Global array to store nodes
//let edges = []; // Global array to store edges
let ant = null; // Global variable to track ant
let sidebarWidth = 200; // Width of the sidebar for metrics and buttons
let nodeCount = 30; // Default number of nodes to create
let minDistance = 50; // Minimum distance between nodes
let currentNode = null; // Variable to store the currently selected node
let desirability; // Global desirability value
let desirabilitySlider; // Global slider object
let antCountInput; // Input for the number of ants
let antCount = 3; // Default number of ants
let placeAntsButton;
let ants = []; // Global array to store ants
let stepping = false
function setup() {
createCanvas(windowWidth, windowHeight);
background(50); // Graphite gray background
// Create input for node count
nodeCountInput = createInput(nodeCount.toString());
nodeCountInput.position(20, 60);
nodeCountInput.style('width', '160px');
nodeCountInput.attribute('type', 'number'); // Specify input type as number
nodeCountInput.attribute('min', '1'); // Minimum value
nodeCountInput.attribute('max', '220'); // Maximum value
// Create button for regenerating nodes
regenerateButton = createButton('Regenerate Nodes');
regenerateButton.position(20, 100);
regenerateButton.mousePressed(regenerateNodes);
// Initial generation of nodes
createNodes();
// Initialize desirability to 5.0
desirability = 5.0;
// Create a slider for desirability with a default value of 5.0
desirabilitySlider = createSlider(0.0, 10.0, 5.0, 0.1);
desirabilitySlider.position(20, 140); // Adjust position as needed
desirabilitySlider.style('width', '160px');
// Create input for the number of ants
antCountInput = createInput(antCount.toString());
antCountInput.position(20, 180);
antCountInput.style('width', '160px');
antCountInput.attribute('type', 'number');
antCountInput.attribute('min', '1'); // Minimum value
// Create 'Place Ants' button
placeAntsButton = createButton('Place Ants');
placeAntsButton.position(20, 220); // Adjust the position as needed
placeAntsButton.mousePressed(placeAnts); // Handle 'Place Ants' button click
// Create 'Step' button
StepButton = createButton('Step');
StepButton.position(20, 250); // Adjust the position as needed
StepButton.mousePressed(startStep); // Handle 'Place Ants' button click
// Create 'Stop' button
StepButton = createButton('Stop');
StepButton.position(20, 280); // Adjust the position as needed
StepButton.mousePressed(stop); // Handle 'Place Ants' button click
}
// Main draw loop
function draw() {
// Clear the background to redraw everything again
background(50); // Graphite gray background
// Draw the sidebar
fill(30); // Darker shade for the sidebar
noStroke();
rect(0, 0, sidebarWidth, height);
// Draw the nodes
for (let node of nodes) {
node.display();
}
// Draw the ant
//if (ant){
// ant.update(); // Update Ant animation
// ant.display(); // Draw the Ant
//}
// Draw the ants
ants.forEach(ant => {
ant.update(); // Update Ant animation
ant.display(); // Draw the Ant
// For each ant update and draw all edges, and remove them if they are done retracting
for (let i = ant.edges.length - 1; i >= 0; i--) {
ant.edges[i].update();
if (ant.edges[i].toRemove) {
ant.edges.splice(i, 1); // Remove the edge
} else {
ant.edges[i].display(); // Only display the edge if it's not removed
}
}
});
// Update and draw all edges
//edges.forEach(edge => {
// edge.update(); // Update the edge for animation
// edge.display(); // Draw the edge
//});
// Update desirability value from the slider
desirability = desirabilitySlider.value();
// Adjacency matrix (Not sure if I will keep this)
//if (currentNode) {
// let matrix = currentNode.calculateAdjacencyMatrix(nodes);
// displayAdjacencyMatrix(matrix);
//}
}
// Function to display the adjacency matrix
function displayAdjacencyMatrix(matrix) {
// Starting position for displaying the matrix
let startX = 20;
let startY = 200;
matrix.forEach((row, i) => {
let rowText = row.join(' ');
text(rowText, startX, startY + i * 15);
});
}
// Function gets called whenever a mouseclick occurs
function mousePressed() {
//console.log("Mouse was clicked at " + mouseX + ", " + mouseY);
// Loop through all nodes to find if any of them were clicked
nodes.forEach(node => {
// Call the clicked method for each node
node.clicked(mouseX, mouseY, nodes);
// If the node was clicked, set it as the current node
if (node.selected) {
currentNode = node;
// EXECUTE ANIMATION TO MOVE ANT TO NODE
}
});
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
background(50); // Maintain the graphite gray background after resizing
}
function startStep(){
stepping = true;
multiStep();
}
function multiStep(){
step();
if (stepping == true){
setTimeout(multiStep, 2000); // Adjust delay as necessary
}
}
// Function to stop stepping through the algorithm
function stop(){
stepping = false;
}