![带角度标签的不规则多边形](https://linux22.com/image/269020/%E5%B8%A6%E8%A7%92%E5%BA%A6%E6%A0%87%E7%AD%BE%E7%9A%84%E4%B8%8D%E8%A7%84%E5%88%99%E5%A4%9A%E8%BE%B9%E5%BD%A2.png)
我希望创建下面的 15 边不规则多边形,而不必声明每个点的坐标。
有没有更简单的方法可以做到这一点?Tikz?PGF?
答案1
以下是根据内角列表绘制此多边形的方法。关键点是命令\pgfextra{...}
,它允许您在不中断路径构建的情况下执行代码,以及环境内\path
带有关键字的命令,用于查找最终角。name path
pgfinterruptboundingbox
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, intersections}
\begin{document}
\begin{tikzpicture}
\def\totalangle{0}
\draw (0,0) -- (0:1cm) \foreach \angle in {152,165,167,160,160,150,150,150,170,145,170,155,170,155}{
\pgfextra{ % Calculate the current direction
\pgfmathparse{180-\angle+\totalangle}
\xdef\totalangle{\pgfmathresult}
}
-- ++(\totalangle:1cm) node [pos=0, circle, anchor=(\totalangle+\angle/2+180), inner sep=0pt] {$\angle^\circ$}
} coordinate (final);
\pgfmathsetmacro\unknownangle{\totalangle-180}
% The final point is on the intersection between the extensions of the first and last segments.
% Interrupt the bounding box, so we can use long paths for finding the intersection.
\begin{pgfinterruptboundingbox}
\path [name path global=horizontal] (-20cm,0pt) -- (20cm,0pt);
\path [name path global=lastsegment] (final) -- +(\totalangle:30cm);
\end{pgfinterruptboundingbox}
\draw [name intersections={of=horizontal and lastsegment}]
(final) -- (intersection-1)
node [anchor=south west] {$x$}
-- (0,0);
\end{tikzpicture}
\end{document}
答案2
以下是改编循环序列图(tikz?)这将适应任何根据提供的标签数量确定段数。
代码:
\documentclass{article}
\usepackage{tikz}
\usepackage{xstring}
% https://tex.stackexchange.com/questions/21559/macro-to-access-a-specific-member-of-a-list/21560#21560
\newcommand*\GetListMember[2]{\StrBetween[#2,\number\numexpr#2+1]{,#1,},,\par}%
\newlength{\MidRadius}
\newcommand{\LastAngle}{}%
\newcommand*{\CircularSequence}[3]{%
% #1 = outer circle radius
% #2 = inner circle radius
% #3 = seqeunce
\StrCount{#3}{,}[\NumberOfElements]
\pgfmathsetmacro{\AngleSep}{360/(\NumberOfElements+1)}
\pgfmathsetlength{\MidRadius}{(#1+#2)/2}
\foreach [count = \Count] \Angle in {0,\AngleSep,..., 360} {%
\IfStrEq{\LastAngle}{}{}{%
\draw [blue, ultra thick] (\LastAngle:#1) -- (\Angle:#1);
}%
\xdef\LastAngle{\Angle}% Save it so we can access it next iteration
\pgfmathsetmacro{\MidPoint}{\Angle+\AngleSep/2}
\node at (\MidPoint:\MidRadius) {\GetListMember{#3}{\Count}};
}%
}%
\begin{document}
\begin{tikzpicture}
\CircularSequence{4.0cm}{3.0cm}{170,155,170,165,$x$,163,155,167,170,160,140,150,172,170,145}
\end{tikzpicture}
\end{document}