How to generate a polygon number figure using metapost?

253 views Asked by At

I'm learning metapost and i want to know how to generate a figure like a polygon number http://en.wikipedia.org/wiki/Polygonal_number , i got triangular numbers but not hexagonal.

Thanks for help.

1

There are 1 answers

0
Charles Stewart On BEST ANSWER

It occurred to me that if you don't care about drawing points and lines upon each other, then this problem is pretty easy. The following is as ugly as sin, mostly because of the definition of dot using just fullcircle scaled, but it works:

beginfig(1);
pair right, nright;
u:=1cm; right:=(u,0);
path p,q,dot,seg;
dot:=fullcircle scaled (u/2);
seg:=(0,0)-- (dot shifted right);
for N=1 upto 6:
  p:=dot; nright:=(N*u,0);
  for i=1 upto N:
    p:=seg -- (p shifted right);
  endfor
  q:=(0,0);
  for j=1 upto 6:
    q:=p -- ((q rotated 60) shifted nright);
  endfor
  draw q;
endfor
endfig

This approach should work for all of the n-gonal numbers.