\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[domain=-1.5:3,scale=0.8]
\draw[gray!50, thin, step=0.5] (-3,-2) grid (10,6.5);
\foreach \x in {-3,...,9} \draw (\x,0.05) -- (\x,-0.05) node[below] {\tiny\x};
\foreach \y in {-2,...,6} \draw (-0.05,\y) -- (0.05,\y) node[right] {\tiny\y};
\node at (3.5,5) {$B$};
\node at (9,-0.5) {$C$};
\node at (-2,-0.6) {$A$};
\filldraw[black] (-2, -1) circle(1.5pt);
\filldraw[black] (4, 5) circle(1.5pt);
\filldraw[black] (8, 0) circle(1.5pt);
\filldraw[black] (3, -0.5) circle(1.5pt);
\draw[->] (-3,0) -- (10,0) node[below right] {$X$};
\draw[->] (0,-2) -- (0,6) node[above] {$Y$};
\draw[color=green!40!black,ultra thick][domain=-2.5:5] plot (\x, 1+\x) node[right] {$x-y+1=0$};
\draw[color=red!60!black,ultra thick][domain=3:9.5] plot (\x, 10-5/4*\x) node[below,left] {$5x+4y-40=0$};
\draw[color=red,ultra thick][domain=-3:9.5] plot (\x,-4/5+1/10*\x) node[right,above] {$x-10y-8=0$};
\draw[thick,dashed][domain=3.9:4.65] plot (\x,45-10*\x) node[right,above] {$H_B$};
\draw[thick,dashed,blue][domain=2.8:4.2] plot (\x,-17+5.5*\x) node[right,above] {$M_B$};
\end{tikzpicture}
\end{document}
我想将直线方程放在直线的中间,倾斜。如果我使用选项,node[midway,sloped]
则不会得到理想的结果。我该如何正确放置这些方程?
答案1
像这样吗?
如果我没错的话,很难插入nodes
用选项定义的路径,plot
而这正是您用来绘制最终线条的路径。
解决方案包括绘制线条plot
并使用另一个命令将标签放置在其上。
在下面的代码中,还有一些尊重原始代码的更改。
第一个是,我没有绘制(隐藏)圆圈并在稍后标记它们,而是使用dot
带有标签的节点:
\node[dot, label=100:$A$] at (-2, -1) (A) {};
这样我就可以以此(A)
作为参考。
除了抑制端节点之外,命令无需做任何改变,plot
因为相应的方程式稍后会写出来。
由于节点 A、B 和 C 已经定义,因此它们可以作为放置方程式的直线的参考。例如:
\path (B) -- node[sloped, above, color=red!60!black] {$5x+4y-40=0$} (C);
我尝试使用pos
和above/below
选项来调整方程式的位置,但您可以根据需要进行调整。
完整代码如下:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[domain=-1.5:3,
scale=0.8,
dot/.style={circle, draw, fill, minimum size=1.5pt, inner sep=1pt}
]
\draw[gray!50, thin, step=0.5] (-3,-2) grid (10,6.5);
\foreach \x in {-3,...,9} \draw (\x,0.05) -- (\x,-0.05) node[below] {\tiny\x};
\foreach \y in {-2,...,6} \draw (-0.05,\y) -- (0.05,\y) node[right] {\tiny\y};
\node[dot, label=100:$A$] at (-2, -1) (A) {};
\node[dot, label=west:$B$] at (4, 5) (B) {};
\node[dot, label=80:$C$] at (8, 0) (C) {};
\draw[->] (-3,0) -- (10,0) node[below right] {$X$};
\draw[->] (0,-2) -- (0,6) node[above] {$Y$};
\draw[color=green!40!black,ultra thick][domain=-2.5:5] plot (\x, 1+\x);
\path (A) -- node[pos=.65, sloped, above, color=green!40!black] {$x-y+1=0$} (B);
\draw[color=red!60!black,ultra thick][domain=3:9.5] plot (\x, 10-5/4*\x);
\path (B) -- node[sloped, above, color=red!60!black] {$5x+4y-40=0$} (C);
\draw[color=red,ultra thick][domain=-3:9.5] plot (\x,-4/5+1/10*\x);
\path (A) -- node[pos=.85,sloped, below=1mm, color=red] {$x-10y-8=0$} (C);
\draw[thick,dashed][domain=3.9:4.65] plot (\x,45-10*\x) node[right,below] {$H_B$};
\draw[thick,dashed,blue][domain=2.8:4.2] plot (\x,-17+5.5*\x) node[right,above] {$M_B$};
\end{tikzpicture}
\end{document}