2
$\begingroup$

I am using the alphaShapes2DC function from this post. It is a module which contains a Quiet operation. DelaunayMesh in a specified closed region - creating a concave hull from a set of points

I have a list containing 14 lists of 2d-points

prjs = {{{0, 1}, {1, 2}, {5, 1}}, {{2,2}}, ..., {{1, 5}, {6, 5}, {3, 5}}} 

with some sublists containing only one element and some duplicates so that the alphaShapes2DC function cannot run on such sublists.

I want to create a Manipulate function to display several sublists (with their polygon) at the same time.

Manipulate[
  If[Length @ prjs[[i]] > 2,
     Show[
       ListPlot[prjs[[i]], 
         Joined -> False, PlotStyle -> ColorData[97, "ColorList"][[i]]], 
       RegionPlot[Evaluate[alphaShapes2DC[DeleteDuplicates @ prjs[[i]], 6]],
         PlotStyle -> {ColorData[97, "ColorList"][[i]], Opacity@.2}, 
         BoundaryStyle -> {ColorData[97, "ColorList"][[i]], Thin}, 
         PlotRange -> All], PlotRange -> {{-15, 10}, {-10, 10}}],
       ListPlot[prjs[[i]], 
         Joined -> False,
         PlotStyle -> ColorData[97, "ColorList"][[i]],
         PlotRange -> {{-15, 10}, {-20, 20}}]],
  {{i, 1}, Range @ Length @ prjs}, 
    ContinuousAction -> True, ControlType -> TogglerBar]

Running this code first shows what I want.

enter image description here

But it turns bad when I click on a number in the bar — the polygon disappears.

enter image description here

$\endgroup$
2
$\begingroup$

Revision

First I contrive some test data since you did't provide any,

SeedRandom[3];
Block[{size},
  size := RandomInteger[{1, 20}];
  pts = RandomInteger[{1, 10}, {#, 2}] & /@ Table[size, 14]];

Now I generate lists for the items that will be plotted according to the buttons pressed on the toggler bar.

hullPlots = 
  Table[
    If[Length @ pts[[i]] > 2, 
      RegionPlot[ConvexHullMesh[pts[[i]]], 
        PlotStyle -> {ColorData[97][i], Opacity @ .2}, 
        BoundaryStyle -> {ColorData[97][i], Thin}], 
      Graphics[]], 
    {i, Length @ pts}];

ptPlots =
  Table[ListPlot[pts[[i]], PlotStyle -> ColorData[97][i]], {i, Length @ pts}];

Finally I write a Manpulate expression with a toggler bar control. The important thing to note about a toggler bar is that it returns a list of the buttons currently pressed.

DynamicModule[{hulls, pts},
  Manipulate[
    hulls = hullPlots[[selctn]];
    pts = ptPlots[[selctn]];
    Show[##, AxesOrigin -> {0, 0}, PlotRange -> {{0, 10}, {0, 10}}] & @@
      Join[hulls, pts],
    {{selctn, {1}, "i"}, Range @ Length @ ptPlots, TogglerBar}]]

The Manpulate expression comes up looking like this.

initial

And here is what it might look like with several buttons pressed.

many

Note: If there are problems with alphaShapes2DC, I think that is an issue for a separate question, and I don't want to deal with it. That is why I used ConvexHullMesh instead.

BTW, if you would prefer all the points to always be displayed, that can be done with even simpler code. Like so.

DynamicModule[{hulls, pts},
  Manipulate[
    hulls = hullPlots[[selctn]];
    Show[##, AxesOrigin -> {0, 0}, PlotRange -> {{0, 10}, {0, 10}}] & @@
      Join[hulls, ptPlots],
    {{selctn, {1}, "i"}, Range @ Length @ ptPlots, TogglerBar}]]

all_pts

$\endgroup$
  • $\begingroup$ Dear m_goldberg. Thanks for your answer but unfortunately it does not work as I need. I need a TogglerBar to display several sublists at the same time (my code was already working with a SetterBar). I took your code and change to TogglerBar and I get exactly the same issue : selecting one sublist make the polygon disappear and selecting two or more sublists is leading to a RegionPlot crash. $\endgroup$ – Porty Feb 12 '18 at 6:42
  • $\begingroup$ Based on your code, I try to run this simplest one with a TogglerBar and don't understand why it does not work : SeedRandom[3]; Block[{size}, size := RandomInteger[{10, 20}]; pts = RandomInteger[{1, 10}, {#, 2}] & /@ Table[size, 14]]; Manipulate[ Show[ListPlot[pts[[i]]], RegionPlot[ConvexHullMesh[pts[[i]]]]], {{i, 1}, Range@Length@pts, TogglerBar}] $\endgroup$ – Porty Feb 12 '18 at 7:20
  • $\begingroup$ I just want to do something like this : mathematica.stackexchange.com/questions/71718/… $\endgroup$ – Porty Feb 12 '18 at 7:23
  • $\begingroup$ Even this one does not work : SeedRandom[3]; Block[{size}, size := RandomInteger[{10, 20}]; pts = RandomInteger[{1, 10}, {#, 2}] & /@ Table[size, 14]]; Manipulate[ ConvexHullMesh@pts[[i]], {{i, 1}, Range@Length@pts, TogglerBar}] while it works with replacing ConvexHullMesh by ListPlot $\endgroup$ – Porty Feb 12 '18 at 7:33
  • $\begingroup$ @Porty. I have revised my answer to use a toggler bar. I hope it is now gives you a framework that you can use to work with alphaShapes2DC. $\endgroup$ – m_goldberg Feb 12 '18 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.