Skip to content

Commit 69d40e8

Browse files
committed
2 parents ca842c4 + 4ee9aba commit 69d40e8

File tree

11 files changed

+65
-12
lines changed

11 files changed

+65
-12
lines changed

src/algorithms/controllers/AStar.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default {
2121
};
2222
},
2323

24-
run(chunker, {edgeValueMatrix, coordsMatrix, startNode, endNodes, heuristicFn}) {
24+
run(chunker, {edgeValueMatrix, coordsMatrix, startNode, endNodes, heuristicFn, moveNode}) {
2525
// String Variables used in displaying algo
2626
const algNameStr = 'aStar';
2727
const dashStr = '-';
@@ -130,6 +130,7 @@ export default {
130130
(vis, edgeArray, coordsArray) => {
131131
vis.graph.directed(false);
132132
vis.graph.weighted(true);
133+
vis.graph.moveNodeFn(moveNode);
133134
vis.graph.set(edgeArray, Array.from({ length: numVertices }, (v, k) => (k + 1)),coordsArray);
134135
},
135136
[E, coords]

src/algorithms/controllers/BFS.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
};
1919
},
2020

21-
run(chunker, { edgeValueMatrix, coordsMatrix, endNodes, startNode}) {
21+
run(chunker, { edgeValueMatrix, coordsMatrix, endNodes, startNode, moveNode}) {
2222

2323
//Defining queue
2424
function Queue() {
@@ -70,6 +70,7 @@ export default {
7070
(vis, edgeArray, coordsArray) => {
7171
vis.graph.directed(false);
7272
vis.graph.weighted(false);
73+
vis.graph.moveNodeFn(moveNode);
7374
vis.graph.set(edgeArray, Array.from({ length: edgeValueMatrix.length }, (v, k) => (k + 1)), coordsArray);
7475

7576
},

src/algorithms/controllers/DFS.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default {
2121
};
2222
},
2323

24-
run(chunker, { edgeValueMatrix, coordsMatrix, endNodes, startNode}) {
24+
run(chunker, { edgeValueMatrix, coordsMatrix, endNodes, startNode, moveNode}) {
2525

2626
const E = [...edgeValueMatrix];
2727
const coords = [...coordsMatrix];
@@ -50,6 +50,7 @@ export default {
5050
(vis, edgeArray, coordsArray) => {
5151
vis.graph.directed(false);
5252
vis.graph.weighted(false);
53+
vis.graph.moveNodeFn(moveNode);
5354
vis.graph.set(edgeArray, Array.from({ length: edgeValueMatrix.length }, (v, k) => (k + 1)), coordsArray);
5455

5556
},

src/algorithms/controllers/DFSrec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default {
2525
};
2626
},
2727

28-
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes}) {
28+
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes, moveNode}) {
2929
// String Variables used in displaying algo
3030
const algNameStr = 'DFSrec';
3131
const nStr = 'n';
@@ -241,6 +241,7 @@ export default {
241241
(vis, edgeArray, coordsArray) => {
242242
vis.graph.directed(false);
243243
vis.graph.weighted(false);
244+
vis.graph.moveNodeFn(moveNode);
244245
vis.graph.set(edgeArray, Array.from({ length: numVertices }, (v, k) => (k + 1)),coordsArray);
245246
},
246247
[E, coords], 0

src/algorithms/controllers/dijkstra.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default {
1717
};
1818
},
1919

20-
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes}) {
20+
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes, moveNode}) {
2121
// String Variables used in displaying algo
2222
const algNameStr = 'dijkstra';
2323
const dashStr = '-';
@@ -115,6 +115,7 @@ export default {
115115
(vis, edgeArray, coordsArray) => {
116116
vis.graph.directed(false);
117117
vis.graph.weighted(true);
118+
vis.graph.moveNodeFn(moveNode);
118119
vis.graph.set(edgeArray, Array.from({ length: numVertices }, (v, k) => (k + 1)),coordsArray);
119120
},
120121
[E, coords]

src/algorithms/controllers/kruskal.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
};
1919
},
2020

21-
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes}) {
21+
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes, moveNode}) {
2222
// String Variables used in displaying algo
2323
const algNameStr = 'kruskal';
2424
const etcStr = '...';
@@ -114,6 +114,7 @@ export default {
114114
(vis, edgeArray, coordsArray) => {
115115
vis.graph.directed(false);
116116
vis.graph.weighted(true);
117+
vis.graph.moveNodeFn(moveNode);
117118
vis.graph.set(edgeArray, Array.from({ length: numVertices }, (v, k) => (k + 1)),coordsArray);
118119
},
119120
[E, coords]

src/algorithms/controllers/prim.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
};
1919
},
2020

21-
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes}) {
21+
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes, moveNode}) {
2222
// String Variables used in displaying algo
2323
const algNameStr = 'prim';
2424
const dashStr = '-';
@@ -132,6 +132,7 @@ export default {
132132
(vis, edgeArray, coordsArray) => {
133133
vis.graph.directed(false);
134134
vis.graph.weighted(true);
135+
vis.graph.moveNodeFn(moveNode);
135136
vis.graph.set(edgeArray, Array.from({ length: numVertices }, (v, k) => (k + 1)),coordsArray);
136137
},
137138
[E, coords]

src/algorithms/instructions/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ on the arrow in its middle, the right and bottom panels can be enlarged or shrun
7979
by dragging the "..." up temporarily`,
8080
`Under the ${KEY_PLAY} button, toggle between sample graphs (eg Graph 1) and random graphs, or`,
8181
'edit text for X-Y node coordinates (this can change the graph size) and edges/weights (weights are ignored for unweighted graph algorithms), or',
82-
'enter X-Y node coordinates and edges/weights in tables.',
82+
'enter X-Y node coordinates and edges/weights in tables below, or',
83+
'change X-Y node coordinates by selecting a node with the mouse and dragging it.',
8384
`The graph size can also be explicitly increased/decreased - this generates a new random graph.`,
8485
`Edge weights (for weighted graph algorithms) can be toggled between Euclidean, Manhattan and as defined explicitly in the input.`,
8586
]},

src/algorithms/parameters/helpers/EuclideanMatrixParams.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,25 @@ function EuclideanMatrixParams({
418418
setCoordsTxt(getCoordinateList(newData1));
419419
};
420420

421+
// set up a function to call if node is moved with mouse (see
422+
// components/DataStructures/Graph/GraphRenderer/index.js) so
423+
// coordinates here can be updated
424+
const moveNode = (nodeID, x, y) => {
425+
console.log(['moveNode', nodeID, x, y]);
426+
const newData1 = data1.map((row, index) => {
427+
if (index === nodeID) {
428+
return {
429+
...data1[nodeID],
430+
['col0']: x.toString(),
431+
['col1']: y.toString(),
432+
};
433+
}
434+
return row;
435+
});
436+
setData1(newData1);
437+
setCoordsTxt(getCoordinateList(newData1));
438+
}
439+
421440
// When cell renderer calls updateData2, we'll use
422441
// the rowIndex, columnId and new value to update the
423442
// original data + symmetric cell if flag set
@@ -577,7 +596,8 @@ function EuclideanMatrixParams({
577596
endNodes,
578597
heuristicFn,
579598
coordsMatrix,
580-
edgeValueMatrix
599+
edgeValueMatrix,
600+
moveNode
581601
});
582602
// setButtonMessage('Reset');
583603
} else {

src/components/DataStructures/Graph/GraphRenderer/index.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,34 @@ class GraphRenderer extends Renderer {
8181
}
8282

8383
handleMouseMove(e) {
84-
if (this.selectedNode && this.props.title !== 'Graph view') {
84+
// XXX would be nice to avoid selecting text with reverse video
85+
// as we move the mouse around!
86+
// XXX really shouldn't depend on this.props.title - moving away
87+
// from this but still have a hack for Warshalls
88+
if (this.selectedNode && this.props.title === 'Transitive Closure') {
89+
// XXX Old stuff so Warshall's remains as it was
8590
// Allow mouse movement
8691
const { x, y } = this.computeCoords(e);
8792
const node = this.props.data.findNode(this.selectedNode.id);
8893
node.x = x;
8994
node.y = y;
9095
this.refresh();
91-
} else if (this.selectedNode && this.props.title === 'Graph view') {
92-
// Ignore mouse movement if Graph view was used
96+
} else if (this.selectedNode && this.props.data.moveNode) {
97+
// Allow mouse to move nodes (for Euclidean graphs) if
98+
// this.props.data.moveNode function is defined
99+
const { x, y } = this.computeCoords(e);
100+
const node = this.props.data.findNode(this.selectedNode.id);
101+
const scaleSize = 30; // XXX should define globally in one spot!
102+
const scaledX = Math.round(x/scaleSize);
103+
const scaledY = -Math.round(y/scaleSize);
104+
if (scaledX > 0 && scaledY > 0) { // limit range, XXX add max?
105+
this.props.data.moveNode(node.id, scaledX, scaledY);
106+
node.x = x;
107+
node.y = y;
108+
this.refresh();
109+
}
110+
} else if (this.selectedNode) {
111+
// Ignore mouse movement if no moveNode function is defined
93112
this.refresh();
94113
} else {
95114
super.handleMouseMove(e);

src/components/DataStructures/Graph/GraphTracer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class GraphTracer extends Tracer {
112112
* @param {array} array2d 2D array of nodes
113113
*/
114114
set(array2d = [], values = [], coordinates = []) {
115+
this.scaledCoords = coordinates;
115116
this.setNodeRadius(coordinates);
116117

117118
// Set layout to null if nodes are to be displayed by coordinates.
@@ -130,6 +131,7 @@ class GraphTracer extends Tracer {
130131
else
131132
{
132133
// Do not change this value unless you also change axis scales
134+
// and also check handleMouseMove
133135
const scaleSize = 30;
134136
const x = coordinates[i][0] * scaleSize;
135137
const y = -coordinates[i][1] * scaleSize;
@@ -273,6 +275,10 @@ class GraphTracer extends Tracer {
273275
this.isWeighted = isWeighted;
274276
}
275277

278+
moveNodeFn(moveNode) {
279+
this.moveNode = moveNode;
280+
}
281+
276282
addNode(id, value = undefined, shape = 'circle', color = 'blue', weight = null,
277283
x = 0, y = 0, visitedCount = 0, selectedCount = 0, visitedCount1 = 0,
278284
isPointer = 0, pointerText = '') {

0 commit comments

Comments
 (0)