5

I started to draw some simple points and curves in a tikzpicture. But I am not sure if it's the best idea to use specific coordinates anymore. In this case I have to make up some linear function as basis and calculate all necessary points... is there an option to draw additional lines to the 'middle' of the so far existing line?

\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}

I still need those dashed lines and some red arrows in between. I there a way of relative positioning those elements, or should I just calculate the missing coordinates. What is the right thing to do? Thanks enter image description here

Update

I now modified my code according to your answers. The perpendicular way is very convincing. I understood all steps execept for why the coordinate label M hits the middle of the line (somehow by definition). But maybe I find more information in the manual.

\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}

So here's my result:

enter image description here

Thank you very much

2 Answers 2

3

If you have three points, say x, y, and a, then you can draw a perpendicular from a on to xy by orthogonal coordinates like

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

This needs calc library. To findt the point of intersection of vertical line and curve, use intersections library as Peter did. A sample will be:

\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}

enter image description here

5
  • Each of your answers fascinates me, how came you know all this stuff :) thx for advice
    – Mac
    Commented Oct 24, 2015 at 16:06
  • Is there an option for linestyle despite "dotted" or "dashed" which can be adjusted to be "loosely" or "densely", that I can create some dashed line which just contains "longer" dashes?
    – Mac
    Commented Oct 24, 2015 at 17:08
  • @MacUse dash pattern like dash pattern=on 5pt off 2pt, Here on draws the line by the given amount and off doesn't draw until given amount. Adjust 5pt and 2pt as you wish. For example try dash pattern=on 5pt off 10pt,
    – user11232
    Commented Oct 24, 2015 at 23:33
  • Now I think I got most of it! Can you explain the command \draw (L) -- coordinate (M) (R); more in detail? I just don't see why a coordinate label M really give the middle of the lines, it could have been anywhere?
    – Mac
    Commented Oct 25, 2015 at 8:44
  • @Mac: It defines a coordinate on the line LR. The default position is 0.5 (i.e., midway). If you want, you can change it with pos=0.3 etc.
    – user11232
    Commented Oct 25, 2015 at 8:57
8

I would recommend you use the Tikz calc library to do the calculations and the intersections library to compute the intersection of the vertical line with the curve

enter image description here

The line

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

defines the coordinate (Middle Of L And R) to be 0.5 of the way from (L) to (R).

Note:

  • I got a bit lazy with the intersection with the vertical axis and had to move the axis drawing to the end to remedy that. It is certainly possible to compute the intersection with the y-axis properly.

Code:

\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} 
9
  • Why do you even need to define Lx, you could just project L, I usually just define O as the origin and project on that point. Save a little, ad might end up simplifying the code a bit.
    – daleif
    Commented Oct 24, 2015 at 9:58
  • @daleif: Yep, that would improve things, but since Lx was already specified in the original code I thought it was ok to use it. Commented Oct 24, 2015 at 10:01
  • Of course, never looked at the OPs code, but might be a good addition to note that a lot of this can be simplified. Btw: I don't quite understand your note about the axis drawing?
    – daleif
    Commented Oct 24, 2015 at 10:03
  • That one might also be easier with just below=length just to show that feature.
    – daleif
    Commented Oct 24, 2015 at 10:04
  • @daleif: Move the axis drawing to the beginning and you'll see the red dashed line goes to (0,0). Commented Oct 24, 2015 at 10:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .