Skip to content

Commit e7d002d

Browse files
committed
gwrap a reasonable prototype
1 parent 8a71b90 commit e7d002d

File tree

4 files changed

+80
-38
lines changed

4 files changed

+80
-38
lines changed

src/algorithms/controllers/gwrap.js

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// to fix this display without breaking other algorithms (code seems a
1212
// bit messy).
1313
//
14-
// XXX Not properly linked to pseudocode (need more bookmarks and change
14+
// XXX Partially linked to pseudocode (may need more bookmarks/change
1515
// the bookmarks in this controller code); also will need to animate a
1616
// few more things.
1717
//
@@ -50,7 +50,7 @@ export default {
5050
initVisualisers() {
5151
return {
5252
graph: {
53-
instance: new GraphTracer('graph', null, 'Graph view'),
53+
instance: new GraphTracer('graph', null, 'Points'),
5454
order: 0,
5555
},
5656
};
@@ -116,8 +116,22 @@ export default {
116116
// Prints convex hull of a set of n coords.
117117
function convexHull(coords, n, E)
118118
{
119+
chunker.add(
120+
'start',
121+
(vis, edgeArray, coordsArray) => {
122+
vis.graph.directed(false);
123+
vis.graph.weighted(false);
124+
vis.graph.moveNodeFn(moveNode);
125+
vis.graph.set(edgeArray, Array.from({ length: numVertices }, (v, k) => (k + 1)),coordsArray);
126+
vis.graph.dimensions.defaultNodeRadius = 15;
127+
vis.graph.dimensions.nodeRadius = 15;
128+
vis.graph.setZoom(0.6);
129+
},
130+
[E, coords], 0
131+
);
132+
119133
// There must be at least 3 coords
120-
// XXX highlight all
134+
// XXX highlight all here if n < 3
121135
if (n < 3) return;
122136

123137
// Initialize Result
@@ -146,13 +160,14 @@ export default {
146160
// clockwise until reach the start point
147161
// again. This loop runs O(h) times where h is
148162
// number of coords in result or output.
149-
let p = l, q;
163+
let p = l, q, prevP;
150164
chunker.add(
151-
'start',
165+
'initP',
152166
(vis, edgeArray, coordsArray, c_p, c_q) => {
153-
// XXX Graph too big and too far left...
154-
// vis.graph.setZoom(0.5);
155-
vis.graph.colorNode(c_p, colorsCH.HULLP_N);
167+
// Add special node a long way away for "wrapper"
168+
vis.graph.addNode(wrapperStr, wrapperStr);
169+
vis.graph.setNodePosition(wrapperStr, 10, -2000);
170+
vis.graph.colorNode(c_p, colorsCH.NEXTQ_N);
156171
vis.graph.addEdge(wrapperStr, c_p);
157172
vis.graph.colorEdge(c_p, wrapperStr, colorsCH.HULL_E);
158173
},
@@ -161,6 +176,32 @@ export default {
161176
do
162177
{
163178

179+
chunker.add('addP',
180+
(vis, edgeArray, coordsArray, c_pp, c_p) => {
181+
vis.graph.colorNode(c_p, colorsCH.HULLP_N);
182+
// Nice to do this in a couple of steps if possible
183+
// Move wrapper close then final position with node
184+
// colour change?
185+
if (c_pp) { // first time around c_pp is undefined
186+
vis.graph.addEdge(c_pp, c_p);
187+
vis.graph.colorEdge(c_pp, c_p, colorsCH.HULL_E);
188+
vis.graph.colorNode(c_p, colorsCH.HULLP_N);
189+
// redo "wrapping" edge
190+
vis.graph.removeEdge(c_pp, wrapperStr);
191+
vis.graph.addEdge(wrapperStr, c_p);
192+
vis.graph.colorEdge(c_p, wrapperStr, colorsCH.HULL_E);
193+
let [pX, pY] = vis.graph.getNodePosition(c_pp);
194+
let [qX, qY] = vis.graph.getNodePosition(c_p);
195+
// XXX wrapper position should be colinear but far away
196+
// Currently if p and q are close, wrapper may be too
197+
// close; also need to consider case where p=q...?
198+
let wX = qX + 50 * (qX - pX);
199+
let wY = qY + 50 * (qY - pY);
200+
vis.graph.setNodePosition(wrapperStr, wX, wY);
201+
}
202+
},
203+
[E, coords, prevP, p], 0
204+
);
164205
// Add current point to result
165206
hull.push(p);
166207

@@ -249,18 +290,14 @@ export default {
249290
hullHasQ = hull.includes(q);
250291
}
251292

252-
// XXX this is more animation of add p to hull...
253-
// Should move most of this earlier but have to deal with
254-
// initial case when q is undefined and we don't have to
255-
// add edge etc
256293
chunker.add('p<-q',
257294
(vis, edgeArray, coordsArray, c_p, c_q) => {
258-
// Nice to do this in a couple of steps if possile
295+
// Nice to do this in a couple of steps if possible
259296
// Move wrapper close then final position with node
260297
// colour change?
261298
vis.graph.addEdge(c_p, c_q);
262299
vis.graph.colorEdge(c_p, c_q, colorsCH.HULL_E);
263-
vis.graph.colorNode(c_q, colorsCH.HULLP_N);
300+
// vis.graph.colorNode(c_q, colorsCH.HULLP_N);
264301
// redo "wrapping" edge
265302
vis.graph.removeEdge(c_p, wrapperStr);
266303
vis.graph.addEdge(wrapperStr, c_q);
@@ -279,8 +316,20 @@ export default {
279316
// Now q is the most clockwise with
280317
// respect to p. Set p as q for next iteration,
281318
// so that q is added to result 'hull'
319+
prevP = p;
282320
p = q;
283-
chunker.add('whileP');
321+
if (p != l) {
322+
chunker.add('whileP');
323+
} else {
324+
// clean up on loop exit
325+
chunker.add('whileP',
326+
(vis, edgeArray, coordsArray, c_pp, c_p) => {
327+
vis.graph.colorNode(c_pp, colorsCH.HULLP_N);
328+
vis.graph.colorNode(c_p, colorsCH.HULLP_N);
329+
},
330+
[E, coords, prevP, p], 0
331+
);
332+
}
284333

285334
} while (p != l); // While we don't come to first
286335
// point
@@ -317,22 +366,6 @@ export default {
317366

318367

319368

320-
chunker.add(
321-
'start',
322-
(vis, edgeArray, coordsArray) => {
323-
vis.graph.directed(false);
324-
vis.graph.weighted(false);
325-
vis.graph.moveNodeFn(moveNode);
326-
vis.graph.dimensions.defaultNodeRadius = 15;
327-
vis.graph.dimensions.nodeRadius = 15;
328-
vis.graph.set(edgeArray, Array.from({ length: numVertices }, (v, k) => (k + 1)),coordsArray);
329-
// vis.graph.edges = []
330-
// Add special node a long way away for "wrapper"
331-
vis.graph.addNode(wrapperStr, wrapperStr);
332-
vis.graph.setNodePosition(wrapperStr, 10, -2000);
333-
},
334-
[E, coords], 0
335-
);
336369

337370
convexHull(coords, coords.length, E);
338371

src/algorithms/parameters/gwrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const DEFAULT_HEUR = null; // disable heuristic display/input
2222
const GRAPH_EGS = [ // XXX think up better examples?
2323
{ name: 'Graph 1',
2424
size: 14,
25-
coords: '33-10,2-7,9-7,14-11,22-7,19-4,21-15',
25+
coords: '33-10,2-7,11-7,14-11,22-7,19-4,21-15',
2626
// coords: '4-3,2-7,7-11,9-3,12-6,13-2,12-16,17-2,20-4,34-4,26-9,30-6,34-10,38-5',
2727
edges: ''
2828
},

src/algorithms/pseudocode/gwrap.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ giftWrap(P, n) // return convex hull of points P[0]...P[n-1] in a plane \\B star
2222
\\In{
2323
if n <= 3
2424
\\In{
25-
return the set of all points // n < 3 could be considered an error
25+
return the set of all points // n < 3 could be considered an error \\B returnAll
2626
\\In}
27-
minX <- point with min. X value
27+
hull <- Empty // Initialize hull to the empty set of points
28+
minX <- point with min. X value \\B minX
2829
\\Expl{ We could choose any point guaranteed to be on the convex
2930
hull. Here we use a point with the minimal X coordinate. We
3031
scan through all points to find it. If there are multiple points
3132
with the minimal X coordinate we choose one with a minimal Y coordinate.
3233
\\Expl}
33-
hull <- Empty // Initialize hull to the empty set of points
34-
p <- minX // initialize current point; "string" points left/up
34+
p <- minX // initialize current point; "string" points left/up \\B initP
3535
\\Expl{ In the animation we show a line from p; you can think
3636
of this as a string we use to wrap around the points to form the
3737
convex hull.
3838
\\Expl}
3939
do
4040
\\In{
41-
add p to hull
41+
add p to hull \\B addP
4242
\\Note{ Use color of p to indicate membership of hull. Also q
4343
and i (not sure if two vars are equal or q/i are on hull...).
4444
Should also display vars next to (or in?) nodes. Should we

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,19 @@ class GraphRenderer extends Renderer {
6161
// linked. Some magic numbers were added to shift things around and make
6262
// things look ok. It should be rethought or at least the numbers for
6363
// this.centerX and this.centerY should be put in one place.
64+
// Hacked so we now have an axes flag set depending on title XXX
6465
if (this.props.title === 'Graph view') {
6566
// Center to new axis origin
6667
// this.centerX = 180;
6768
this.centerX = 650; // shift graph display left
6869
this.centerY = -200;
70+
this.axes = true;
71+
} else if (this.props.title === 'Points') {
72+
// Center to new axis origin
73+
// this.centerX = 180;
74+
this.centerX = 440; // shift graph display left a bit
75+
this.centerY = -200;
76+
this.axes = true;
6977
}
7078
this.zoom = 0.85; // zoom out a bit to fit graph on screen
7179

@@ -319,7 +327,8 @@ class GraphRenderer extends Renderer {
319327

320328
const originCoords = { x: axisCenter.x - 12, y: axisCenter.y + 16 };
321329

322-
if (this.props.title !== 'Graph view') {
330+
// if (this.props.title !== 'Graph view') {
331+
if (!this.axes) {
323332
// Do not render axis if its not graph
324333
return <g></g>;
325334
}

0 commit comments

Comments
 (0)