Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1992
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1992

[Date Index] [Thread Index] [Author Index]

Search the Archive

re: Plotting a polygon

  • To: mathgroup at yoda.physics.unc.edu
  • Subject: re: Plotting a polygon
  • From: tgayley (Todd Gayley)
  • Date: Tue, 15 Dec 1992 16:17:20 -0600

Oslo Kolemainen <koleh%joyl.dnet at joyx.joensuu.fi> asks:

>Hi Mathgroup !
>
>What is the problem here? My student tried to plot a polygon in a
>complicated way.
>
>Mathematica 2.0 for MS-DOS 386/7
>Copyright 1988-91 Wolfram Research, Inc.
>
>In[1]:= t=Table[Random[],{5},{2}];
>
>This works:
>
>In[2]:= Show[Graphics[Map[Line,Partition[AppendTo[t,First[t]],2,1]]]]
>
>Out[2]= -Graphics-
>
>But when he uses a function polygon[], it does not work.
>
>In[3]:= polygon[t_]:=
>        Show[Graphics[Map[Line,Partition[AppendTo[t,First[t]],2,1]]]]
>
>In[4]:= polygon[t]
>
>AppendTo::rvalue: 
>   {{0.10459, 0.840989}, {0.177361, 0.503785}, <<3>>, {0.10459, 0.840989}}
>     has not been assigned a value.
>
>AppendTo::argr: AppendTo called with 1 argument; 2 arguments are expected.
>
>AppendTo::argr: AppendTo called with 1 argument; 2 arguments are expected.
>
>Graphics::gprim: 
>   Unknown Graphics primitive AppendTo[Line[AppendTo[<<2>>]]] encountered.
>
>Out[4]= -Graphics-
>
>In[5]:= ?polygon
>Global`polygon
>
>polygon[t_] := Show[Graphics[Line /@ Partition[AppendTo[t, First[t]], 2, 1]]]
>
>Osmo Kolemainen
>University of Joensuu
>Finland 

The problem here is that the AppendTo assigns back into the variable t.
This means that t must have an lvalue. But t is a dummy variable that gets
replaced with its value (a list) before any evaluation takes place. You
can't say

   AppendTo[{1,2,3},4]

any more than you can say

   {1,2,3} = Append[{1,2,3},4]

In short, you can't assign into a dummy variable. The general way to fix
this type of problem is to make the code into a Module with a local
variable that is assigned the value of t initially:

polygon[t_]:= Module[{x=t},
               
Show[Graphics[Map[Line,Partition[AppendTo[x,First[x]],2,1]]]]
              ]

In the present case, there is an easier fix. There is no need to use
AppendTo here -- Append works just as well. This situation is like using +=
when you really only want +.

polygon[t_]:= Show[Graphics[Map[Line,Partition[Append[t,First[t]],2,1]]]]


---------------------
Todd Gayley
WRI Technical Support






  • Prev by Date: Problem with MatrixPower, v. 2.1
  • Next by Date: 3D graphics
  • Previous by thread: Plotting a polygon
  • Next by thread: problems with mathematica