是否可以使用tikz
样式来自动执行下列操作?
\documentclass[tikz,border=1cm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[
plotmark/.style = {%
draw, fill=lightgray, circle, inner sep=0pt, minimum size=4pt
}
]
\coordinate (A) at (0,0);
\coordinate (B) at (0,7);
\coordinate (C) at (7,5);
\coordinate (AnBC) at ($(B)!(A)!(C)$);
\draw (B)--(C);
\draw (A)--(AnBC);
\draw[gray!10] (A)--(C);
\draw[gray!25] (A)--($(B)!0.8!(C)$);
\draw[gray!50] (A)--($(B)!0.6!(C)$);
\draw[gray!75] (A)--($(B)!0.4!(C)$);
\node[plotmark, label={below left:$A$}] at (A) {};
\node[plotmark, label={above left:$B$}] at (B) {};
\node[plotmark, label={above right:$C$}] at (C) {};
\end{tikzpicture}
\end{document}
在两个给定点之间,我应该能够确定线条的数量,并以增加的不透明度绘制线条。类似于:
\draw[motion lines={lines=4, start opacity=0.2, end opacity=1, color=blue} (C)--(AnBC);
答案1
TeXnician 已经告诉过你最重要的要素:循环当然,你可以定义使用此类循环的样式。我尽可能遵循了你对语法的说明,因此你可以这样做
\draw[blue,set motion lines={lines=4,start opacity=0.2},rays=from A to line from C to AnBC];
平均能量损失
\documentclass[tikz,border=1cm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[
plotmark/.style = {%
draw, fill=lightgray, circle, inner sep=0pt, minimum size=4pt
},rays/.style args={from #1 to line from #2 to #3}{insert path={
foreach \X in {0,...,\pgfkeysvalueof{/tikz/motion lines/lines}}
{[/utils/exec=\pgfmathsetmacro{\myopa}{\pgfkeysvalueof{/tikz/motion lines/start opacity}
+(\pgfkeysvalueof{/tikz/motion lines/end opacity}-\pgfkeysvalueof{/tikz/motion lines/start opacity})
*\X/\pgfkeysvalueof{/tikz/motion lines/lines}}]
(#1) edge[opacity=\myopa] ($(#2)!{\X/\pgfkeysvalueof{/tikz/motion lines/lines}}!(#3)$)}}},
set motion lines/.code={\tikzset{motion lines/.cd,#1}},
motion lines/.cd,
lines/.initial=5,
start opacity/.initial=0.1,
end opacity/.initial=1,
]
\coordinate (A) at (0,0);
\coordinate (B) at (0,7);
\coordinate (C) at (7,5);
\coordinate (AnBC) at ($(B)!(A)!(C)$);
\draw (B)--(C);
\draw[dotted,rays=from A to line from C to AnBC];
\node[plotmark, label={below left:$A$}] at (A) {};
\node[plotmark, label={above left:$B$}] at (B) {};
\node[plotmark, label={above right:$C$}] at (C) {};
\begin{scope}[xshift=10cm]
\coordinate (A) at (0,0);
\coordinate (B) at (0,7);
\coordinate (C) at (7,5);
\coordinate (AnBC) at ($(B)!(A)!(C)$);
\draw (B)--(C);
\draw[blue,set motion lines={lines=4,start opacity=0.2},rays=from A to line from C to AnBC];
\node[plotmark, label={below left:$A$}] at (A) {};
\node[plotmark, label={above left:$B$}] at (B) {};
\node[plotmark, label={above right:$C$}] at (C) {};
\end{scope}
\end{tikzpicture}
\end{document}
答案2
一种简单的基于循环的方法:
\documentclass[tikz,border=1cm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[
plotmark/.style = {%
draw, fill=lightgray, circle, inner sep=0pt, minimum size=4pt
}
]
\coordinate (A) at (0,0);
\coordinate (B) at (0,7);
\coordinate (C) at (7,5);
\coordinate (AnBC) at ($(B)!(A)!(C)$);
\draw (B)--(C);
\draw (A)--(AnBC);
\pgfmathtruncatemacro\parts{5}
\foreach \tikzi in {1,...,\parts} {
\pgfmathtruncatemacro\colorspec{100-10*\tikzi}
\draw[gray!\colorspec] (A) -- ($(AnBC)!\tikzi/\parts!(C)$);
}
\node[plotmark, label={below left:$A$}] at (A) {};
\node[plotmark, label={above left:$B$}] at (B) {};
\node[plotmark, label={above right:$C$}] at (C) {};
\end{tikzpicture}
\end{document}