Tikz:找到一条线的中间(相对定位)

Tikz:找到一条线的中间(相对定位)

我开始在 tikzpicture 中绘制一些简单的点和曲线。但我不确定使用特定坐标是否是最好的主意。在这种情况下,我必须构造一些线性函数作为基础并计算所有必要的点……是否有选项可以在迄今为止存在的线的“中间”绘制额外的线?

\begin{tikzpicture}[thick]
    % Axis
    \coordinate (y) at (0,6);
    \coordinate (x) at (6,0);
        % Labels
    \draw[<->,line width=1.5pt] (y) node[above] {total savings} -- (0,0) --  (x) node[below,yshift=-0.7cm]
    {income \$};
        
        % Some coordinates and lines
        \coordinate (L) at (1,1);
        \coordinate (R) at (5,4.5);
        \coordinate (Rx) at (5,0);
        \coordinate (Ry) at (0,4.5);
        \coordinate (Lx) at (1,0);
        \coordinate (Ly) at (0,1);
        
        \draw  (L) -- (R);
        \draw  (L) to[out=0,in=-100] (R);
        
        \filldraw [black] (L) circle (2pt);
        \filldraw [black] (R) circle (2pt);
        
        \draw[dotted, ultra thick] (L) -- (Lx) node[below] {\small 5,000};
        \draw[dotted, ultra thick] (Ly) -- (L);
        \draw[dotted, ultra thick] (R) -- (Rx) node[below] {\small 55,000};
\end{tikzpicture}

我仍然需要那些虚线和中间的一些红色箭头。有没有办法相对定位这些元素,或者我应该只计算缺失的坐标。正确的做法是什么?谢谢 在此处输入图片描述

更新

我现在根据您的回答修改了我的代码。垂直方式非常有说服力。我理解所有步骤,除了为什么坐标标签 M 会碰到线的中间(根据定义)。但也许我在手册中找到更多信息。

\begin{tikzpicture}[thick]
    % Axis and coordinates
    \coordinate (y) at (0,6);
    \coordinate (o) at (0,0);
    \coordinate (x) at (6,0);
    \coordinate (L) at (1,1);
    \coordinate (R) at (5,4.5);
        
        % Axis labels
    \draw[<->,line width=1.5pt] (y) node[above] {Total savings} -- (o) -- (x) node[below,yshift=-5mm]{Income \$};
    
        % basic line and curve
    \draw (L) -- coordinate (M) (R);
        \draw[name path=curve]  (L) to[out=0,in=-100] (R);

    % vertical dashed lines: e.g. perpendicular from (R) -- ($(o)!(R)!(x)$)
  \draw[dash pattern=on 6pt off 3pt, thick, gray] (R) -- ($(o)!(R)!(x)$) 
    node[below](55) {\black \footnotesize 55,000};
  \draw[dash pattern=on 6pt off 3pt, thick, gray] (L) -- ($(o)!(L)!(x)$) 
    node[below] (5) {\black \footnotesize 5,000};
  \draw[dash pattern=on 6pt off 3pt,gray,thick,name path=vertical] (M) -- ($(o)!(M)!(x)$) 
    node[below,text=black] (30) {\footnotesize 30,000};

    % horizontal dashed lines
    % perpendicular from M to y-axis
    % coordinate-pos shifts the red arrow position, percentage value starting from M (to the left)
  \draw[dash pattern=on 6pt off 3pt,thick, gray] (M) -- ($(o)!(M)!(y)$) coordinate[pos=0.8](y2);
  % intersection, better to access by names than by calculation
    \coordinate [name intersections={of=curve and vertical,by={V}}];
  \draw[dash pattern=on 6pt off 3pt,thick, gray] (V) -- ($(o)!(V)!(y)$) coordinate[pos=0.8](y1);

    % picture label
    \draw (0,1) node[circle, draw] at (3,5) {A};
                
        % three dots (last due to layer)
    \filldraw [black] (L) circle (2pt);
    \filldraw [black] (R) circle (2pt);
    \filldraw [black] (M) circle (2pt);
                
        % draw red arrows
    \draw[->,red, ultra thick] (y2) -- (y1);
    \draw[->,red, thick] (5) -- (30);
    \draw[->,red] (55) -- (30);
        
\end{tikzpicture}

这是我的结果:

在此处输入图片描述

非常感谢

答案1

如果你有三个点,比如xya,那么你可以用正交坐标从a到画一条垂线xy,比如

\draw (a) -- ($(x)!(a)!(y)$)

这需要calc库。要找到垂直线和曲线的交点,intersections请像 Peter 那样使用库。示例如下:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc,intersections}

\begin{document}
\begin{tikzpicture}[thick]
    % Axis
    \coordinate (y) at (0,6);
    \coordinate (o) at (0,0);
    \coordinate (x) at (6,0);
        % Labels
    \draw[<->,line width=1.5pt] (y) node[above] {total savings} -- (o)
                                   --  (x) node[below,yshift=-3mm]{Income \$};

        % Some coordinates and lines
        \coordinate (L) at (1,1);
        \coordinate (R) at (5,4.5);

        \draw  (L) -- coordinate (M) (R);
        \draw[name path=curve]  (L) to[out=0,in=-100] (R);

        \filldraw [black] (L) circle (2pt);
        \filldraw [black] (R) circle (2pt);
        \filldraw [red] (M) circle (2pt);

        \draw[dotted, ultra thick] (R) -- ($(o)!(R)!(x)$) node[below](50) {50,000};
        \draw[dotted, ultra thick] (L) -- ($(o)!(L)!(x)$) node[below] (5) {5,000};

        \draw[dotted,red] (M) -- ($(o)!(M)!(y)$) coordinate[pos=0.9](y2);
        \draw[dotted,red,name path=vertical] (M) -- ($(o)!(M)!(x)$)node[below,text=black] (30) {30,000};

        \coordinate [name intersections={of=curve and vertical,by={V}}];

        \draw[dotted,red] (V) -- ($(o)!(V)!(y)$)coordinate[pos=0.9](y1);

        \draw[->,red] (y2) -- (y1);
        \draw[->,red] (5) -- (30);
        \draw[->,red] (50) -- (30);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我建议您使用 Tikzcalc库进行计算,并使用intersections库计算垂直线与曲线的交点

在此处输入图片描述

线路

\coordinate (Middle Of L And R) at ($(L)!0.5!(R)$);

定义坐标(Middle Of L And R)为从到 的0.5距离。(L)(R)

笔记:

  • 我在计算与垂直轴的交点时有点懒,所以不得不将轴图移到末尾来解决这个问题。当然可以正确计算与 y 轴的交点。

代码:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}[thick]
    % Axis
    \coordinate (y) at (0,6);
    \coordinate (x) at (6,0);
        % Labels
%    \draw[<->,line width=1.5pt] (y) node[above] {total savings} -- (0,0) --  (x) node[below,yshift=-0.7cm]
%    {income \$}; %% <--- moved below

        % Some coordinates and lines
        \coordinate (L) at (1,1);
        \coordinate (R) at (5,4.5);
        \coordinate (Rx) at (5,0);
        \coordinate (Ry) at (0,4.5);
        \coordinate (Lx) at (1,0);
        \coordinate (Ly) at (0,1);

        \draw [blue] (L) -- (R);
        \draw [name path=My Curve] (L) to[out=0,in=-100] (R);
        
        \coordinate (Middle Of L And R) at ($(L)!0.5!(R)$);
        \coordinate (Middle x) at ($(Lx)!0.5!(Rx)$);
        \coordinate (Middle y) at ($(Ly)!0.5!(Ry)$);
        
        \filldraw [red] (Middle Of L And R) circle (2pt);
        \draw [dashed,red, name path=Vertical Line] (Middle Of L And R) -- (Middle x);
        \draw [dashed,red] (Middle Of L And R) -- (Middle y);
        
        \draw [dashed,red, name intersections={of=My Curve and Vertical Line}]
            (intersection-1) -| (0,0);

        \filldraw [black] (L) circle (2pt);
        \filldraw [black] (R) circle (2pt);

        \draw[dotted, ultra thick] (L) -- (Lx) node[below] {\small 5,000};
        \draw[dotted, ultra thick] (Ly) -- (L);
        \draw[dotted, ultra thick] (R) -- (Rx) node[below] {\small 55,000};
        
    \draw[<->,line width=1.5pt] (y) node[above] {total savings} -- (0,0) --  (x) node[below,yshift=-0.7cm]
    {income \$};
\end{tikzpicture}
\end{document} 

相关内容