problem during polygon deletion

• To: mathgroup at smc.vnet.net
• Subject: [mg87974] problem during polygon deletion
• From: karlscherer3 at yahoo.co.nz
• Date: Mon, 21 Apr 2008 06:39:01 -0400 (EDT)

```(*

I have a bug in my Mathematica program destined for the Wolfram
Demonstration Project. Can anyone please give me a hint what I do
wrong.

The following Mathematica notebook allows the use to create, store and
delete polygons.

When I hit the "delete last vertex" button or the "delete last
polygon" button,

it works, but I get strange error messages. Any help would be very
much appreciated,

please send any answers to k a r l s c h e r e r 3 @ y a h o o . c o .
n z

(without the gaps). Even someone's email address of a person who could
help me would be great.

Getting desparate.

OK, here is the code:

*)

ClearAll["Global`*"];

(* -------------- define the orthogonal grid :---------------- *)

lx =30; ly=30;(*size of lattice *)

rx=Range[lx]; ry=Range[ly];

(* Tuples[list,n] Cell[TextData[{

"generate all possible ",

Cell[BoxData[

StyleBox["n", "TI"]], "InlineFormula"],

"-tuples of elements from ",

Cell[BoxData[

StyleBox["list", "TI"]], "InlineFormula"]

}], "TableText"]

*)lattice = Point[Tuples [{rx,ry}]];

(* -------------- define the isometric grid :---------------- *)

d=1 ;(*horizontal grid spacing *)

h=Sqrt[3]/2; (* height of triangles*)

lx ; ly=10;(*size of lattice *)

rx1= Range[0,lx];

ry1 = 2*h* Range[0,ly];

(* Cell["generate all possible pairs of elements from these lists:",
"TableText"] *)

lattice1 = Tuples [{rx1,ry1}]; (* lattice 1 *)

rx2 = .5 +rx1;

ry2= h+ry1;

lattice2 = Tuples [{rx2,ry2}]; (* lattice2 *)

isolattice = Point[Join[lattice1, lattice2]];

(* -------------- define snap to ortho and isometric
grid :---------------- *)

osnap[{x_,y_}]:={Floor[x +.5],Floor[y +.5]}

isosnap[{p_,q_}]:= With[

{d=Floor[Floor[q+h/2,2 h]-Floor[q+h/2, h]]},

{Floor[p+ (1-d)/2]+d/2,Floor[q+h/2, h]}

]

snap[p_]:=

If[gridsnap,

If[ortho,osnap[p],isosnap[p]],

p

]

(* --------------manipulate--------------------*)

Manipulate[

ClickPane[

Graphics[

{

(* ------- draw grid ---------------- *)

LightGray,

If[grid,

If[ortho,lattice,isolattice]

,{}

],

(* ------- draw text ---------------- *)

Black,

{Text[Style["Number of stored polygons:",12],{4,-1}]},

{Text[Style[Length[pols],12],{8.6,-1}]},

{Text[Style["Stored polygons shown:",12],{13.5,-1}]},

{Text[Style[Floor[1/2+ showpols*Length[pols]],12],{17.6,-1}]},

(* -------store a new polygon ---------------- *)

If[store,

{store=False,

If[Length[pts]>0,

{pols=AppendTo[pols, pts],

polcols=AppendTo[polcols, col1],

pts={}

},

{}]

},{}

],

(* ------- draw new points ---------------- *)

Black,PointSize[pointsize],

Point[pts], (* draw points *)

(* ------- draw next polygon ---------------- *)

FaceForm[col1],

EdgeForm[If[outline,Thin,None]],

Opacity[opacity],Polygon[pts],

(* ------- draw stored polygons ---------------- *)

Table[

If[showpols=B3(i-.5)/Length[pols],

{FaceForm[polcols[[i]]],Polygon[pols[[i]]]}

,{}],

{i,Length[pols]}

] ,

(* -------delete current polygon ---------------- *)

If[delnew,

{delnew=False,

pts={}

},{}

],

(* -------delete all stored polygons ---------------- *)

If[delall,

{delall=False,

pols={},polcols={}

},{}

],

(* -------delete last vertex of new polygon ---------------- *)

If[delvertex,

{delvertex=False,

If[Length[pts]>0,

pts=Delete[pts,-1]

,{}]

}

,{}]

(* -------delete last shown polygon ---------------- *)

If[delmax,

{delmax=False,

If[Length[pols]>0,

With[

{i=Floor[1/2+ showpols*Length[pols]]},

{pols=Delete[pols,i],polcols=Delete[polcols,i]}

],{}]

},{}]

},

PlotRange=AE{{-.2,18.2},{-2.2, 18.2}}

], (* end graphics *)

(

{ (* here we act on the mouseclicks (we are in a ClickPane !) *)

pts=AppendTo[pts,snap[#]] (* store next vertex *)

}

)&

], (* end clickpane *)

Style["Grid Controls",Bold],

{{grid,True,Style["show grid"]},{True,False}},

{{ortho,False,Style["ortho grid"]},{True,False}},

{{gridsnap,True,Style["snap to grid\n"]},{True,False}},

Style["Global Polygon Controls",Bold],

{{outline,True,Style["outline"]},{True,False}},

{{opacity,.7,Style["opacity\n"]},0,1,ImageSize=AESmall},

Style["Controls For New Polygon",Bold],

{{col1,Cyan,Style["face
colour"]},Red,ControlType=AEColorSlider,ImageSize=AESmall},

{{pointsize,.02,Style["pointsize new polygon",Black]},
0,.03,ControlType=AESlider,ImageSize=AESmall},

{{delvertex,False,Style["delete last vertex"]},{True,False}},

{{delnew,False,Style["delete new polygon"]},{True,False}},

{{store,False,Style["store new polygon\n",Blue,Bold]},{True,False}},

Style["Controls For Stored Polygons",Bold],

{{showpols,1,Style["show stored polygons"]},0,1,ImageSize=AESmall},

{{delmax,False,Style["delete last shown"]},{True,False}},

{{delall,False,Style["delete all stored"]},{True,False}},

{{pols,{}},ControlType=AENone},

{{pts,{}},ControlType=AENone},

{{polcols,{}},ControlType=AENone},

ControlPlacement=AELeft,

SaveDefinitions=AETrue

]

Manipulate[

ClickPane[

Graphics[

{

(* ------- draw grid ---------------- *)

LightGray,

If[grid,

If[ortho,lattice,isolattice]

,{}

],

(* ------- draw text ---------------- *)

Black,

{Text[Style["Number of stored polygons:",12],{4,-1}]},

{Text[Style[Length[pols],12],{8.6,-1}]},

{Text[Style["Stored polygons shown:",12],{13.5,-1}]},

{Text[Style[Floor[1/2+ showpols*Length[pols]],12],{17.6,-1}]},

(* -------store a new polygon ---------------- *)

If[store,

{store=False,

If[Length[pts]>0,

{pols=AppendTo[pols, pts],

polcols=AppendTo[polcols, col1],

pts={}

},

{}]

},{}

],

(* ------- draw new points ---------------- *)

Black,PointSize[pointsize],

Point[pts], (* draw points *)

(* ------- draw next polygon ---------------- *)

FaceForm[col1],

EdgeForm[If[outline,Thin,None]],

Opacity[opacity],Polygon[pts],

(* ------- draw stored polygons ---------------- *)

Table[

If[showpols=B3(i-.5)/Length[pols],

{FaceForm[polcols[[i]]],Polygon[pols[[i]]]}

,{}],

{i,Length[pols]}

] ,

(* -------delete current polygon ---------------- *)

If[delnew,

{delnew=False,

pts={}

},{}

],

(* -------delete all stored polygons ---------------- *)

If[delall,

{delall=False,

pols={},polcols={}

},{}

],

(* -------delete last vertex of new polygon ---------------- *)

If[delvertex,

{delvertex=False,

If[Length[pts]>0,

pts=Delete[pts,-1]

,{}]

}

,{}]

(* -------delete last shown polygon ---------------- *)

If[delmax,

{delmax=False,

If[Length[pols]>0,

With[

{i=Floor[1/2+ showpols*Length[pols]]},

{pols=Delete[pols,i],polcols=Delete[polcols,i]}

],{}]

},{}]

},

PlotRange=AE{{-.2,18.2},{-2.2, 18.2}}

], (* end graphics *)

(

{ (* here we act on the mouseclicks (we are in a ClickPane !) *)

pts=AppendTo[pts,snap[#]] (* store next vertex *)

}

)&

], (* end clickpane *)

Style["Grid Controls",Bold],

{{grid,True,Style["show grid"]},{True,False}},

{{ortho,False,Style["ortho grid"]},{True,False}},

{{gridsnap,True,Style["snap to grid\n"]},{True,False}},

Style["Global Polygon Controls",Bold],

{{outline,True,Style["outline"]},{True,False}},

{{opacity,.7,Style["opacity\n"]},0,1,ImageSize=AESmall},

Style["Controls For New Polygon",Bold],

{{col1,Cyan,Style["face
colour"]},Red,ControlType=AEColorSlider,ImageSize=AESmall},

{{pointsize,.02,Style["pointsize new polygon",Black]},
0,.03,ControlType=AESlider,ImageSize=AESmall},

{{delvertex,False,Style["delete last vertex"]},{True,False}},

{{delnew,False,Style["delete new polygon"]},{True,False}},

{{store,False,Style["store new polygon\n",Blue,Bold]},{True,False}},

Style["Controls For Stored Polygons",Bold],

{{showpols,1,Style["show stored polygons"]},0,1,ImageSize=AESmall},

{{delmax,False,Style["delete last shown"]},{True,False}},

{{delall,False,Style["delete all stored"]},{True,False}},

{{pols,{}},ControlType=AENone},

{{pts,{}},ControlType=AENone},

{{polcols,{}},ControlType=AENone},

ControlPlacement=AELeft,

SaveDefinitions=AETrue

]

```

• Prev by Date: Re: Polygon cutter
• Next by Date: Re: Defining derivatives
• Previous by thread: Re: Wolfram User Interface Research?
• Next by thread: Converting Power terms to Times terms