Tikz 绘制弯曲线条

Tikz 绘制弯曲线条

我想要画出下面的图:
在此处输入图片描述
我已经做到了以下几点:
在此处输入图片描述 这是 MWE:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\filldraw[color=lightgray] (1,0) rectangle (7,4);
\filldraw[color=blue] (1.5,3) rectangle (2.5,0.5);
\filldraw[color=blue] (5.5,3) rectangle (6.5,0.5);
\filldraw[color=white] (3.5,1.2) rectangle (4.5,2.4);
\end{tikzpicture}
\end{document} 

我的问题是,如何通过中间的正方形从北极到南极画出这些红线?

答案1

为了便于比较,下面是绘制效果线的方法元帖子

prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);

u = 1cm;

% background and the central whitish box
fill unitsquare xscaled 8u yscaled 5u shifted (-4u,-5/2u) withcolor .8 white;
fill unitsquare scaled u shifted (-1/2u,-1/2u) withcolor .95 white;
draw unitsquare scaled u shifted (-1/2u,-1/2u);

% parameters
-z1 = z2 = (2.4u,0);
a := 4/5;
b := 2/5;
c := 4/5;

% draw the effect lines
for i = -c step c/5 until c+eps:
  s := if i<0: -sqrt(-i) else: sqrt(i) fi;
  draw (x1,i*u) .. (-a*u,i*u) {right} .. {right} (-b*u,s*u) 
    -- (+b*u,s*u) {right} .. {right} (+a*u,i*u) -- (x2,i*u) 
    withcolor .67 red;
endfor

% draw the N and S boxes
fill unitsquare xscaled u yscaled 2u shifted z1 shifted (-1/2u,-u) withcolor .3 blue + .7 white;
draw unitsquare xscaled u yscaled 2u shifted z1 shifted (-1/2u,-u);
fill unitsquare xscaled u yscaled 2u shifted z2 shifted (-1/2u,-u) withcolor .3 blue + .7 white;
draw unitsquare xscaled u yscaled 2u shifted z2 shifted (-1/2u,-u);

% add the labels
defaultfont := "phvr8r";
label("N", z1);
label("S", z2);
label(btex $\mu_r<1$, $\chi_m<0$, (weak effect) etex, (0,3/2u));

endfig;
end.

在此处输入图片描述

答案2

我认为如果使用命名节点和坐标,代码会更加灵活和易于维护,如下例所示:

\usetikzlibrary{calc}

\begin{tikzpicture}
% Background
\filldraw[color=lightgray] (1,0) rectangle (7,4);

% North pole
\node[draw, minimum width=1cm, minimum height=2.5cm, inner sep=0pt, fill=blue, anchor=south west] at (1.5, 0.5) (north pole) {\sffamily\bfseries N};

% South pole
\node[draw, minimum width=1cm, minimum height=2.5cm, inner sep=0pt, fill=blue, anchor=south west] at (5.5, 0.5) (south pole) {\sffamily\bfseries S};

% White block
\node[draw, minimum width=1cm , minimum height=1.2cm, inner sep=0pt, fill=white, anchor=center] at ($(north pole)!.5!(south pole)$) (block) {};

% Vertical points at which the field lines are distorted
% as percentage of the distance between poles and white block
\coordinate (outer left edge) at ($(north pole.east)!.8!(block.west)$);
\coordinate (inner left edge) at ($(block.west)!.2!(block.east)$);
\coordinate (inner right edge) at ($(block.west)!.8!(block.east)$);
\coordinate (outer right edge) at ($(block.east)!.2!(south pole.west)$);

% To draw the field lines, I use a for loop. The numbers in the list
% is the vertical distance each line is deflected
\foreach \p [count=\i] in {0.1,0.2,0.3,0.4,0.3,0,-0.4,-0.4,-0.3,-0.2,-0.1} {
  % Vertical coordenate of the undeflected line
  \coordinate (aux1) at ($(north pole.north east)!\i*1/12!(north pole.south east)$);
  % Vertical coordinate of the deflected line
  \coordinate (aux2) at ($(aux1)+(0,\p)$);

  \draw[red, rounded corners=2pt] (aux1) -- (aux1-|outer left edge) --
       (aux2-|inner left edge) -- (aux2-|inner right edge) -- 
       (aux1-|outer right edge) -- (aux1-|south pole.west);
}
\end{tikzpicture}

其结果为:

结果

这种方法的优点是可以简单地更改尺寸(例如杆的尺寸),并自动调整图形。例如,将杆的高度更改为 2 厘米:

% North pole
\node[draw, minimum width=1cm, minimum height=2cm, inner sep=0pt, fill=blue, anchor=south west] at (1.5, 0.5) (north pole) {\sffamily\bfseries N};

% South pole
\node[draw, minimum width=1cm, minimum height=2cm, inner sep=0pt, fill=blue, anchor=south west] at (5.5, 0.5) (south pole) {\sffamily\bfseries S};

新的数字是:

新结果

答案3

如果您只想伪造数字,请使用曲线库。

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\filldraw[color=lightgray] (1,0) rectangle (7,4);
\filldraw[color=blue] (1.5,3) rectangle (2.5,0.5);
\filldraw[color=blue] (5.5,3) rectangle (6.5,0.5);
\filldraw[color=white] (3.5,1.2) rectangle (4.5,2.4);
\draw[color=red]  (2.5,3) -- (3.2,3) to[in=180,out=0] (3.5,3.1) -- (4.5,3.1) to[in=180,out=0] (4.8,3) -- (5.5,3);% repeat
\end{tikzpicture}
\end{document} 

第一行

相关内容