Re: Polygon cutter
- To: mathgroup at smc.vnet.net
- Subject: [mg88028] Re: Polygon cutter
- From: carlos at Colorado.EDU
- Date: Tue, 22 Apr 2008 06:28:20 -0400 (EDT)
- References: <200804200349.XAA11201@smc.vnet.net> <fuhf9s$ib1$1@smc.vnet.net>
On Apr 21, 12:39 pm, Mark Fisher <particlefil... at gmail.com> wrote:
> On Apr 21, 6:41 am, car... at colorado.edu wrote:
>
>
>
> > On Apr 21, 1:21 am, "W_Craig Carter" <ccar... at mit.edu> wrote:
>
> > > Hello Carlos,
> > > I believe the interpretation of good style is a bit like ethics--it's
> > > easy to agree in extreme cases, but the in-between cases are
> > > subjective.
>
> > > I think this depends on whether you intend to modify the code in the
> > > future. I believe V1 is easier to read, and if you think that someone
> > > else may wish to modify it, then I recommend sticking with that.
> > > However,"Which" will probably be more efficient, and rank the order of=
> > > tests with decreasing frequency.
>
> > > V2 looks cool--some people like that. There is probably a way to=
> > > improve the coolness even more, and remove the need to return the
> > > impossible "Null"s. Creating V2 probably hones your skills and is
> > > more interesting to do, which is similarly important; but probably has=
> > > a direct impact on a broader interpretation of efficiency.
>
> > > On Sat, Apr 19, 2008 at 11:49 PM, <car... at colorado.edu> wrote:
> > > > A programming style question. The code fragments below come
> > > > Version 1. Straight translation from Fortran:
>
> > > > p={Null};
> > > > If [tb>0&&tb==tt, p={P1,P2,P4,P3}];
>
> > > :
> > > :
>
> > > > poly=Graphics[Polygon[p]];
>
> > > > Version 2. Table driven variant of above:
>
> > > > p={{{},{Pc1,Pc2,Pc3},{Pc1,P4,P3},{Pc1,Pc2,P3,P4}},
> > > > {{Null},{},{Null},{Null}},
> > > > {{Null},{Pc3,Pc2,P1,P2},{P1,P2,P4,P3},{Pc2,P1,P2,P4,P3}},
> > > > {{Null},{Pc3,P1,P2},{Null},{P1,P2,P4,P3}}} [[tb+2,tt+2]];
> > > > poly=Graphics[Polygon[p]];
>
> > > > Question: which version is preferable in Mathematica, or is
> > > > there a better one? Both run roughly at the same speed
> > > > (about 25 microsec under 5.2 on an Intel MacBook Pro).
> > > > Since this loop is traversed once for each polygon,
> > > > efficiency is important.
>
> > > --
> > > W. Craig Carter
>
> > Your suggestion of looking at the Which construct was an interesting
> > one -
> > didn't know it existed. So I tried it as
>
> > p={Null};
> > Which [tb>0&&tb==tt, p={P1,P2,P4,P3}, tb==1&&tt==2,=
> > p={Pc2,P1,P2,P4,P3},
> > tb==-1&&tt==-1,p={}, =
tb==-1&&tt==0=
> > ,
> > p={Pc1,Pc2,Pc3},
> > tb==-1&&tt==1, p={Pc1,P4,P3}, t=
b==-1&&tt==2,
> > p={Pc1,Pc2,P3,P4},
> > tb==0&&tt==0, p={}, =
tb==1&&tt==0,
> > p={Pc3,Pc2,P1,P2},
> > tb==2&&tt==0, p={Pc3,P1,P2}];
> > poly=Graphics[Polygon[p]];
>
> > The two most common cases are the first two. I expected this to be
> > quicker than V1
> > but it wasnt. On a MacBook Pro under 5.2:
>
> > V1 25 microsec/polygon
> > V2 24
> > Which 30
>
> > Similar rankings for 4.1 and 4.2. Havent tested it on 6.0.
>
> How about using Switch?
>
> switchfun[{tb_, tt_}]:=
> Switch[{tb, tt},
> {tb, tb} /; tb > 0, {P1, P2, P4, P3},
> {1, 2}, {Pc2, P1, P2, P4, P3},
> {-1, 0}, {Pc1, Pc2, Pc3},
> {-1, 1}, {Pc1, P4, P3},
> {-1, 2}, {Pc1, Pc2, P3, P4},
> {1, 0}, {Pc3, Pc2, P1, P2},
> {2, 0}, {Pc3, P1, P2},
> {_, _}, {}
> ]
>
> Graphics@Polygon@switchfun@{tb,tt}
>
> Note, in V6 Graphics[Polygon[{}]] is OK but Graphics[Polygon[{Null}]]
> produces an error. And the same is true for Graphics3D.
>
> --Mark
{Null} IS intended to trigger an error, since those tag combinations,
in the famous Unix phrase, "cant happen"
- References:
- Polygon cutter
- From: carlos@Colorado.EDU
- Polygon cutter