如何在 LaTeX 中绘制不规则封闭形状

如何在 LaTeX 中绘制不规则封闭形状

我在 LaTeX 中编写了以下代码,该代码可以在正方形中生成一个带有箭头线的圆圈,但我想用不规则形状替换圆圈,就像图片中的分子一样。我该怎么做?

\begin{figure}[H]
\centering
\begin{tikzpicture}[thick]
  %x-axes
  \draw [red](4,2) --(7,2);
  %\draw (4,3) --(7,3);
  %\draw (4,4) --(7,4);
  \draw [red,very thick](4,5) --(7,5);
  %y-axes
  \draw [red,very thick](7,2) --(7,5);
  %\draw (6,2) --(6,5);
  %\draw (5,2) --(5,5);
  \draw [red,very thick](4,2) --(4,5);
  %centre
  \fill[blue,very thick] (5.5,3.5) circle(.1);
  %arrows on the right
  \draw [-latex][dashed] (5.5,3.5) --(8,5);
  \draw [-latex][dashed] (5.5,3.5) --(8,4);
  \draw [-latex][dashed] (5.5,3.5) --(8,3);
  \draw [-latex][dashed] (5.5,3.5) --(8,2);
   %arrows on the left
  \draw [-latex][dashed] (5.5,3.5) --(3,2);
  \draw [-latex][dashed] (5.5,3.5) --(3,3);
  \draw [-latex][dashed] (5.5,3.5) --(3,4);
  \draw [-latex][dashed] (5.5,3.5) --(3,5);
  %arrows at the bottom
  \draw [-latex][dashed] (5.5,3.5) --(4,1);
  \draw [-latex][dashed] (5.5,3.5) --(5,1);
  \draw [-latex][dashed] (5.5,3.5) --(6,1);
  \draw [-latex][dashed] (5.5,3.5) --(7,1);
  %arrows at the top
  \draw [-latex][dashed] (5.5,3.5) --(4,6);
  \draw [-latex][dashed] (5.5,3.5) --(5,6);
  \draw [-latex][dashed] (5.5,3.5) --(6,6);
  \draw [-latex][dashed] (5.5,3.5) --(7,6);
  %draw a circle
  \node(circle) [circle, inner sep=0.7cm, draw=red!120] at (5.5,3.5) {};
\end{tikzpicture}
\end{figure}

在此处输入图片描述

答案1

你可以使用爱好包裹:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{hobby}

\begin{document}
\begin{tikzpicture}[scale=2]
  \draw [red,very thick](4,2) --(7,2);
  \draw [red,very thick](4,5) --(7,5);
  \draw [red,very thick](7,2) --(7,5);
  \draw [red,very thick](4,2) --(4,5);

  \fill[blue,very thick] (5.5,3.5) circle(.1);
  %arrows on the right
  \draw [-latex][dashed] (5.5,3.5) --(8,5);
  \draw [-latex][dashed] (5.5,3.5) --(8,4);
  \draw [-latex][dashed] (5.5,3.5) --(8,3);
  \draw [-latex][dashed] (5.5,3.5) --(8,2);
   %arrows on the left
  \draw [-latex][dashed] (5.5,3.5) --(3,2);
  \draw [-latex][dashed] (5.5,3.5) --(3,3);
  \draw [-latex][dashed] (5.5,3.5) --(3,4);
  \draw [-latex][dashed] (5.5,3.5) --(3,5);
  %arrows at the bottom
  \draw [-latex][dashed] (5.5,3.5) --(4,1);
  \draw [-latex][dashed] (5.5,3.5) --(5,1);
  \draw [-latex][dashed] (5.5,3.5) --(6,1);
  \draw [-latex][dashed] (5.5,3.5) --(7,1);
  %arrows at the top
  \draw [-latex][dashed] (5.5,3.5) --(4,6);
  \draw [-latex][dashed] (5.5,3.5) --(5,6);
  \draw [-latex][dashed] (5.5,3.5) --(6,6);
  \draw [-latex][dashed] (5.5,3.5) --(7,6);
  %draw a circle
  \node(circle) [circle, inner sep=0.7cm, draw=red!120] at (5.5,3.5) {};

  \coordinate[label=below left:{$A$}] (A) at (4+0.8,2+0.8);
  \coordinate[label=left:{$B$}]       (B) at (4+0.4,2+1.5);
  \coordinate[label=above:{$C$}]      (C) at (4+0.5,2+2.5);
  \coordinate[label=above:{$D$}]      (D) at (4+1.0,2+2.0);
  \coordinate[label=below:{$E$}]      (E) at (4+1.8,2+2.7);
  \coordinate[label=below left:{$F$}] (F) at (4+2.4,2+2.4);
  \coordinate[label=left:{$G$}]       (G) at (4+2.7,2+1.8);
  \coordinate[label=right:{$H$}]      (H) at (4+2.0,2+1.0);
  \coordinate[label=right:{$I$}]      (I) at (4+2.5,2+0.5);
  \coordinate[label=below left:{$J$}] (J) at (4+1.5,2+0.4);

  \draw[blue,very thick] (A) to [closed, curve through = {(B) (C) (D) (E) (F) (G) (H) (I) (J)}] (A);

  \foreach \j in {A,...,J}{\fill[blue] (\j) circle (0.04);}

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

由于您的 MWE 有一个奇怪的草图,与您想要的结果不太相关,所以我有点困惑,抱歉。无论如何,我将介绍:

  1. 重新绘制原始代码,作为某些命令如何工作的基础。
  2. 以彩色的方式绘制您想要的结果。
  3. 另一种方法是绘制平滑曲线,您可以选择使用哪一种方法。

我使用颜色是为了更容易识别谁做了什么,但并不完全符合您所展示的。您可以随意混合和编辑代码。

韓國

请注意,使用某些命令时,某些代码会简单得多。我邀请您检查一下TikZ 的简单介绍

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[thick]
  \draw [red,very thick] (4,2) rectangle ++(4,4);
  \fill[blue,very thick] (6,4) coordinate (A) circle (0.2); 
  \foreach \t in {0,20, ..., 360}{
    \draw [-latex, dashed]  (A) -- ++(\t:3.5);
  };
  \node(circle) [circle, inner sep=0.7cm, draw=red!120] at (A) {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

韓國

主 MWE。用 绘制了封闭的平滑部分... plot[smooth cycle, tension=.7] coordinates {...};。请注意如何scope将代码压缩到应用every node,它使代码更加干净和清晰易读。

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[thick]
  \draw [magenta,very thick] (0,0) node[left]{Solvent} rectangle ++(6.8,5.5);
  
  \draw[green!50!black]  plot[smooth cycle, tension=.7]
  coordinates {(0.8,0.6) (3.3,1.2) (3.3,2.5) (4.4,2.9) (5.5,3.5) (4.8,5.1) (1.3,3.6)};
  
  \node at (7.2,0.1) {$\partial \Omega$};
  \node[green!50!black] at (5.6,5.1) {$\Gamma$};
  \node[green!50!black] at (2.9,5) {$\Omega^{+}$};
  \node[green!50!black] at (2.9,4.1) {$\Omega^{-}$};
  \node[green!50!black] at (2.9,3.1) {Molecule};
  \node[orange] (B) at (5.5,1) {Mobile Ions};
  
  % Base syntax
  % \node[circle, red, inner sep=0.1cm, draw] (circle) at (4.1,-1.3) {$+$};
  % \node[circle, blue, inner sep=0.1cm, draw] (circle) at (2.4,-2.9) {$-$};
  
  \begin{scope}[every node/.style={circle, red, inner sep=0.1cm, draw}]
    \node at (3.7,3.8) {$+$};
    \node at (2,2.2) {$+$};
    \node at (1.7,3.1) {$+$};
  \end{scope}
  
  \begin{scope}[every node/.style={circle, blue, inner sep=0.1cm, draw}]
    \node at (4.6,4.4) {$-$};
    \node at (1.3,1.4) {$-$};
    \node at (1.2,2.3) {$-$};
  \end{scope}
  
  % Base syntax
  % \node[red] (circle) at (6.6,-0.8) {$+$};
  % \node[blue] (circle) at (2,-0.3) {$-$};
  
  \begin{scope}[every node/.style={cyan}]
    \node (P) at (4.7,2.2) {$+$};
    \node at (0.2,1.8) {$+$};
    \node at (0.6,3.8) {$+$};
  \end{scope}
  
  \begin{scope}[every node/.style={teal}]
    \node (N) at (6.1,4.1) {$-$};
    \node at (1.6,4.6) {$-$};
    \node at (0.6,4.6) {$-$};
  \end{scope}
  
  \draw[-latex] (B) -- (N);
  \draw[-latex] (B) -- (P);
\end{tikzpicture}
\end{document}

在此处输入图片描述

韓國

的替代方法plot[smooth]是使用,其中Bezier curves,和是给定坐标。(A) .. controls (B) and (C) ..(A)(B)(C)

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[purple] (9.8,2.5) .. controls (10.8,2) and (12.3,2) .. (11.3,3) .. controls (10.8,3.5) and (12.3,3) ..
  (12.8,4) .. controls (13.3,4.5) and (10,5.4) ..
  (8.8,4.5) .. controls (7.3,3.5) .. cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容