向新的 tikz 命令添加参数

向新的 tikz 命令添加参数

我想向 tikz 命令添加一个参数。我现在在镜子上有 5 条射线,但我想添加一个角度和一个射线在镜子上反射的点。那么,是否可以使用命令 \lichtraalwk 中的参数更改点 B 的位置,我想更改射线的角度(现在在 A 中)。这样我就不用再画 5 次相同的图形了。

\documentclass{article}
\usepackage{tkz-euclide}
\usepackage{tikz}
\usetkzobj{all} 
\newcommand{\midarrow}{\tikz \draw[-triangle 60] (0,0) -- +(.1,0);}

\begin{document}
\begin{tikzpicture}
\tkzInit[ymin=0,ymax=5,xmin=0,xmax=10]
\tkzClip
%\tkzGrid
\tkzDefPoints{0/1/C, 10/1/D, 12/0.8/F}

\newcommand\lichtstraalwk{
\tkzDefPoints{5/1/B}
\tkzDefShiftPoint[B](135:4){A};
\tkzDefShiftPoint[B](90:2){K};
\tkzDefShiftPoint[B](-90:3){L};
\tkzDefPointBy[reflection = over K--L](A) \tkzGetPoint{E};
\draw (A)-- node[sloped,pos=0.2] {\midarrow} (B) ;
\draw[orange] (B)-- node[sloped,pos=0.6] {\midarrow} (E);
\tkzDrawSegment[very thick](C,D);
\draw[dashed] (K) -- (L) ;
}

\begin{scope}[xshift=-2cm,yshift=0cm] \lichtstraalwk \end{scope}
\begin{scope}[xshift=-1cm,yshift=0cm] \lichtstraalwk \end{scope}
\begin{scope}[xshift=0cm,yshift=0cm]  \lichtstraalwk \end{scope}
\begin{scope}[xshift=1cm,yshift=0cm]  \lichtstraalwk \end{scope}
\begin{scope}[xshift=2cm,yshift=0cm]  \lichtstraalwk \end{scope}
\draw [fill,pattern=north east lines,draw=none] (C) rectangle (F);

\end{tikzpicture}

\end{document}

在此处输入图片描述

这样我就可以做出这样的事情: 在此处输入图片描述

答案1

像这样?

\documentclass{article}
\usepackage{tkz-euclide}
 %\usepackage{tikz}
\usetkzobj{all}
\newcommand{\midarrow}{\tikz \draw[-triangle 60] (0,0) -- +(.1,0);}

\begin{document}
\begin{tikzpicture}
\tkzInit[ymin=0,ymax=5,xmin=0,xmax=10]
\tkzClip
%\tkzGrid

\newcommand\lichtstraalwk[2]{%
\tkzDefPoints{0/1/C, 10/1/D, 12/0.8/F}   %% moved inside \newcommand
\tkzDefPoints{#1/1/B}
\tkzDefShiftPoint[B](#2:4){A};
\tkzDefShiftPoint[B](90:2){K};
\tkzDefShiftPoint[B](-90:3){L};
\tkzDefPointBy[reflection = over K--L](A) \tkzGetPoint{E};
\draw (A)-- node[sloped,pos=0.2] {\midarrow} (B) ;
\draw[orange] (B)-- node[sloped,pos=0.6] {\midarrow} (E);
\tkzDrawSegment[very thick](C,D);
\draw[dashed] (K) -- (L) ;
\draw [fill,pattern=north east lines,draw=none] (C) rectangle (F);   %% this too moved inside
}

\foreach \x/\ang in {4/135,5/165}{
\lichtstraalwk{\x}{\ang}
}

\end{tikzpicture}

\end{document}

在此处输入图片描述

以下是奖金:

\documentclass{article}
\usepackage{tkz-euclide}
%\usepackage{tikz}
\usetkzobj{all}
\newcommand{\midarrow}{\tikz \draw[-triangle 60] (0,0) -- +(.1,0);}

\begin{document}
\begin{tikzpicture}
\tkzInit[ymin=0,ymax=5,xmin=0,xmax=10]
\tkzClip
%\tkzGrid
\newcommand\lichtstraalwk[2]{%
\tkzDefPoints{0/1/C, 10/1/D, 12/0.8/F}   %% this moved inside
\tkzDefPoints{#1/1/B}
\tkzDefPoint(85:4){A}
%\tkzDefShiftPoint[B](135:4){A};
\tkzDefShiftPoint[B](90:6){K};
\tkzDefShiftPoint[B](-90:3){L};
\tkzDefPointBy[reflection = over K--L](A) \tkzGetPoint{E};
\draw (A)-- node[sloped,pos=0.2] {\midarrow} (B) ;
\draw[orange] (B)-- node[sloped,pos=0.6] {\midarrow} (E);
\tkzDrawSegment[very thick](C,D);
\draw[dashed] (K) -- (L) ;
\draw [fill,pattern=north east lines,draw=none] (C) rectangle (F);   %% this too moved inside
}

\foreach \x in {2,5}{
\lichtstraalwk{\x}
}

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容