
저는 아직 Tikz를 처음 사용하는 초보입니다... TikZ를 사용하여 바퀴 모양의 그래프를 그려보려고 합니다. 기본 그래프를 완성했지만 예상한 대로 나오지 않습니다. 내 그래프의 가장자리가 중앙에 제대로 정렬되지 않았습니다.
가장자리는 원 표현에서 멈추는 노드 사이에 최단 경로 직선을 그리는 대신 좌표를 따르는 것처럼 보입니다. 일부 가장자리는 괜찮지만(예: v1에서 v2, v1에서 v6, ...) 일부 가장자리는 그렇지 않습니다(예: v2에서 v3).
다음은 TikZ 사진에 대한 코드와 내가 얻은 결과를 보여주는 이미지입니다.
\begin{tikzpicture}[auto, scale=0.9]
\tikzstyle{vertex}=[draw, circle, inner sep=0.55mm]
\node (v1) at (0,0) [vertex] {};
\node (v2) at (1,0) [vertex] {};
\node (v3) at (1.5,-1) [vertex] {};
\node (v4) at (1,-2) [vertex] {};
\node (v5) at (0,-2) [vertex] {};
\node (v6) at (-.5,-1) [vertex] {};
\node (v7) at (.5,-1) [vertex, fill=blue] {};
\foreach \x in {2, 3, 4, 5, 6, 7}{
\pgfmathsetmacro\y{\x - 1}
\draw (v\y) to (v\x);
}
\draw (v6) to (v1);
\draw (v5) to (v7);
\draw (v4) to (v7);
\draw (v3) to (v7);
\end{tikzpicture}
답변1
그 이유는 \y 계산이 정수를 제공하지 않기 때문입니다. 두 가지 가능성이 있습니다:
\pgfmathtruncatemacro
첫 번째는 대신 매크로를 사용하는 것입니다 .\pgfmathsetmacro
- 두 번째는
\y
foreach 루프 자체 내에서 평가하는 것입니다.
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[auto, scale=0.9]
\tikzstyle{vertex}=[draw, circle, inner sep=0.55mm]
\node (v1) at (0,0) [vertex] {};
\node (v2) at (1,0) [vertex] {};
\node (v3) at (1.5,-1) [vertex] {};
\node (v4) at (1,-2) [vertex] {};
\node (v5) at (0,-2) [vertex] {};
\node (v6) at (-.5,-1) [vertex] {};
\node (v7) at (.5,-1) [vertex, fill=blue] {};
\foreach \x[evaluate=\x as \y using int(\x-1)] in {2, 3, 4, 5, 6, 7}{
%\pgfmathtruncatemacro\y{\x - 1}
\draw (v\y) to (v\x);
}
\draw (v6) to (v1);
\draw (v5) to (v7);
\draw (v4) to (v7);
\draw (v3) to (v7);
\end{tikzpicture}
\end{document}
답변2
\tikzstyle
은 더 이상 사용되지 않으며 문제는 \pgfmathsetmacro
정수를 생성하지 않지만 노드 앵커로 해석되는 2.0
것과 같은 것입니다..0
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[auto, scale=0.9]
\tikzset{vertex/.style={draw, circle, inner sep=0.55mm}}
\node (v1) at (0,0) [vertex] {};
\node (v2) at (1,0) [vertex] {};
\node (v3) at (1.5,-1) [vertex] {};
\node (v4) at (1,-2) [vertex] {};
\node (v5) at (0,-2) [vertex] {};
\node (v6) at (-.5,-1) [vertex] {};
\node (v7) at (.5,-1) [vertex, fill=blue] {};
\foreach \x [remember =\x as \lastx (initially 1)] in {2, 3, 4, 5, 6, 7}{
\draw (v\lastx) to (v\x);
}
\draw (v6) to (v1);
\draw (v5) to (v7);
\draw (v4) to (v7);
\draw (v3) to (v7);
\end{tikzpicture}
\end{document}
답변3
어쩌면 당신은 좋아할 것입니다 :
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[
vertex/.style = {circle, draw, fill=#1, inner sep=0.5mm}
]
%
\node (s) [regular polygon,regular polygon sides=6,
draw, minimum size=20mm, above] at (0.5,-2) {};
\draw (s.corner 3) -- (s.corner 6);
\node (c) [vertex=blue] at (s.center) {};
%
\foreach \i in {1,...,6}{\node (s\i) [vertex=white] at (s.corner \i) {}; }
\draw (c) -- (s4)
(c) -- (s5);
\end{tikzpicture}
\end{document}
답변4
더 많은 휠을 얻으려면 극좌표를 사용하여 원점에서 45도 떨어진 (45:1)
거리에 노드를 그릴 수 있습니다. 1
다음은 노드 수가 가변적인 대체 버전입니다. \numNodes{6}
원 안의 노드 수를 변경하려면 숫자를 변경하세요 .
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
auto,
scale=0.9,
vert/.style={draw, circle, inner sep=0.55mm,fill=white}
]
\newcommand\numNodes{6}
\node[vert,fill=blue] (vC) at (0,0){};
\draw (0:1) node[vert](v0) {}
\foreach \n [evaluate = \n as \deg using {\n*360/\numNodes}] in {1,2,...,\numNodes}{
-- (\deg:1) node[vert](v\n) {}
};
\foreach \n in {0,3,4,5}{
\draw (vC) -- (v\n);
}
\end{tikzpicture}
\end {document}