帮助使用 tikz 绘图

帮助使用 tikz 绘图

我需要按照图片的方式绘制这些图形。不幸的是我不能。我发布了我的代码。

在此处输入图片描述

代码1:

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{angles,quotes}
\usetikzlibrary{arrows,arrows.meta}
\pagestyle{empty}
\newcommand{\degre}{\ensuremath{^\circ}}
% \renewcommand{\rmdefault}{phv}
\renewcommand{\sfdefault}{phv}

\usepackage{sansmath}

\definecolor{uququq}{rgb}{0.25098039215686274,0.25098039215686274,0.25098039215686274}
\tikzset{
    quote/.style={{|[width=2mm]}-{|[width=2mm]}}
}

\begin{document}
\begin{tikzpicture}[> = stealth]
\coordinate (a) at (0,0);
\coordinate (b) at (3,3);


\coordinate (c) at (3,0);
\coordinate (d) at (0,3);


\coordinate (g) at (6,3);
\coordinate (h) at (3,6);

\coordinate (i) at (6,6);


\coordinate (e) at (5,5);
\coordinate (f) at (5,1);


\draw[black,step=1cm] (0,0) grid +(6cm,6cm);

\draw[line width=0.3mm, red, -]  (b) -- node[above left] {\Huge \textbf{}} (a);

\draw[line width=0.3mm, red, -]  (c) -- node[right] {\Huge \textbf{}} (d);
%\draw[ultra thick,blue,->]  (a) -- node[below left] {\Large $\vec{d}$} (c);

\draw[line width=0.3mm, green, -]  (g) -- node[right] {\Huge \textbf{}} (h);

\draw[line width=0.3mm, green, -]  (b) -- node[right] {\Huge \textbf{}} (i);
\end{tikzpicture}
\end{document}

代码2:

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{angles,quotes}
\usetikzlibrary{arrows,arrows.meta}
\pagestyle{empty}
\newcommand{\degre}{\ensuremath{^\circ}}
% \renewcommand{\rmdefault}{phv}
\renewcommand{\sfdefault}{phv}

\usepackage{sansmath}

\definecolor{uququq}{rgb}{0.25098039215686274,0.25098039215686274,0.25098039215686274}
\tikzset{
    quote/.style={{|[width=2mm]}-{|[width=2mm]}}
}

\begin{document}
\begin{tikzpicture}[> = stealth]
\coordinate (a) at (5,1);
\coordinate (b) at (1,3);




\draw[black,step=1cm] (0,0) grid +(5cm,3cm);

\draw[line width=0.3mm, black, -]  (b) -- node[above left] {\Huge \textbf{}} (a);


\end{tikzpicture}
\end{document}

代码3:

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{angles,quotes}
\usetikzlibrary{arrows,arrows.meta}
\pagestyle{empty}
\newcommand{\degre}{\ensuremath{^\circ}}
% \renewcommand{\rmdefault}{phv}
\renewcommand{\sfdefault}{phv}

\usepackage{sansmath}

\definecolor{uququq}{rgb}{0.25098039215686274,0.25098039215686274,0.25098039215686274}
\tikzset{
    quote/.style={{|[width=2mm]}-{|[width=2mm]}}
}

\begin{document}
\begin{tikzpicture}[> = stealth]




\draw[white,step=1cm] (0,0) grid +(6cm,8cm); %white grid

\draw [line width=0.8mm, color=black,fill=white,fill opacity=0.3] (3.0,1.0) circle (1.0cm);

\draw [line width=0.8mm,color=black,fill=blue,fill opacity=1.0] (1.0,3.0) circle (1.0cm);
\draw [line width=0.8mm, color=black,fill=white,fill opacity=0.3] (3.0,3.0) circle (1.0cm);
\draw [line width=0.8mm, color=black,fill=blue,fill opacity=1.0] (5.0,3.0) circle (1.0cm);

\draw [line width=0.8mm, color=black,fill=white,fill opacity=0.3] (1.0,5.0) circle (1.0cm);
\draw [line width=0.8mm, color=black,fill=green,fill opacity=1.0] (3.0,5.0) circle (1.0cm);
\draw [line width=0.8mm, color=black,fill=white,fill opacity=0.3] (5.0,5.0) circle (1.0cm);

\draw [line width=0.8mm, color=black,fill=white,fill opacity=0.3] (3.0,7.0) circle (1.0cm);


\end{tikzpicture}
\end{document}

代码4:

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{angles,quotes}
\usetikzlibrary{arrows,arrows.meta}
\pagestyle{empty}
\newcommand{\degre}{\ensuremath{^\circ}}
% \renewcommand{\rmdefault}{phv}
\renewcommand{\sfdefault}{phv}

\usepackage{sansmath}

\definecolor{uququq}{rgb}{0.25098039215686274,0.25098039215686274,0.25098039215686274}
\tikzset{
    quote/.style={{|[width=2mm]}-{|[width=2mm]}}
}

\begin{document}
\begin{tikzpicture}[> = stealth]


\coordinate (a) at (0,0);
\coordinate (b) at (2,0);
\coordinate (c) at (4,0);
\coordinate (d) at (6,0);
\coordinate (e) at (8,0);


\coordinate (f) at (1,2);
\coordinate (g) at (3,2);
\coordinate (h) at (5,2);
\coordinate (i) at (7,2);

\coordinate (l) at (2,4);
\coordinate (m) at (4,4);
\coordinate (n) at (6,4);

\coordinate (o) at (3,6);
\coordinate (p) at (5,6);

\coordinate (q) at (4,8);


\draw[white,step=1cm] (0,0) grid +(8cm,8cm);




\draw[line width=1.3mm, black, -]  (e) -- node[above left] {\Huge \textbf{}} (a);

\draw[line width=1.3mm, black, -]  (i) -- node[above left] {\Huge \textbf{}} (f);

\draw[line width=1.3mm, black, -]  (n) -- node[above left] {\Huge \textbf{}} (l);

\draw[line width=1.3mm, black, -]  (p) -- node[above left] {\Huge \textbf{}} (o);




\draw[line width=1.3mm, black, -]  (f) -- node[above left] {\Huge \textbf{}} (b);
\draw[line width=1.3mm, black, -]  (l) -- node[above left] {\Huge \textbf{}} (c);
\draw[line width=1.3mm, black, -]  (o) -- node[above left] {\Huge \textbf{}} (d);
\draw[line width=1.3mm, black, -]  (q) -- node[above left] {\Huge \textbf{}} (e);


\draw[line width=1.3mm, black, -]  (i) -- node[above left] {\Huge \textbf{}} (d);
\draw[line width=1.3mm, black, -]  (n) -- node[above left] {\Huge \textbf{}} (c);
\draw[line width=1.3mm, black, -]  (p) -- node[above left] {\Huge \textbf{}} (b);
\draw[line width=1.3mm, black, -]  (q) -- node[above left] {\Huge \textbf{}} (a);

\end{tikzpicture}
\end{document}

答案1

大多数情况下,只需使用\fill适当的坐标,并在绘制线条之前执行此操作,这样填充就不会覆盖线条。我整理了一些代码,但我确信有些事情可以做得更有效率。

不确定是否有很多需要解释的地方,应该很清楚如何选择坐标。可以提到-|/|-语法在TikZ:箭头的 |- 符号到底起什么作用?,这cycle意味着画一条线回到路径上的第一个点。

代码输出

\documentclass[margin=20pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (0,0);
\coordinate (b) at (3,3);
\coordinate (c) at (3,0);
\coordinate (d) at (0,3);
\coordinate (g) at (6,3);
\coordinate (h) at (3,6);
\coordinate (i) at (6,6);
\coordinate (e) at (5,5);
\coordinate (f) at (5,1);

\fill [blue!20] (0,6) rectangle +(2,-2);
\fill [blue!20] (a) -- (1.5,1.5) -- (c) -- cycle;
\fill [blue!20] (6,0) rectangle +(-1,1);
\fill [blue!20] (6,6) -- ++(-1,-1) -| cycle;
\fill [blue!20] (4,5) -- (5,5) -- (4.5,4.5) -- cycle;
\fill [blue!20] (4,4) -- (5,4) -- (4.5,4.5) -- cycle;
\fill [blue!20] (4,4) -- (3,3) -| cycle;
\fill [blue!20] (3,3) -- (2,2) |- cycle;


\draw[black,step=1cm] (0,0) grid +(6,6);
\draw[line width=0.3mm, red]  (b) -- (a);
\draw[line width=0.3mm, red]  (c) -- (d);
\draw[line width=0.3mm, green]  (g) -- (h);
\draw[line width=0.3mm, green]  (b) -- (i);
\end{tikzpicture}

\begin{tikzpicture}
\coordinate (a) at (5,1);
\coordinate (b) at (1,3);

\fill [green!40] (0,0) rectangle (5,3);
\draw[black,step=1cm] (0,0) grid +(5,3);
\filldraw [fill=yellow,draw=black] (b) -- (a) |- cycle;
\draw[line width=0.3mm, black, -]  (b) -- (a);

\end{tikzpicture}

\begin{tikzpicture}[
  Circle/.style={
    circle,
    minimum size=2cm,
    fill=#1,
    draw=black,
    thick,
    node contents={}
  },
  Circle/.default=white,
  node distance=0
]
\node [Circle,name=a];
\node [Circle=green!30,below=of a,name=b];
\node [Circle,left=of b];
\node [Circle,right=of b];
\node [Circle,below=of b,name=c];
\node [Circle=blue!30,left=of c];
\node [Circle=blue!30,right=of c];
\node [Circle,below=of c];

\end{tikzpicture}

\begin{tikzpicture}
\coordinate (a) at (0,0);
\coordinate (b) at (2,0);
\coordinate (c) at (4,0);
\coordinate (d) at (6,0);
\coordinate (e) at (8,0);
\coordinate (f) at (1,2);
\coordinate (g) at (3,2);
\coordinate (h) at (5,2);
\coordinate (i) at (7,2);
\coordinate (l) at (2,4);
\coordinate (m) at (4,4);
\coordinate (n) at (6,4);
\coordinate (o) at (3,6);
\coordinate (p) at (5,6);
\coordinate (q) at (4,8);

\fill [green!30] (q) -- (p) -- (o) -- cycle;
\fill [green!30] (l) -- (m) -- (h) -- (g) -- cycle;
\fill [blue!30]  (a) -- (b) -- (f) -- cycle;

\draw [line width=1.3mm] (e) -- (a) -- (q) -- cycle;
\foreach \i/\j in {i/f,n/l,p/o,f/b,l/c,o/d,i/d,n/c,p/b}
  \draw [line width=1.3mm] (\i) -- (\j);

\end{tikzpicture}
\end{document}

相关内容