Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to use Manipulate to show the process of creating a convex hull using the Jarvis march algorithm. My code currently computes the convex hull using locators when you input points on the pane. I've shown it below.

What I would like to do is use the Manipulate to plot the progress of each addition of a side, one step at a time. In particular, I'd like for the user to be able to click on an "Autorun" button, and have the graph update after each computation of find$next$point, showing the addition of the next side. How could I go about doing this?

l = Graphics[{Black, Disk[{0, 0}, .1]}, ImageSize -> 10];

find$next$point[u_, p1_, p2_] := 
Module[{v1, v2, angles, maxIndex, i},
   v1 = p1 - p2;
   v2 = Inner[List, u[[All, 1]] - p2[[1]], u[[All, 2]] - p2[[2]], List];
   angles = {};
   Do [
   If[v2[[i]] == {0, 0},
     angles = Append[angles, 0];,
     angles  = Append[angles, VectorAngle[v1, v2[[i]]]];
   ];
   , {i, 1, Length[u]}
 ];
 maxIndex = Position[angles, Max[angles]][[1, 1]];
 u[[maxIndex]]
];

 convexHull[u_] := 
 Module[{hull, p1, p2, temp},
 If[u == {},
 hull = u;
 ,
 hull  = {};
 p2 = u[[Position[u[[All, 1]], Min[u[[All, 1]]]][[1, 1]]]];
 p1 = {p2[[1]], -10000};
 hull = Append[hull, p2];
 temp = find$next$point[u, p1, p2];
 p1 = p2;
 p2 = temp;
 hull = Append[hull, p2];
 While [p2 != hull[[1]],
  temp = find$next$point[u, p1, p2];
  p1 = p2;
  p2 = temp;
  hull = Append[hull, p2];
  ];
 ];
hull
];

Manipulate[
  Graphics[Line[convexHull[u]], PlotRange -> 3], 
  {{u, {}}, Locator, LocatorAutoCreate -> True, Appearance -> l}]
share|improve this question
    
I think you should work to optimize your code more first. You have things like angles = {};Do [.. angles=Append[angles, VectorAngle[v1,v2[[i]]]];];, {i, 1, Length[u]}]; Well, since you know the size Length[u], why are using Append for? This is slow. I'd preallocate angle array before the loop using Table and use [[]] to insert the values. Direct access is much faster than having to allocate new array for each loop iterations. see blog.wolfram.com/2011/12/07/… –  Nasser Apr 27 at 4:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.