How to draw this graph by TikZ?
- 206,688
3 Answers
The points for the Voronoi diagram are the vertices of the dashed lines. The outer points are named A to I the two thick points are named K and L.
The positions are extracted from the image by putting the cropped image in the background and putting a grid on it. The final document should not have the grid, thus it can be enabled by \gridtrue for the development phase.
Each dashed triangle is drawn separately. The coordinates for the circumcenter is calculated using a formula found in Wikipedia and the circumcenter is named with the tree points of the triangle.
Since the triangles are touching each other, the drawn dashed lines are remembered in a macro to avoid drawing the same line twice.
The middle points of the dashed lines are named with the two end points of the line. Then the solid lines are drawn inside the triangle.
The outer exceed lines are also drawn, by defining, which lines they are crossing beforehand.
The position of the squares also needs some calculation, because they are set at the crossing of dashed with solid lines. TikZ library intersections helps here.
Full example:
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}
\usepackage{graphicx}
\newif\ifgrid
%\gridtrue
\makeatletter
\newcommand*{\Triangle}[3]{%
\DashLine{#1}{#2}%
\DashLine{#2}{#3}%
\DashLine{#3}{#1}%
\coordinate (#1#2) at ($(#1)!.5!(#2)$);
\coordinate (#2#3) at ($(#2)!.5!(#3)$);
\coordinate (#3#1) at ($(#3)!.5!(#1)$);
% https://en.wikipedia.org/wiki/Circumscribed_circle#Cartesian_coordinates_2
\draw[red, thick, fixed point arithmetic] let
\p{A}=(#1),
\p{B}=(#2),
\p{C}=(#3),
\n{D}={%
2 * (
\x{A}*(\y{B}-\y{C}) + \x{B}*(\y{C}-\y{A}) + \x{C}*(\y{A}-\y{B})
)},
\n{x}={%
(
(\x{A}*\x{A} + \y{A}*\y{A}) * (\y{B}-\y{C}) +
(\x{B}*\x{B} + \y{B}*\y{B}) * (\y{C}-\y{A}) +
(\x{C}*\x{C} + \y{C}*\y{C}) * (\y{A}-\y{B})
) / \n{D}},
\n{y}={%
(
(\x{A}*\x{A} + \y{A}*\y{A}) * (\x{C}-\x{B}) +
(\x{B}*\x{B} + \y{B}*\y{B}) * (\x{A}-\x{C}) +
(\x{C}*\x{C} + \y{C}*\y{C}) * (\x{B}-\x{A})
) / \n{D}}
in
(\n{x}, \n{y}) coordinate (#1#2#3)
(#1#2#3)
\@ifundefined{L@#1#2}{%
-- (#1#2)%
}{%
-- ($(#1#2)!-15pt!(#1#2#3)$)%
}
(#1#2#3) -- (#2#3)
(#1#2#3) -- (#3#1)
;%
}
\newcommand*{\DashLine}[2]{%
\@ifundefined{dd@#1#2}{%
\draw[densely dashed, blue] (#1) -- (#2);%
\global\expandafter\let\csname dd@#1#2\endcsname=1%
\global\expandafter\let\csname dd@#2#1\endcsname=1%
}{}%
}
\newdimen\dimSq
\setlength{\dimSq}{3pt}
\newcommand*{\Square}[3]{%
\draw
($(#1)!#2!(#3)$) coordinate (sq1)
($(sq1)!\dimSq!0:(#3)$) coordinate (sq2)
($(sq2)!\dimSq!-90:(#3)$) coordinate (sq3)
($(sq1)!\dimSq!-90:(#3)$) coordinate (sq4)
(sq1) -- (sq2) -- (sq3) -- (sq4) -- cycle
;%
}
\newcommand*{\SquareM}[4]{%
\path[name path=lineA] (#1) -- (#2);
\path[name path=lineB] (#3) -- (#4);
\draw[
name intersections={of=lineA and lineB, name=cross},
]
($(cross-1)!\dimSq!0:(#2)$) coordinate (sq2)
($(sq2)!\dimSq!90:(#2)$) coordinate (sq3)
($(cross-1)!\dimSq!90:(#2)$) coordinate (sq4)
(cross-1) -- (sq2) -- (sq3) -- (sq4) -- cycle
;
}
\makeatother
\begin{document}
\setlength{\unitlength}{.75mm}
\begin{tikzpicture}[x=\unitlength, y=\unitlength]
\ifgrid
\node[above right, inner sep=0pt]
(img) {\includegraphics[width=100\unitlength]{D27KN-crop.png}};
\draw[ultra thin, gray, step=1\unitlength]
(img.south west) grid (img.north east);
\draw[thin, red, step=10\unitlength]
(img.south west) grid (img.north east);
\fi
\coordinate (A) at (94, 62);
\coordinate (B) at (73, 86);
\coordinate (C) at (38, 82);
\coordinate (D) at (15, 61);
\coordinate (E) at ( 7, 34);
\coordinate (F) at (22, 10);
\coordinate (G) at (48, 1);
\coordinate (H) at (76, 8);
\coordinate (I) at (86, 27);
\coordinate (K) at (62, 53);
\coordinate (L) at (46, 27);
\fill[radius=1pt] \foreach \P in {A, ..., I} {(\P) circle[]};
\fill[radius=2pt] (K) circle[] (L) circle[];
\foreach \L in {AB, BC, CD, DE, EF, FG, GH, HI, IA}
{\global\expandafter\let\csname L@\L\endcsname=1 }
\foreach \T in {ABK, BCK, CDK, DEL, EFL, FGL, GHL, HIL, IAK, LKD, LIK}
{\expandafter\Triangle\T}
\path
(K) -- node[pos=.75] {$K$} (ABK)
(K) -- node[pos=.35] {$\mathbf{x}_K$} (IAK)
(LK) -- node[pos=.55] {$S$} (I)
(L) -- node[pos=.3] {$\mathbf{x}_L$} (GH)
(L) -- node[pos=.4] {$L$} (EF)
(LKD) -- node[pos=.4, above] {$e$} (LIK)
;
\draw[-{LaTeX}]
($(LKD)!.65!(LIK)$) coordinate (tmp)
(tmp) -- ($(tmp)!15pt!90:(LKD)$)
node[below right, yshift=1ex] {$\mathbf{n}_{K,e}$}
;
\Square{LKD}{.65}{LIK}
\SquareM{LIK}{IAK}IK
\SquareM{IAK}{ABK}AK
\SquareM{ABK}{BCK}BK
\SquareM{BCK}{CDK}CK
\SquareM{CDK}{LKD}DK
\path[lightgray, node font=\scriptsize]
\foreach \P/\N in {
A/right,
B/above,
C/above left,
D/left,
E/left,
F/below left,
G/below,
H/below right,
I/right%
} { (\P) node[\N] {\P} }
;
\end{tikzpicture}
\end{document}
- 271,626
Run with xelatex or latex->dvips->ps2pdf
\documentclass{scrartcl}
\usepackage{pst-eucl,pst-math}
\begin{document}
\psset{unit=1mm}
\begin{pspicture}(100,100)
\pstGeonode[PosAngle={10,45,90,110,170,200,-90,-45,0,-90,90}]%
(94, 62){A}(73, 86){B}(38, 82){C}(15, 61){D}( 7, 34){E}%
(22, 10){F}(48, 1){G}(76, 8){H}(86, 27){I}(62, 53){K}(46, 27){L}
\end{pspicture}
\psset{linestyle=dashed,linecolor=blue}
\pspolygon(A)(B)(C)(D)(E)(F)(G)(H)(I)
\psline(B)(K)(L)(G)\psline(H)(L)(E)\psline(I)(L)(D)\psline(I)(K)(D)
\psline(C)(K)(A)\psline(L)(F)
\psset{PointSymbol=none,PointName=none,linestyle=none}
\pstCircleABC{A}{B}{K}{M0} \pstCircleABC{B}{C}{K}{M1}% Mx intersection point
\pstCircleABC{C}{D}{K}{M2} \pstCircleABC{D}{L}{K}{M3}
\pstCircleABC{D}{E}{L}{M4} \pstCircleABC{E}{F}{L}{M5}
\pstCircleABC{F}{G}{L}{M6} \pstCircleABC{G}{H}{L}{M7}
\pstCircleABC{H}{I}{L}{M8} \pstCircleABC{I}{K}{L}{M9}
\pstCircleABC{I}{A}{K}{M10}
\psset{linestyle=solid,linecolor=red,linewidth=1pt}
\pspolygon(M9)(M10)(M0)(M1)(M2)(M3)
\psline(M3)(M4)(M5)(M6)(M7)(M8)(M9)
\pstMiddleAB{A}{B}{AB}\pstMiddleAB{B}{C}{BC}
\pstMiddleAB{C}{D}{CD}\pstMiddleAB{D}{E}{DE}
\pstMiddleAB{E}{F}{EF}\pstMiddleAB{F}{G}{FG}
\pstMiddleAB{G}{H}{GH}\pstMiddleAB{H}{I}{HI}
\pstMiddleAB{I}{A}{IA}
\psset{nodesepA=-10}
\pcline(AB)(M0)\pcline(BC)(M1)\pcline(CD)(M2)
\pcline(DE)(M4)\pcline(EF)(M5)\pcline(FG)(M6)
\pcline(GH)(M7)\pcline(HI)(M8)\pcline(IA)(M10)
\uput[-80](L){$x_L$}\uput[190](K){$x_K$}
\end{document}
Use \usetikzlibrary{intersections,calc}
%\usetikzlibrary{intersections,calc}
\begin{tikzpicture}
\coordinate[label=left:$X_k$](O) at (0,0);
\coordinate[label=$A2$](A2) at ($(O)+(10:3)$);
\coordinate[label=$A3$](A3) at ($(O)+(45:3)$);
\coordinate[label=$A4$](A4) at ($(O)+(100:3)$);
\coordinate(C5) at ($(O)+(160:4)$);
\coordinate[label=left:$X_L$](K) at ($(O)+(220:3)$);
\coordinate(C1) at ($(O)+(-60:4)$);
\coordinate(B1) at ($(K)+(110:4)$);
\coordinate[label=$B2$](B2) at ($(K)+(170:3)$);
\coordinate[label=$B3$](B3) at ($(K)+(220:3)$);
\coordinate[label=$B4$](B4) at ($(K)+(-90:3)$);
\coordinate[label=$B5$](B5) at ($(K)+(-45:3.5)$);
\coordinate(B7) at ($(K)+(-10:2)$);
\fill(O)circle (1mm);
\fill(K)circle (1mm);
\coordinate[label=$A5$] (A5) at (intersection cs:first line={(O)--(C5)}, second line={(K)--(B1)});
\coordinate[label=$A5$] (B1) at (intersection cs:first line={(O)--(C5)}, second line={(K)--(B1)});
\coordinate[label=$A1$] (A1) at (intersection cs:first line={(O)--(C1)}, second line={(K)--(B7)});
\coordinate(B6) at (intersection cs:first line={(O)--(C1)}, second line={(K)--(B7)});
\begin{scope}[dashed,blue]
\draw (A1)--(A2)--(A3)--(A4)--(A5)--(B2)--(B3)--(B4)--(B5)--cycle;
\foreach \x in {1,2,3,4,5}
{
\draw (O)--(A\x);
}
\foreach \x in {1,2,3,4,5,6}
{
\draw (K)--(B\x);
}
\draw (O)--(K);
\end{scope}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\foreach \i in {1,2,3,4,5}
{
\coordinate(M\i) at ($(O)!0.5!(A\i)$);
}
\foreach \i in {1,2,3,4,5,6}
{
\coordinate(X\i) at ($(K)!0.5!(B\i)$);
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\foreach \i in {1,2,3,4,5}
{
\coordinate(L\i) at ($(M\i)!2cm!90:(A\i)$);
}
\coordinate (T1) at (intersection cs:first line={(M1)--(L1)}, second line={(M2)--(L2)});
\coordinate (T2) at (intersection cs:first line={(M2)--(L2)}, second line={(M3)--(L3)});
\coordinate (T3) at (intersection cs:first line={(M3)--(L3)}, second line={(M4)--(L4)});
\coordinate (T4) at (intersection cs:first line={(M4)--(L4)}, second line={(M5)--(L5)});
\foreach \i in {1,2,3,4,5,6}
{
\coordinate(H\i) at ($(X\i)!2cm!90:(B\i)$);
}
\coordinate (T5) at (intersection cs:first line={(M5)--(L5)}, second line={(X1)--(H1)});
\coordinate (T6) at (intersection cs:first line={(X1)--(H1)}, second line={(X2)--(H2)});
\coordinate (T7) at (intersection cs:first line={(X2)--(H2)}, second line={(X3)--(H3)});
\coordinate (T8) at (intersection cs:first line={(X3)--(H3)}, second line={(X4)--(H4)});
\coordinate (T9) at (intersection cs:first line={(X4)--(H4)}, second line={(X5)--(H5)});
\coordinate (T10)at (intersection cs:first line={(X5)--(H5)}, second line={(X6)--(H6)});
\coordinate (T11)at (intersection cs:first line={(M1)--(L1)}, second line={(X6)--(H6)});
\begin{scope}[red,very thick]
\draw(T1)--(T2)--(T3)--(T4)--(T5)--(T6)--(T7)--(T8)--(T9)--(T10)--(T11)--cycle;
\draw(T5)--(T11);
\coordinate (E1) at ($(A1)!0.5!(A2)$);
\coordinate (E2) at ($(A2)!0.5!(A3)$);
\coordinate (E3) at ($(A3)!0.5!(A4)$);
\coordinate (E4) at ($(A4)!0.5!(A5)$);
\coordinate (E5) at ($(A5)!0.5!(B2)$);
\coordinate (E6) at ($(B2)!0.5!(B3)$);
\coordinate (E7) at ($(B3)!0.5!(B4)$);
\coordinate (E8) at ($(B4)!0.5!(B5)$);
\coordinate (E9) at ($(B5)!0.5!(B6)$);
\draw(T1)--($(T1)!2!(E1)$);
\draw(T2)--($(T2)!2!(E2)$);
\draw(T3)--($(T3)!2!(E3)$);
\draw(T4)--($(T4)!2!(E4)$);
\draw(T6)--($(T6)!2!(E5)$);
\draw(T7)--($(T7)!2!(E6)$);
\draw(T8)--($(T8)!2!(E7)$);
\draw(T9)--($(T9)!2!(E8)$);
\draw(T10)--($(T10)!2!(E9)$);
\end{scope}
\node at (0.5,1) {$K$};
\node at (-3,-1) {$L$};
\coordinate(S) at ($(T11)!.40!(T5)$);
\coordinate(S1) at ($(S)!1cm!90:(T5)$);
\draw[->,thick](S)--node[below,sloped]{$n_{k,e}$}(S1);
\end{tikzpicture}
- 595
-
2If you recommend to use
\usetikzlibrary{intersections,calc}it shouldn't be commented out ;-) Please provide a compilable code, not just a fragment – May 27 '16 at 18:14




\includegraphics{}from thegraphicxpackage. – cfr May 25 '16 at 21:56