在 TikZ 中从一个点到另一条线(沿 x 轴或 y 轴)绘制一条线

在 TikZ 中从一个点到另一条线(沿 x 轴或 y 轴)绘制一条线

我想利用交叉工具绘制一条线到另一条线,使其仅沿 x 轴或 y 轴延伸。我的意思是我目前思考我会通过交叉部分来实现这一点……但我对此并不完全确定。我希望所附图片能正确说明这一点。这可能吗?

笔记:坐标值仅仅为了演示而这么简单。 编辑:编辑了更好的代码和截图来澄清问题,感谢杰克。

具有目标点的图像,其值仅在一个轴上变化

平均能量损失

    \documentclass[
a4paper
]{scrartcl}

\usepackage{
lmodern,
}
\usepackage[T1]{fontenc}
\usepackage{
tikz
}
\usetikzlibrary{calc}


\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
    %
    \draw[style=help lines,step=0.5cm] (0,0) grid (6.2,6.2);
    %
    \draw[->,thick] (-0.1,0) -- (6.5,0) node[anchor=west]{x};
    \draw[->,thick] (0,-0.1) -- (0,6.5) node[anchor=south]{y};
    %
    \foreach \x in {0,1,...,6} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
    \foreach \y in {0,1,...,6} \draw [thick](-2pt,\y) -- (2pt,\y);
    %
    \foreach \x in {0,1,...,6} \draw (\x cm, 0 cm) node[anchor=north]{\x};
    \foreach \y in {0,1,...,6}  \draw (0 cm, \y cm) node[anchor=east]{\y};
    %
    \begin{scope}[color=black]
    \filldraw (1,1) circle (0.08cm) node (A) {} node[anchor=north,fill=white,yshift=-0.1cm] {A};
    \filldraw (6,6) circle (0.08cm) node (B) {} node[anchor=west,fill=white,xshift=5pt] {B};
    \filldraw (4,2) circle (0.08cm) node (C) {} node[anchor=south,fill=white,yshift=0.1cm] {C};
    \end{scope}
    \draw[very thick] (A) -- (B);
    \draw[->,>=latex,very thick,dashed] (C.center) -- ($(A)!(C)!(B)$);
    \draw (2,5) node[fill=white] {\textcolor{red}{from C\ldots}};
    \draw[color=red,very thick] (2,2) circle (0.1cm) node[above,yshift=5pt,fill=white]{to this point};
    \draw[color=red,very thick] (4,4) circle (0.1cm) node[below,yshift=-5pt,fill=white]{or this one};
    %\node (Ex) at (....)?
    %\draw[->,>=latex',very thick,dashed] (E.center) -- ($(A)!(E.135)!(B)$);    
\end{tikzpicture}
\end{center}
\end{document}

答案1

加载库后intersections,使用 命名所涉及的路径name path=<name>,然后可以使用name intersections={<options>}键查找交点(详细信息请参见第节13.3.2 任意路径的交点pgf 手册)。

\documentclass[a4paper]{scrartcl}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}


\begin{document}

\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
    %
    \draw[style=help lines,step=0.5cm] (0,0) grid (6.2,6.2);
    %
    \draw[->,thick] (-0.1,0) -- (6.5,0) node[anchor=west]{x};
    \draw[->,thick] (0,-0.1) -- (0,6.5) node[anchor=south]{y};
    %
    \foreach \x in {0,1,...,6} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
    \foreach \y in {0,1,...,6} \draw [thick](-2pt,\y) -- (2pt,\y);
    %
    \foreach \x in {0,1,...,6} \draw (\x cm, 0 cm) node[anchor=north]{\x};
    \foreach \y in {0,1,...,6}  \draw (0 cm, \y cm) node[anchor=east]{\y};
    %
    \begin{scope}[color=black]
    \filldraw (1,1) circle (0.08cm) node (A) {} node[anchor=north,fill=white,yshift=-0.1cm] {A};
    \filldraw (6,6) circle (0.08cm) node (B) {} node[anchor=west,fill=white,xshift=5pt] {B};
    \filldraw (4,2) circle (0.08cm) node (C) {} node[anchor=south,fill=white,yshift=0.1cm] {C};
    \end{scope}
    \draw[name path=diagonal,very thick] (A) -- (B);
    \draw[->,>=latex,very thick,dashed] (C.center) -- ($(A)!(C)!(B)$);
    \draw (2,5) node[fill=white] {\textcolor{red}{from C\ldots}};
    \draw[color=red,very thick] (2,2) circle (0.1cm) node[above,yshift=5pt,fill=white]{to this point};
    \draw[color=red,very thick] (4,4) circle (0.1cm) node[below,yshift=-5pt,fill=white]{or this one};

  \path[name path=line1] (C) -- +(-3,0);
  \path[name path=line2] (C) -- +(0,3);
  \draw[thick,blue,name intersections={of=diagonal and line1,by={Int1}}] (C) -- (Int1);
  \draw[thick,green,name intersections={of=diagonal and line2,by={Int2}}] (C) -- (Int2);
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

答案2

最好通过交点找到这些点。我使用了intersection of语法(它是 的包装器intersection cs)。/|-简写-|用于在 中查找坐标perpendicular cs

我擅自对代码进行了一些清理,并使用了样式。此外,标签AB等实际上都是labels。

代码

\documentclass[tikz,convert=false]{standalone}
\usepackage{lmodern}\usepackage[T1]{fontenc}
\usetikzlibrary{arrows}
\tikzset{
  dot/.style={shape=circle,inner sep=+0pt,minimum size=+1.6mm,label={#1}},
  dot/.default=,
  dot*/.style={dot={#1},fill=black},
  dot*/.default=,
  doto/.style={dot={#1},draw=red,solid,thick},
  doto/.default=,
  shorten/.style={shorten >={#1},shorten <={#1}}
}
\begin{document}
\begin{tikzpicture}[font=\sffamily\small]
    \draw[style=help lines,step=0.5cm] (0,0) grid (6.2,6.2);

    \draw[->,thick] (-0.1,0) -- (6.5,0) node[right]{x};
    \draw[->,thick] (0,-0.1) -- (0,6.5) node[left] {y};

    \foreach \x in {0,1,...,6} {
      \draw [thick](\x,-2pt) -- (\x,2pt) node[midway,below] {\x};
      \draw [thick](-2pt,\x) -- (2pt,\x) node[midway,left]  {\x};
    }

    \node[dot*=below:A] (A) at (1,1) {};
    \node[dot*=right:B] (B) at (6,6) {};
    \node[dot*=right:C] (C) at (4,2) {};

    \draw[very thick,shorten=+3pt] (A) -- (B);

    \node[doto=left:C(A)] (C'A) at (intersection of C--A|-C and A--B) {};
    \node[doto=left:C(B)] (C'B) at (intersection of C--B-|C and A--B) {};

    \path[-latex',very thick, dashed] (C) edge (C'A) edge (C'B);   
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

答案3

使用 PSTricks。它只关注有问题的部分。由于函数是线性的,因此无需使用交叉技术。

评论:

  • 左图是通过首先指定点L和来创建的R。然后由和C构造。LR

  • 右图是通过C首先指定点来创建的。

\documentclass[crop,border=12pt]{standalone}
\usepackage{pst-plot,pst-eucl}
\psset{algebraic,saveNodeCoors}

\def\f{x+.5}
\def\g{y-.5}% inverse of \f

\begin{document}

% C is defined last
\begin{pspicture}[showgrid](-1,0)(4,4)
    \pstGeonode[PointName={none,none,default},PointSymbol={none,none,default}]
        (*.5 {\f}){L}
        (*2 {\f}){R}
        (R|L){C}
    \psline[linestyle=dashed](L)(C)(R)
    \psplot[linecolor=blue]{0}{3}{\f}
\end{pspicture}

\hspace{12pt}

% C is defined first
\begin{pspicture}[showgrid](-1,0)(4,4)
    \pstGeonode(2,1){C}
    \psline[linestyle=dashed]
        (**{\g} N-C.y)
        (C)
        (*N-C.x {\f})
    \psplot[linecolor=red]{0}{3}{\f}
\end{pspicture}

\end{document}

在此处输入图片描述

相关内容