Polygon cutter
- To: mathgroup at smc.vnet.net
- Subject: [mg87901] Polygon cutter
- From: carlos at Colorado.EDU
- Date: Sat, 19 Apr 2008 23:49:45 -0400 (EDT)
A programming style question. The code fragments below come from conversion to Mathematica (they must work on versions >=4.0) of an ancient Fortran IV contourplot program written in 1966 for my thesis. They are key part of the inner loop "polygon cutter". ----------------------------------------------------------- Version 1. Straight translation from Fortran: p={Null}; If [tb>0&&tb==tt, p={P1,P2,P4,P3}]; If [tb==1&&tt==2, p={Pc2,P1,P2,P4,P3}]; If [tb==-1&&tt==-1,p={}]; If [tb==-1&&tt==0, p={Pc1,Pc2,Pc3}]; If [tb==-1&&tt==1, p={Pc1,P4,P3}]; If [tb==-1&&tt==2, p={Pc1,Pc2,P3,P4}]; If [tb==0&&tt==0, p={}]; If [tb==1&&tt==0, p={Pc3,Pc2,P1,P2}]; If [tb==2&&tt==0, p={Pc3,P1,P2}]; poly=Graphics[Polygon[p]]; V1 was actually a separate subroutine, with a RETURN after each match, but in Mathematica it goes inline. tb (tag-bottom) and tt (tag-top) are integers in range -1 through 2 whereas P1, ... etc, are point coordinates. ----------------------------------------------------------- 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]]; Impossible (tb,tt) combinations return {Null}. V2 was actually that implemented when V1 was later converted to Univac 1110 machine language. ---------------------------------------------------------- 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.
- Follow-Ups:
- Re: Polygon cutter
- From: "W_Craig Carter" <ccarter@mit.edu>
- Re: Polygon cutter