绘制康托集

绘制康托集

我正在尝试在 LaTeX 中绘制康托尔集,但遇到了问题。我尝试了多个\put命令,但它们只是彼此右侧移动。有人知道该怎么做吗?

任何帮助将非常感激。

答案1

只是尝试使用 MetaPost。这么晚才提出这个问题,这真的是为了好玩:距离我上次进行递归编程已经过去了很长一段时间 :-)。(递归编程无疑是解决此类任务的最佳方法。)

pair v; v = (0, -1cm);

def cantor_set(expr segm, n) =
  draw segm;
  if n>1:  
    cantor_set((point 0 of segm -- point 1/3 of segm) shifted v, n-1); 
    cantor_set((point 2/3 of segm -- point 1 of segm) shifted v, n-1); 
  fi;
enddef;

beginfig(1);
  pickup penrazor rotated 90 scaled .5cm;
  cantor_set(origin -- (12cm, 0), 6);
endfig;
end.

在此处输入图片描述

编辑:我已将“圆形”笔换成“剃刀”笔,这样绘图精度会更高。

答案2

是的,pgfmanual 在分形装饰中提供了一种方法,但找到一种个人方法更有趣。我发现用于装饰的语法不太好。

下面的代码给出了一个宏\cantor 没有 tikz。该宏需要两个参数:第一个是步骤,第二个是线的长度。

要得到 :

在此处输入图片描述

你需要写:

\unitlength=1pt 
\linethickness{3mm} 
\xlen=270pt     
\cantor{1}{270pt}\par
\cantor{2}{270pt}\par
\cantor{3}{270pt}\par
\cantor{4}{270pt}\par
\cantor{5}{270pt}\par
\cantor{6}{270pt} 

完整代码如下

\documentclass{article} 
\usepackage{fp}
\newcount\cnt \cnt=1 
\newcount\ccnt \ccnt=0
\newdimen\xpos  
\newdimen\xlen 
\newcount\cnti   
\makeatletter

\def\onestep#1{\advance\ccnt by1 }  
%%%%%%%%%%%% main macro %%%%%%%%%%%%%%%%%%%
% get a string composed with 0 and  1.  If 1 a line is drawn.
% The string  is stored in \tmp
% the length of the string is  stored in \ccnt
\def\scan#1#2\end{% 
\def\aux{#1}% 
\ifx\aux\empty 
\else 
  \ifnum #1=1 
    \ifnum\ccnt=0  \def\tmp{101}%
    \else
       \expandafter\def\expandafter\tmp\expandafter{\tmp 101}% 
    \fi
    \else 
       \expandafter\def\expandafter\tmp\expandafter{\tmp 000}%
  \fi
  \def\aux{#2}% 
  \onestep{#1}% 
  \ifx\aux\empty  
  \else 
     \scan#2\end 
  \fi
\fi
\gdef\compteur{\the\ccnt}% 
}%
%%%%%%%%%%%%% draw a line %%%%%%%%%%%%%%%%%%
\def\scandraw#1#2\end{%
\def\aux{#1}% 
\ifnum #1=1 
     \put(\strip@pt\xpos,0){\line(1,0){\strip@pt\xlen}}%
      \advance\xpos by \xlen 
  \else 
      \advance\xpos by \xlen 
\fi 
\def\aux{#2}% 
\ifx\aux\empty  
\else 
   \scandraw#2\end 
\fi
}%  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
\def\cantor#1#2{% 
\xpos=0pt \xlen=#2 
\noexpand\def\tmp{1} 
\ifnum #1>1    
  \cnti=1
  \loop 
    \ccnt=0
    \expandafter\scan \tmp\end 
    \advance\cnti by 1 %
    \ifnum \cnti<#1
  \repeat
   \FPeval{\tmpxlen}{\strip@pt\xlen/(3*\compteur)}% 
   \xlen=\tmpxlen pt   
\fi
\begin{picture}(0,15)    
 \expandafter\scandraw \tmp\end  
\end{picture}  
}%    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
\makeatother
\begin{document} 

\unitlength=1pt 
\linethickness{3mm} 
\xlen=270pt     
\cantor{1}{270pt}\par
\cantor{2}{270pt}\par
\cantor{3}{270pt}\par
\cantor{4}{270pt}\par
\cantor{5}{270pt}\par
\cantor{6}{270pt}   
\end{document}

答案3

以下是我对康托集前五行的解释:

\begin{picture}(1.1,0.40)(-0.1, -0.1)
\put(0.45, 0.27){$S^c = C$}
\put(0, 0){\line(1, 0){1}}
\put(0, -0.025){\line(0, 1){0.025}}
\put(1, -0.025){\line(0, 1){0.025}}
\put(0, -0.05){\small 0}
\put(1, -0.05){\small 1}

\put(0, 0.25){\line(1, 0){1}}

% (0, 2) / 3
\put(0.0000, 0.20){\line(1, 0){0.3333}}
\put(0.6667, 0.20){\line(1, 0){0.3333}}

% (0, 2, 6, 8) / 9
\put(0.0000, 0.15){\line(1, 0){0.1111}}
\put(0.2222, 0.15){\line(1, 0){0.1111}}
\put(0.6667, 0.15){\line(1, 0){0.1111}}
\put(0.8889, 0.15){\line(1, 0){0.1111}}

% (0, 2, 6, 8, 18, 20, 24, 26) / 27
\put(0.0000, 0.10){\line(1, 0){0.037}}
\put(0.0741, 0.10){\line(1, 0){0.037}}
\put(0.2222, 0.10){\line(1, 0){0.037}}
\put(0.2963, 0.10){\line(1, 0){0.037}}
\put(0.6667, 0.10){\line(1, 0){0.037}}
\put(0.7407, 0.10){\line(1, 0){0.037}}
\put(0.8889, 0.10){\line(1, 0){0.037}}
\put(0.9630, 0.10){\line(1, 0){0.037}}

% (0, 2, 6, 8, 18, 20, 24, 26, 54, 56, 60, 62, 72, 74, 78, 80) / 81
\put(0.0000, 0.05){\line(1, 0){0.0123}}
\put(0.0247, 0.05){\line(1, 0){0.0123}}
\put(0.0741, 0.05){\line(1, 0){0.0123}}
\put(0.0988, 0.05){\line(1, 0){0.0123}}
\put(0.2222, 0.05){\line(1, 0){0.0123}}
\put(0.2469, 0.05){\line(1, 0){0.0123}}
\put(0.2963, 0.05){\line(1, 0){0.0123}}
\put(0.3210, 0.05){\line(1, 0){0.0123}}
\put(0.6667, 0.05){\line(1, 0){0.0123}}
\put(0.6914, 0.05){\line(1, 0){0.0123}}
\put(0.7407, 0.05){\line(1, 0){0.0123}}
\put(0.7654, 0.05){\line(1, 0){0.0123}}
\put(0.8889, 0.05){\line(1, 0){0.0123}}
\put(0.9136, 0.05){\line(1, 0){0.0123}}
\put(0.9630, 0.05){\line(1, 0){0.0123}}
\put(0.9877, 0.05){\line(1, 0){0.0123}}

\end{picture}

答案4

尽管我强烈建议使用tikz,但这里有一个使用该命令的示例\put

语法是\put(x,y){}其中(x,y)是起点的坐标,要放置在此坐标处的对象作为括号内的参数传入{}。下面我用命令将此对象指定为一条线\line(u,v){n}。在本例中(u,v)是线的方向矢量,并且此线的长度作为传入n

在此处输入图片描述

这是上图的代码。如果这没有帮助,请以此为起点创建一个小段代码这说明了您遇到的问题。

\documentclass{article}
\begin{document}
\setlength{\unitlength}{1cm}
\begin{picture}(2, 2)
\put(0,0){\line(0,1){3}}% Vertical line of length 3 starting at (0,0)
\put(0,2){\line(1,0){1}}% Horizontal line of length 1 starting at (0,2)
\put(0,1){\line(1,0){5}}% Horizontal line of length 5 starting at (0,1)
\end{picture}
\end{document}

相关内容