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
0 commit comments