使用 TikZ 进行简单绘图

使用 TikZ 进行简单绘图

首先我要说的是,我完全是个新手。我一直想画下面的图,但一直没成功:

通缉

以下是我目前所掌握的信息: 在此处输入图片描述

\documentclass[border=5mm,tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\definecolor{DarkOceanSky}{RGB}{133,163,229}
\definecolor{Reu}{RGB}{102,173,76}
\definecolor{Crimson}{RGB}{220, 20, 60}

\tikzset{% define a pic for the little squares: takes colour and label as arguments
   pics/mysquare/.style args={#1, #2}{
     code={\draw[#1](-0.2,-0.2) rectangle (0.2,0.2);
           \draw[fill=#1] circle[#1,radius=2pt](0,0);
           \node[#1] at (0.4,-0.08){$#2$};
}}}

\begin{document}
    \begin{tikzpicture}

        \begin{scope}[my square/.style={rectangle,draw=red,minimum width=1em, minimum height=1em}]
                \draw[fill=Reu] (0,1)--(1.25,1.6)--(2.95,3)--(2.25,4)--(0,4)--(0,1);
                \draw[fill=DarkOceanBlue](1.25,1.6)--(2.95,3)--(2.25,4)--(4,4)--(4,1.9)--(2.75,2)--cycle;
                \draw[very thick, Crimson,rounded corners] (0,1)--(1.25,1.6)--(2.75,2)--(4, 1.9);
                \draw[thick] (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle;
                \node[right] at (1.7, 0.8) (e2) {\fontsize{12}{5} \selectfont $\Omega$};
                \node[right, rotate=22] at (0.2, 0.95) (e2) {\fontsize{8}{5} \selectfont $\delta \Omega$};
                \node[right] at (0.8, 3.3) (e2) {\fontsize{12}{5} \selectfont $\Phi$};
        \end{scope}

        \begin{scope}[my square/.style={rectangle,draw=red,minimum width=1em, minimum height=1em}, xshift=116]
                \draw[fill=Reu] (0,1)--(1.25,1.6)--(2.95,3)--(2.25,4)--(0,4)--(0,1);
                \draw[fill=DarkOceanBlue](1.25,1.6)--(2.95,3)--(2.25,4)--(4,4)--(4,1.9)--(2.75,2)--cycle;
                \draw[very thick, Crimson,rounded corners] (0,1)--(1.25,1.6)--(2.75,2)--(4, 1.9);
                \draw[thick] (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle;
                \draw (1.25,1.6) pic{mysquare={Crimson, p}};
                \node[right] at (1.7, 0.8) (e2) {\fontsize{12}{5} \selectfont $\Omega$};
                \node[right, rotate=22] at (0.2, 0.95) (e2) {\fontsize{8}{5} \selectfont $\delta \Omega$};
                \node[right] at (0.8, 3.3) (e2) {\fontsize{12}{5} \selectfont $\Phi$};
        \end{scope}

        \begin{scope}[my square/.style={rectangle,draw=red,minimum width=1em, minimum height=1em}, xshift=232]
                \draw[fill=Reu] (0,1)--(1.25,1.6)--(2.95,3)--(2.25,4)--(0,4)--(0,1);
                \draw[fill=DarkOceanBlue](1.25,1.6)--(2.95,3)--(2.25,4)--(4,4)--(4,1.9)--(2.75,2)--cycle;
                \draw[very thick, Crimson,rounded corners] (0,1)--(1.25,1.6)--(2.75,2)--(4, 1.9);
                \draw[thick] (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle;
                \draw (1.25,1.6) pic{mysquare={Crimson, p}};
                \draw (2.735,3.3) pic{mysquare={yellow, q''}};
                \draw ($ (1.25,1.6)!0.5!(2.95,3) $) pic{mysquare={yellow, q'}};
                \node[right] at (1.7, 0.8) (e2) {\fontsize{12}{5} \selectfont $\Omega$};
                \node[right, rotate=22] at (0.2, 0.95) (e2) {\fontsize{8}{5} \selectfont $\delta \Omega$};
                \node[right] at (0.8, 3.3) (e2) {\fontsize{12}{5} \selectfont $\Phi$};
        \end{scope}

        \node[right] at (1.7, -0.3) (e2) {\fontsize{12}{5} \selectfont  a};
        \node[right] at (5.75, -0.3) (e2) {\fontsize{12}{5} \selectfont  b};
        \node[right] at (9.8, -0.3) (e2) {\fontsize{12}{5} \selectfont  c};
    \end{tikzpicture}
\end{document}

你能告诉我怎么画吗最后的正方形(红线部分被修改的地方)!

答案1

这是实现此目的的一种方法。有趣的部分是使用pic通过定义一个名为的新“图片”来绘制小方块mysquare。这些方块有两个参数:颜色和标签。

这是我的代码生成的图像。

在此处输入图片描述

以下是代码:

\documentclass[border=5mm,tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc,decorations}

\tikzset{% define a pic for the little squares: takes colour and label as arguments
   pics/mysquare/.style args={#1,#2}{
     code={\draw[thick,#1](-0.2,-0.2) rectangle (0.2,0.2);
           \draw[fill=#1] circle[#1,radius=2.5pt](0,0);
           \node[black] at (0.4,0){$#2$};
}}}

\begin{document}
\begin{tikzpicture}[my square/.style={rectangle,draw=red,minimum width=1em, minimum height=1em}]
    \draw[fill=green!80!blue] (0,1)--(1.25,1.6)--(2.95,3)--(2.25,4)--(0,4)--(0,1);
    \draw[blue, fill=blue](1.25,1.6)--(2.95,3)--(2.25,4)--(4,4)--(4,1.9)--(2.75,2)--cycle;
    \draw[very thick, red,rounded corners] (0,1)--node[below=-1mm,black,rotate=30]{$\delta\Omega$}(1.25,1.6)--(2.75,2)--(4, 1.9);
    %\draw[thick, fill=green] (0,1) -- (4,0) -- (4,4) -- (0,4) -- cycle;
    \draw[very thick] (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle;
    \draw (1.25,1.6) pic{mysquare={yellow, p}};
    \draw ($ (1.25,1.6)!0.6!(2.95,3) $) pic{mysquare={red, q'}};
    \draw ($ (2.25,4)!0.5!(2.95,3) $) pic{mysquare={red, q''}};
    \node at (3,1){$\Omega$};
    \node at (2,3){$\Phi$};
\end{tikzpicture}
\end{document}

除了mysquare图片之外,代码中另一行有趣的内容是坐标:

 ($ (1.25,1.6)!0.6!(2.95,3) $)

这是通过 tikzcalc库完成的,它计算出连接两点(1.25,1.6)和的线段 60% 处的点(2.95,3)。类似地,(1.25,1.6)!0.2!2.95,3)将选择该线段五分之一处的点。告诉($ ... $)tikz 进入“计算模式”。

编辑根据要求,这是您最后一张图的代码。我删除了圆边,因为这样更容易。还请注意,第二条\draw命令应该是

\draw[blue, fill=blue]...

特别是,它将blue消除图表中分隔绿色和蓝色区域的黑线。

在此处输入图片描述

\documentclass[border=5mm,tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc,decorations}

\tikzset{% define a pic for the little squares: takes colour and label as arguments
   pics/mysquare/.style args={#1, #2}{
     code={\draw[thick,#1](-0.2,-0.2) rectangle (0.2,0.2);
           \draw[fill=#1] circle[#1,radius=2.5pt](0,0);
           \node[black] at (0.4,0){$#2$};
}}}

\begin{document}
\begin{tikzpicture}[my square/.style={rectangle,draw=red,minimum width=1em, minimum height=1em}]
    \draw[fill=green!80!blue] (0,1)--(1.25,1.6)--++(0,-0.2)--(2.95,3)--(2.25,4)--(0,4)--(0,1);
    \draw[blue, fill=blue](1.25,1.4)--++(0.3,0)--++(0,0.2)--(2.75,2)--(4,1.9)--(4,4)--(2.25,4)--(2.95,3)--cycle;
    \draw[very thick, red] (0,1)--node[below=-0.5mm,black,rotate=25]{$\delta\Omega$}(1.25,1.6)--++(0,-0.2)--++(0.3,0)--++(0,0.2)--(2.75,2)--(4, 1.9);
    %\draw[thick, fill=green] (0,1) -- (4,0) -- (4,4) -- (0,4) -- cycle;
    \draw[very thick] (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle;
    %\draw (1.25,1.6) pic{mysquare={yellow, p}};
    \draw ($ (1.25,1.6)!0.6!(2.95,3) $) pic{mysquare={red, q'}};
    \draw ($ (2.25,4)!0.5!(2.95,3) $) pic{mysquare={red, q''}};
    \node at (3,1){$\Omega$};
    \node at (2,3){$\Phi$};
\end{tikzpicture}
\end{document}

相关内容