画正多边形来说明余数

画正多边形来说明余数

我希望绘制具有 n 条边的正多边形。每条边有相等数量的 k 条线段。以下是我能绘制的最简单的情况。你能帮助绘制其他情况吗?

正方形

\documentclass[border=.2cm]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[scale=.5]
\draw [line width=1.8pt, color=gray] (3.,9.)-- (3.,6.);
\draw [line width=1.8pt, color=gray] (3.,5.)-- (3.,2.);
\draw [line width=1.8pt, color=gray] (3.,10.)-- (6.,10.);
\draw [line width=1.8pt, color=gray] (7.,10.)-- (10.,10.);
\draw [line width=1.8pt, color=gray] (11.,10.)-- (14.,10.);
\draw [line width=1.8pt, color=gray] (15.,10.)-- (15.,7.);
\draw [line width=1.8pt, color=gray] (15.,6.)-- (15.,3.);
\draw [line width=1.8pt, color=gray] (15.,2.)-- (15.,-1.);
\draw [line width=1.8pt, color=gray] (15.,-2.)-- (12.,-2.);
\draw [line width=1.8pt, color=gray] (11.,-2.)-- (8.,-2.);
\draw [line width=1.8pt, color=gray] (7.,-2.)-- (4.,-2.);
\draw [line width=1.8pt, color=gray] (3.,-2.)-- (3.,1.);



\draw [fill=blue!66] (3.,9.) circle (4.55pt);
\draw [fill=blue!66] (3.,5.) circle (4.55pt);
\draw [fill=blue!66] (6.,10.) circle (4.55pt);
\draw [fill=blue!66] (10.,10.) circle (4.55pt);
\draw [fill=blue!66] (14.,10.) circle (4.55pt);
\draw [fill=blue!66] (15.,7.) circle (4.55pt);
\draw [fill=blue!66] (15.,3.) circle (4.55pt);
\draw [fill=blue!66] (15.,-1.) circle (4.55pt);
\draw [fill=blue!66] (12.,-2.) circle (4.55pt);
\draw [fill=blue!66] (8.,-2.) circle (4.55pt);
\draw [fill=blue!66] (4.,-2.) circle (4.55pt);
\draw [fill=blue!66] (3.,1.) circle (4.55pt);
\end{tikzpicture}

\end{document}

答案1

像这样吗?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\tikzset{
  Bullet/.style     = {fill=blue!66,draw,color=blue!66,circle,minimum size=4.55pt,scale=0.4},
}
\newcommand{\RegularPolygon}[3][3]{
\pgfmathtruncatemacro{\N}{#2}
\pgfmathtruncatemacro{\Rad}{#1}
\pgfmathsetmacro{\Step}{1/#3}
\pgfmathsetmacro{\Max}{1-1/#3}
\pgfmathsetmacro{\Len}{1.6*\Rad*sin(180/\N)/#3}
\foreach \X in {1,...,\N}
{
\coordinate (corner\X) at ({\X*360/\N+180/\N}:\Rad);
\typeout{\X\space\N}
}
\foreach \X in {1,...,\N}
{\pgfmathtruncatemacro{\Y}{mod(\X-2+\N,\N)+1}
\path[postaction={decorate,decoration={markings,
mark=between positions 0 and \Max step \Step with {
 \draw[-,line width=1.8pt,color=gray] (0,0) -- (\Len,0) node[Bullet] {};}}
        }] (corner\X)--(corner\Y);
}
}
\begin{document}
\begin{tikzpicture}
 \RegularPolygon{4}{2}
\end{tikzpicture}

\begin{tikzpicture}
 \RegularPolygon{6}{3}
\end{tikzpicture}


\begin{tikzpicture}
 \RegularPolygon{5}{5}
\end{tikzpicture}

\end{document}

在此处输入图片描述

请注意,我不清楚圆后的空段是占段的一定百分比还是某个固定量。一旦弄清楚了这一点,我很乐意在必要时调整代码。

编辑:这是一个稍微干净一些的版本。在上面,我将半径截断为整数,并且有一个不必要的\typeoutin。在这里,我还将最低边界设为水平。(这可以通过更改-90in来调整\coordinate (corner\X) at ({\X*360/\N+180/\N-90}:\Rad);。)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\tikzset{
  Bullet/.style     = {fill=blue!66,draw,color=blue!66,circle,minimum size=4.55pt,scale=0.4},
}
\newcommand{\RegularPolygon}[3][3]{
\pgfmathtruncatemacro{\N}{#2}
\pgfmathsetmacro{\Rad}{#1}
\pgfmathsetmacro{\Step}{1/#3}
\pgfmathsetmacro{\Max}{1-1/#3}
\pgfmathsetmacro{\Len}{1.6*\Rad*sin(180/\N)/#3}
\foreach \X in {1,...,\N}
{
\coordinate (corner\X) at ({\X*360/\N+180/\N-90}:\Rad);
}
\foreach \X in {1,...,\N}
{\pgfmathtruncatemacro{\Y}{mod(\X-2+\N,\N)+1}
\path[postaction={decorate,decoration={markings,
mark=between positions 0 and \Max step \Step with {
 \draw[-,line width=1.8pt,color=gray] (0,0) -- (\Len,0) node[Bullet] {};}}
        }] (corner\X)--(corner\Y);
}
}
\begin{document}
\begin{tikzpicture}
\begin{scope}
 \RegularPolygon{4}{2}
\end{scope}
\begin{scope}[xshift=6cm]
 \RegularPolygon{6}{3}
\end{scope}
\begin{scope}[yshift=-5.5cm]
 \RegularPolygon{5}{5}
\end{scope} 
\end{tikzpicture}

答案2

使用正多边形并且不使用循环开始(这可以使代码更短):

在此处输入图片描述

\documentclass{article}
\usepackage{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, shapes.geometric}

\tikzset{
every edge/.style = {-{Circle[length=5pt, color=blue!66!black, line width=0.5pt, fill=blue!66]},
                        draw=gray, line width=1.8pt, shorten >=1mm},
        RP/.style = {regular polygon,regular polygon sides=#1, minimum size=44mm}
        }    
\begin{document}
\begin{figure}
\centering
    \begin{tikzpicture}[scale=.5,baseline=(current bounding box.center)]
\node (s) [RP=3]  {};
\path (s.corner 1) edge (s.side 1) (s.side 1) edge (s.corner 2)
      (s.corner 2) edge (s.side 2) (s.side 2) edge (s.corner 3)
      (s.corner 3) edge (s.side 3) (s.side 3) edge (s.corner 1);
\end{tikzpicture}\hfil
\begin{tikzpicture}[scale=.5,baseline=(current bounding box.center)]
\node (s) [RP=4]  {};
\path (s.corner 1) edge (s.side 1) (s.side 1) edge (s.corner 2)
      (s.corner 2) edge (s.side 2) (s.side 2) edge (s.corner 3)
      (s.corner 3) edge (s.side 3) (s.side 3) edge (s.corner 4)
      (s.corner 4) edge (s.side 4) (s.side 4) edge (s.corner 1);
\end{tikzpicture}

\bigskip
\begin{tikzpicture}[scale=.5,baseline=(current bounding box.center)]
\node (s) [RP=5]  {};
\path (s.corner 1) edge (s.side 1) (s.side 1) edge (s.corner 2)
      (s.corner 2) edge (s.side 2) (s.side 2) edge (s.corner 3)
      (s.corner 3) edge (s.side 3) (s.side 3) edge (s.corner 4)
      (s.corner 4) edge (s.side 4) (s.side 4) edge (s.corner 5)
      (s.corner 5) edge (s.side 5) (s.side 5) edge (s.corner 1);
\end{tikzpicture}\hfil
\begin{tikzpicture}[scale=.5,baseline=(current bounding box.center)]
\node (s) [RP=6]  {};
\path (s.corner 1) edge (s.side 1) (s.side 1) edge (s.corner 2)
      (s.corner 2) edge (s.side 2) (s.side 2) edge (s.corner 3)
      (s.corner 3) edge (s.side 3) (s.side 3) edge (s.corner 4)
      (s.corner 4) edge (s.side 4) (s.side 4) edge (s.corner 5)
      (s.corner 5) edge (s.side 5) (s.side 5) edge (s.corner 6)
      (s.corner 6) edge (s.side 6) (s.side 6) edge (s.corner 1);
\end{tikzpicture}
\end{figure}
\end{document}

答案3

装饰可能会很方便:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.pathreplacing,calc,shapes.geometric}
\def\drawmarker#1#2{%
  \draw [line/.try] let \p1=(\tikzinputsegmentfirst),\p2=(\tikzinputsegmentlast), 
    \n1={veclen(\x2-\x1, \y2-\y1)} in
  \foreach \i [evaluate={\j=\n1*(\i-1)/(#1)+#2;\k=\n1*\i/(#1)-#2;}] in {1,...,#1}{
    ($(\p1)!\j pt!(\p2)$)--($(\p1)!\k pt!(\p2)$) 
     node [shape=circle, draw=none, minimum size=0, inner sep=0, dot/.try] {}
  };
}
\tikzset{lines/.style args={#1with sep #2}{
  decoration={
    reverse path, show path construction,
    lineto code=\drawmarker{#1}{#2},
    closepath code=\drawmarker{#1}{#2} 
  }, decorate},
  lines/.default=2,
  line/.style={thick, draw=gray},
  dot/.style={fill=blue!66, minimum size=3pt}
}
\begin{document}
\tikz\foreach \s [count=\t from 1] in {3,...,6}
  \node [lines=\t with sep 3pt, regular polygon, regular polygon sides=\s,
    minimum size=3cm] at (0,-\s*3) {};
\end{document}

在此处输入图片描述

相关内容