无限三角形(科赫雪花)并合并成一条线

无限三角形(科赫雪花)并合并成一条线

我试图制作以下图像:

无限三角形(抱歉,我不知道这个序列的正式名称)

无论如何,我试图找到一种方法来创建它,并且我从另一个用户那里找到了这个代码,所以我对其进行了如下操作:

\documentclass[11pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{asy}
\begin{document}
\begin{asy}
size(100);
pair t = dir(150);
int d = 0;

pair[] points = {t};

void forward(real dist) {
pair oldT = t;
t = oldT + dist*dir(d);
draw(oldT--t);
pair[] p = {t};
points.append(p);
}

void left(int angle) {
d = d + angle;
}

int[] angles = {60,-120,60,0};

void koch(int order, real size) {
if (order == 0) {
forward(size);
}
else {
for (int i = 0; i < angles.length; i += 1) {
koch(order-1, size/3);
left(angles[i]);
}
}
}

koch(0,sqrt(3));

void drawArray(pair[] arr) {
for (int i = 1; i<arr.length; i+=1) {
if (i == 1) {
draw(arr[0]--arr[1]);
}
else {
draw(arr[i-1]--arr[i]);
}
}
}

pair[] pointsLeft = new pair[points.length];
pair[] pointsRight = new pair[points.length];

for (int i = 0; i < points.length; i+=1) {
pointsLeft[i] = points[i] * dir(120);
pointsRight[i] = points[i] * dir(-120);
}

drawArray(pointsLeft);
drawArray(pointsRight);
\end{asy}

\begin{asy}
size(100);
pair t = dir(150);
int d = 0;

pair[] points = {t};

void forward(real dist) {
pair oldT = t;
t = oldT + dist*dir(d);
draw(oldT--t);
pair[] p = {t};
points.append(p);
}

void left(int angle) {
d = d + angle;
}

int[] angles = {60,-120,60,0};

void koch(int order, real size) {
if (order == 0) {
forward(size);
}
else {
for (int i = 0; i < angles.length; i += 1) {
koch(order-1, size/3);
left(angles[i]);
}
}
}

koch(1,sqrt(3));

void drawArray(pair[] arr) {
for (int i = 1; i<arr.length; i+=1) {
if (i == 1) {
draw(arr[0]--arr[1]);
}
else {
draw(arr[i-1]--arr[i]);
}
}
}

pair[] pointsLeft = new pair[points.length];
pair[] pointsRight = new pair[points.length];

for (int i = 0; i < points.length; i+=1) {
pointsLeft[i] = points[i] * dir(120);
pointsRight[i] = points[i] * dir(-120);
}

drawArray(pointsLeft);
drawArray(pointsRight);
\end{asy}

\begin{asy}
size(100);
pair t = dir(150);
int d = 0;

pair[] points = {t};

void forward(real dist) {
pair oldT = t;
t = oldT + dist*dir(d);
draw(oldT--t);
pair[] p = {t};
points.append(p);
}

void left(int angle) {
d = d + angle;
}

int[] angles = {60,-120,60,0};

void koch(int order, real size) {
if (order == 0) {
forward(size);
}
else {
for (int i = 0; i < angles.length; i += 1) {
koch(order-1, size/3);
left(angles[i]);
}
}
}

koch(2,sqrt(3));

void drawArray(pair[] arr) {
for (int i = 1; i<arr.length; i+=1) {
if (i == 1) {
draw(arr[0]--arr[1]);
}
else {
draw(arr[i-1]--arr[i]);
}
}
}

pair[] pointsLeft = new pair[points.length];
pair[] pointsRight = new pair[points.length];

for (int i = 0; i < points.length; i+=1) {
pointsLeft[i] = points[i] * dir(120);
pointsRight[i] = points[i] * dir(-120);
}

drawArray(pointsLeft);
drawArray(pointsRight);
\end{asy}
\begin{document}

我得到了这个(粉色/橙色背景来自文本框):

我的工作

我想知道如何将这三个图形以合理的间距组合成一条线,以及它下面的 $P_n$ 以及右边的 $\cdots$,尽管这不是必要的。谢谢!

答案1

还有一个选择:

// Koch.asy
//    run `asy Koch.asy`
//    to get a standalone  `Koch.pdf`

settings.tex="pdflatex";
import fontsize;defaultpen(fontsize(7.5pt));
texpreamble("\usepackage{lmodern}"
   +"\usepackage{amsmath}"
   +"\usepackage{amsfonts}"
   +"\usepackage{amssymb}"
);

guide gKoch(int n, pair A, pair B){
  guide g;
  pair C,D,E;
  if(n>0){
    C=A+(B-A)/3; E=B+(A-B)/3;
    D=rotate(-60,C)*E;
    g=gKoch(n-1,A,C)--gKoch(n-1,C,D)--gKoch(n-1,D,E)--gKoch(n-1,E,B);
  }else{
    g=A--B;
  }
  return g;
}

guide KochFlake(int n, pair A, pair B, pair C){
  return gKoch(n,A,B)--gKoch(n,B,C)--gKoch(n,C,A)--cycle;
}

int nMax=6; 

pen linePen=darkblue+0.3bp;

pair A,B,C,D;
real a=1;
A=(0,a); D=(0,-a);
B=rotate( 120)*A; C=rotate(-120)*A;

transform sh=identity();
for(int i=0;i<nMax;++i){
  draw(sh*KochFlake(i,A,B,C),linePen);
  label("$P_{"+string(i)+"}$",sh*D,plain.S);
  sh*=shift(2*a,0);
}

size(nMax*3*a*cm,2*a*cm);

在此处输入图片描述


更新

这是内联版本:

\documentclass[12pt]{article}
\usepackage[inline]{asymptote}

\title{2D Graphics with Asymptote}
\author{The Asymptote Project}
\begin{document}
\maketitle

\begin{asydef}
//
// Global Asymptote definitions can be put here.
//
    import fontsize;defaultpen(fontsize(7.5pt));
    texpreamble("\usepackage{lmodern}"
       +"\usepackage{amsmath}"
       +"\usepackage{amsfonts}"
       +"\usepackage{amssymb}"
    );

    guide gKoch(int n, pair A, pair B){
      guide g;
      pair C,D,E;
      if(n>0){
        C=A+(B-A)/3; E=B+(A-B)/3;
        D=rotate(-60,C)*E;
        g=gKoch(n-1,A,C)--gKoch(n-1,C,D)--gKoch(n-1,D,E)--gKoch(n-1,E,B);
      }else{
        g=A--B;
      }
      return g;
    }
    
    guide KochFlake(int n, pair A, pair B, pair C){
      return gKoch(n,A,B)--gKoch(n,B,C)--gKoch(n,C,A)--cycle;
    }
    
    pen linePen=darkblue+0.3bp;
    
    void KochFlakesDiagram(int nFirst=0, int nLast){
      pair A,B,C,D;
      real a=1;
      A=(0,a); D=(0,-a);
      B=rotate( 120)*A; C=rotate(-120)*A;
    
      transform sh=identity();
    
      for(int i=nFirst;i<=nLast;++i){
        draw(sh*KochFlake(i,A,B,C),linePen);
        label("$P_{"+string(i)+"}$",sh*D,plain.S);
        sh*=shift(2*a,0);
      }
      size((nLast-nFirst)*3*a*cm,2*a*cm);
    }

\end{asydef}

Here is a Koch flakes diagram produced with Asymptote

\begin{center}
\begin{asy}
KochFlakesDiagram(0,3);
\end{asy}
\end{center}

\begin{center}
\begin{asy}
KochFlakesDiagram(4,7);
\end{asy}
\end{center}

\end{document}

在此处输入图片描述

答案2

\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}

在此处输入图片描述

%\documentclass[tikz, border=5pt]{standalone}
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}

\def\a{3.14}

\pgfmathsetmacro\p{2*sqrt(3)/3+0.3}
\tikzset{koch/.style={
insert path={%
 lindenmayer system[very thin, fill=lightgray,
l-system={  rule set={F -> F-F++F-F},  axiom=F++F++F,
step=\a cm/3^#1, angle=60, order=#1,  
anchor=center 
}] -- cycle node[below=0.6*\a cm]{$P_{#1}$}
}    }   }

\begin{document}
%\tikz \draw[] (0,0) [koch=7]; 

\begin{tikzpicture}
\begin{groupplot}[group style={group size=3 by 99,
vertical sep=\p cm, %horizontal sep=5mm, 
}, 
height=1.5*\a cm, width=1.5*\a cm, clip=false, hide axis, 
]
\pgfplotsforeachungrouped \n in {0,...,6}{%%
\edef\tmp{
        \noexpand\nextgroupplot%[title=\x]
        \noexpand\addplot[draw=none] coordinates {(0,0)  (\a,\a)};
        \noexpand\draw[] (0.5*\a,0.5*\a) [koch=\n]; 
}\tmp}%%
\nextgroupplot
\addplot[draw=none] coordinates {(0,0)  (\a,\a)};
\node[font=\Huge] at (0.5*\a,0.5*\a) {\dots}; 
\end{groupplot}
\end{tikzpicture}
\end{document}

相关内容