概述

概述

概述

希望创建具有与网格对齐的规则多边形的背景。

问题

尽管多边形的半径是根据页面宽度计算的,但多边形之间的间距会有所不同。请注意六边形之间的间距:

正六边形

使用四边形时,空间感更加突出:

正四边形

Aunitsquare没有提供空间:

正矩形

类似地,unitcircles 也是齐平的:

正圆边形

代码

一个相当简短的例子:

\setupcolors[state=start]

\definecolor[BaseColour][h=66CEF1]
\ctxlua{math.randomseed(os.time())}

\startuseMPgraphic{ThemeBase}
  fill Page withcolor transparent(1, .5, \MPcolor{BaseColour} );
\stopuseMPgraphic

\startuseMPgraphic{page:ThemeElement}
  tiles_x := 8;
  tiles_y := 13;
  dim_x := OverlayWidth / tiles_x;
  dim_y := dim_x;
  color baseColour;
  baseColour := \MPcolor{BaseColour};

  % Square
  %s := unitsquare scaled dim_x;
  % Circle
  %s := unitcircle scaled dim_x;
  % Triangle
  %s := (0, 0) -- (dim_x/2, dim_y) -- (-dim_x/2, dim_y)  -- cycle;

  polygon_points := 4;
  polygon_angle := 2 * pi / polygon_points;
  % Rotate polygon to align edge along horizontal
  polygon_rotation := 3 pi / 2 - pi / polygon_points;
  polygon_radius := dim_x;

  path s;
  s := (0, 0);

  for i = 0 upto polygon_points:
    x := polygon_radius * cos( i * polygon_angle + polygon_rotation );
    y := polygon_radius * sin( i * polygon_angle + polygon_rotation );
    s := s -- (x, y);
  endfor;

  s := s -- cycle;
  s := s scaled 0.5;

  for x = 0 upto tiles_x:
    for y = 0 upto tiles_y:
      % Pick a random colour deviation that differs from the background
      % and differs from the darker colour.
      deviationLight :=

      if (round( uniformdeviate( 1000 )) mod 2 = 0):
        (.2 + uniformdeviate(.15));
      else:
        (.55 + uniformdeviate(.15));
      fi

      deviationDark := uniformdeviate( .025 );

      def lightColour = deviationLight [baseColour, white] enddef;
      def darkColour = deviationDark [baseColour, black] enddef;

      filldraw s shifted( x * polygon_radius, y * polygon_radius ) withcolor

      if (x mod 2) = (y mod 2):
        darkColour;
      else:
        lightColour;
      fi
    endfor;
  endfor;

  setbounds currentpicture to
    boundingbox currentpicture shifted( -dim_x/2, dim_y*(tiles_x/tiles_y)/2 );
\stopuseMPgraphic

\defineoverlay[ThemeBase][\uniqueMPgraphic{ThemeBase}]
\defineoverlay[page:ThemeElement][\uniqueMPgraphic{page:ThemeElement}]

\starttext
  \setupbackgrounds[page][background={ThemeBase,page:ThemeElement}]
  \startchapter[title=One]
  \input knuth
  \stopchapter
\stoptext

有问题的源代码是这样的:

  s := s scaled 0.5;

由于某种原因,多边形必须缩放,但我不明白为什么。我原以为以下作业会在页面上生成具有相同外接圆半径的正多边形,而不管边数是多少:

x := polygon_radius * cos( i * polygon_angle + polygon_rotation );
y := polygon_radius * sin( i * polygon_angle + polygon_rotation );

问题

如何使用 MetaPost 在 ConTeXt 中生成大小均匀、不重叠的规则多边形?(即相当于“unitregularpolygon”)。

有关的

答案1

像这样吗?

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述 在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

\setupcolors[state=start]

\definecolor[BaseColour][h=66CEF1]
\ctxlua{math.randomseed(os.time())}

\startuseMPgraphic{ThemeBase}
  fill Page withcolor transparent(1, .5, \MPcolor{BaseColour} );
\stopuseMPgraphic

\startuseMPgraphic{page:ThemeElement}
  tiles_x := 8;
  dim_x := OverlayWidth / tiles_x;
  color baseColour;
  baseColour := \MPcolor{BaseColour};

  polygon_points := 3;  
  phi:=180/polygon_points;

  dx:=dim_x;

  if((polygon_points mod 4) = 0):
    polygon_radius := dx/2/cosd(phi);
  else: 
    polygon_radius := 1/2*dx/cosd((2*floor(1/4*polygon_points)+1)*phi-90);
  fi

  pair base_point; 
  base_point := ((0,-polygon_radius) rotated phi);

  path s;
  s := base_point;

  for i = 1 upto polygon_points:
    s := s -- (base_point rotated (2*phi*i));
  endfor;

  s := s -- cycle;

  if((polygon_points mod 2) = 1):
    dy:=polygon_radius+abs(ypart(base_point));
  else: 
    dy:=abs(2*ypart(base_point));
  fi

  tiles_y := floor(abs(OverlayHeight/dy))+1;


  for j := 0 upto tiles_x-1:
    for i := 0 upto tiles_y-1:
      % Pick a random colour deviation that differs from the background
      % and differs from the darker colour.
      deviationLight := 
        if (round( uniformdeviate( 1000 )) mod 2 = 0):
          (.2 + uniformdeviate(.15));
        else:
          (.55 + uniformdeviate(.15));
        fi

      deviationDark := uniformdeviate( .025 );

      def lightColour = deviationLight [baseColour, white] enddef;
      def darkColour = deviationDark [baseColour, black] enddef;

      filldraw s shifted(j * dx, i * dy ) withcolor
        if (j mod 2) = (i mod 2):
          darkColour;
        else:
          lightColour;
        fi
    endfor;
  endfor;

  setbounds currentpicture to
    boundingbox currentpicture shifted( 0, -(OverlayHeight-tiles_y*dy)/2);
\stopuseMPgraphic

\defineoverlay[ThemeBase][\uniqueMPgraphic{ThemeBase}]
\defineoverlay[page:ThemeElement][\uniqueMPgraphic{page:ThemeElement}]

\starttext
  \setupbackgrounds[page][background={ThemeBase,page:ThemeElement}]
  \startchapter[title=One]
  \input knuth
  \stopchapter
\stoptext

相关内容