我想这应该很简单,但我尝试过的方法都行不通。我只想在框架的边缘得到一些三角形。
像这样:
我的代码如下:
\documentclass[tikz]{standalone}
\usetikzlibrary{calc,decorations.markings}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=1,inner sep=0pt,thick,dot/.style={draw,circle,minimum size=4pt},
put coord sys/.style={
decoration={markings,
mark= at position 0.5
with
{
\draw[-latex,ultra thin,blue!80!black] (2pt,2pt) -- ++(5mm,0) node[above right,rotate=\pgfdecoratedangle] {$\scriptstyle x$};
\draw[-latex,ultra thin,green!80!black] (2pt,2pt) -- ++(0,5mm) node[above right,rotate=\pgfdecoratedangle] {$\scriptstyle y$};
}
},
postaction=decorate}]
%\draw [help lines] (0,0) grid (3,4);
\node[dot] (2) at (0,0) [label={[label distance=1mm]270:2}] {};
\node[dot] (3) at (0,2) [label={[label distance=1mm]180:3}] {};
\node[dot] (1) at (0,4) [label={[label distance=1mm]90:1}] {};
\node[dot] (4) at (3,2) [label={[label distance=1mm]0:4}] {};
\node[dot] (5) at (2.5,4) [label={[label distance=1mm]45:5}] {};
%\draw[gray] ++(150:0.3) -- (0,0);
%\draw[gray] ++(-150:0.3) -- (0,0);
%\draw[gray] ++(180:0.3) -- (-.3,0);
%\draw[gray] ++(0,4) -- (150:1);
%\draw[gray] ++(-150:0.3) -- (0,4);
%\draw[gray] ++(180:0.3) -- (-.3,4);
%\draw (2) -- (3) node [midway,sloped,above] {3};
%\draw (3) -- (4) node [midway,above,sloped] {4};
%\draw (2) -- (4) node [midway,above,sloped] {6};
%\draw (3) -- (1) node [midway,above,sloped] {2};
%\draw (1) -- (5) node [midway,above,sloped] {1};
%\draw (3) -- (5) node [midway,above,sloped] {5};
%\draw (4) -- (5) node [midway,above,sloped] {7};
\draw[put coord sys] (2) -- (3);
\draw[put coord sys] (3) -- (4);
\draw[put coord sys] (2) -- (4);
\draw[put coord sys] (1) -- (3);
\draw[put coord sys] (1) -- (5);
\draw[put coord sys] (4) -- (5);
\draw[put coord sys] (3) -- (5);
\end{tikzpicture}
\end{document}
命令的“技巧”\draw[gray] ++(150:0.3) -- (0,0);
效果不佳。事实上,这种方式根本不起作用 :) 有人知道如何获得这些三角形吗?
答案1
图书馆的解决方案tikz-mec
(文档仅有意大利语,但示例和形状细节使其非常容易使用):
\documentclass[tikz,png,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc,decorations.markings,mec}
\begin{document}
\begin{tikzpicture}[scale=1,thick,dot/.style={draw,circle,minimum size=4pt,inner sep=0pt,},
put coord sys/.style={
decoration={markings,
mark= at position 0.5
with
{
\draw[-latex,ultra thin,blue!80!black] (2pt,2pt) -- ++(5mm,0) node[above right,rotate=\pgfdecoratedangle] {$\scriptstyle x$};
\draw[-latex,ultra thin,green!80!black] (2pt,2pt) -- ++(0,5mm) node[above right,rotate=\pgfdecoratedangle] {$\scriptstyle y$};
}
},
postaction=decorate}]
\draw [help lines] (0,0) grid (3,4);
\node[dot] (2) at (0,0) [label={[label distance=1mm]270:2}] {};
\node[dot] (3) at (0,2) [label={[label distance=1mm]180:3}] {};
\node[dot] (1) at (0,4) [label={[label distance=1mm]90:1}] {};
\node[dot] (4) at (3,2) [label={[label distance=1mm]0:4}] {};
\node[dot] (5) at (2.5,4) [label={[label distance=1mm]45:5}] {};
\node[draw,hinge,grounded=270,scale=0.85,transform shape] (hinge-1) at (0,4){};
\node[draw,hinge,grounded=270,scale=0.85,transform shape] (hinge-2) at (0,0){};
% with a loop is easier
\foreach \source/\dest in {2/3,3/4,2/4,1/3,1/5,4/5,3/5}
\draw[put coord sys] (\source) -- (\dest);
% background addition
\begin{scope}[on background layer]
\fill[green!70!lime!15 ]
($(hinge-1.south west)+(-0.5,1)$)--
($(hinge-1.south west)+(4.5,1)$)coordinate (x)--
($(x)+(0,-3)$)coordinate (y)--
($(y-|hinge-1.south east)-(0.5,0)$)--
cycle;
\draw[green!70!lime!50]($(hinge-1.south west)+(-0.5,1)$)--
($(hinge-1.south west)+(4.5,1)$)coordinate (x)--
($(x)+(0,-3)$)coordinate (y)
($(hinge-1.south west)+(-0.5,1)$)--($(hinge-1.south west)-(0.5,2)$);
\end{scope}
\end{tikzpicture}
\end{document}
结果:
已经实现了 foreach 循环以避免写入某些路径,并且引入了背景以类似于发布的图片。
答案2
一种方法是定义三角形的宏,如下所示:
\newcommand*{\Triangle}[2][]{%
\draw [#1] #2 -- ++(-0.2,0.2) -- ++(0,-0.4) -- cycle;
}%
其中#1
是任何可选的绘制选项,#2
是放置的坐标。该xshift=-1mm
选项用于将三角形移到左侧,因此您可以调整它以使三角形位于所需的位置。
笔记:
正如 Qrrbrbirlbel 所建议的,您还可以使用节点标签来放置三角形,例如:
\Triangle[gray]{(2.west)} \Triangle[gray]{(1.center)}
代码:
\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{calc,decorations.markings}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=1,inner sep=0pt,thick,dot/.style={draw,circle,minimum size=4pt},
put coord sys/.style={
decoration={markings,
mark= at position 0.5
with
{
\draw[-latex,ultra thin,blue!80!black] (2pt,2pt) -- ++(5mm,0) node[above right,rotate=\pgfdecoratedangle] {$\scriptstyle x$};
\draw[-latex,ultra thin,green!80!black] (2pt,2pt) -- ++(0,5mm) node[above right,rotate=\pgfdecoratedangle] {$\scriptstyle y$};
}
},
postaction=decorate}]
%\draw [help lines] (0,0) grid (3,4);
\node[dot] (2) at (0,0) [label={[label distance=1mm]270:2}] {};
\node[dot] (3) at (0,2) [label={[label distance=1mm]180:3}] {};
\node[dot] (1) at (0,4) [label={[label distance=1mm]90:1}] {};
\node[dot] (4) at (3,2) [label={[label distance=1mm]0:4}] {};
\node[dot] (5) at (2.5,4) [label={[label distance=1mm]45:5}] {};
\newcommand*{\Triangle}[2][]{%
\draw [#1] #2 -- ++(-0.2,0.2) -- ++(0,-0.4) -- cycle;
}%
\Triangle[gray,xshift=-1mm]{(0,0)}
%\draw[gray] ++(150:0.3) -- (0,0);
%\draw[gray] ++(-150:0.3) -- (0,0);
%\draw[gray] ++(180:0.3) -- (-.3,0);
\Triangle[gray,xshift=-1mm]{(0,4)}
%\draw[gray] ++(0,4) -- (150:1);
%\draw[gray] ++(-150:0.3) -- (0,4);
%\draw[gray] ++(180:0.3) -- (-.3,4);
%\draw (2) -- (3) node [midway,sloped,above] {3};
%\draw (3) -- (4) node [midway,above,sloped] {4};
%\draw (2) -- (4) node [midway,above,sloped] {6};
%\draw (3) -- (1) node [midway,above,sloped] {2};
%\draw (1) -- (5) node [midway,above,sloped] {1};
%\draw (3) -- (5) node [midway,above,sloped] {5};
%\draw (4) -- (5) node [midway,above,sloped] {7};
\draw[put coord sys] (2) -- (3);
\draw[put coord sys] (3) -- (4);
\draw[put coord sys] (2) -- (4);
\draw[put coord sys] (1) -- (3);
\draw[put coord sys] (1) -- (5);
\draw[put coord sys] (4) -- (5);
\draw[put coord sys] (3) -- (5);
\end{tikzpicture}
\end{document}