2

After placing a node in an arbitrary position between two horizontal lines, I would like to draw the diagonal line to a point B and then a series of oblique lines as in this picture:

enter image description here

They should all have the same slope.

The point A is a generic point between the two lines; the point B is a generic point belonging to the upper line. It is always right of A.

I tried with this code.

\documentclass[border=2mm,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\begin{document}
\begin{tikzpicture}

\coordinate (a0) at (0,0);
\coordinate (a1) at (10,0);
\coordinate (a2) at (10,4);
\coordinate (Bbase) at (6,-1);

\draw (a0) -- (a1);
\draw (a0 |- a2) -- (a2);
\node (A) at (2,1) {A};
\draw (A) -- (Bbase |- a2) node[above] {B};

\end{tikzpicture}
\end{document}

This is the result:

enter image description here

How to proceed? The solution should not depend on the slope of this particular case: the correct slope should be determined whatever A and B are (as far as they respect the conditions listed above).

BowPark
  • 1,213

1 Answers1

2

TikZ has a decoration called zigzag. Most likely I misinterpret the question. Here is a macro that draws the continuation. (\gettikzxy is not needed at this point, but it might be useful if you plan to make this macro more flexible.)

\documentclass[border=2mm,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\usetikzlibrary{decorations.pathmorphing,intersections}

\makeatletter % from https://tex.stackexchange.com/a/412901/121799
\newcommand{\Distance}[3]{% % from https://tex.stackexchange.com/q/56353/121799
\tikz@scan@one@point\pgfutil@firstofone($#1-#2$)\relax  
\pgfmathsetmacro{#3}{veclen(\the\pgf@x,\the\pgf@y)/28.45274}
}
% from https://tex.stackexchange.com/q/56353/121799
\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \global\edef#2{\the\pgf@x}%
  \global\edef#3{\the\pgf@y}%
}
\makeatother 

\newcommand{\DrawZigZags}[5][]{% #2=A, #3=B, #4=lower line, #5=end
\Distance{(#2)}{(#2-|#3)}{\myx}
\Distance{(#2)}{(#2|-#3)}{\myy}
\Distance{(#2)}{(#2|-#4)}{\myz}
\typeout{\myx,\myy,\myz}
\path[name path=AB] (#2) -- (#3);
\path[name path=hori] (#2|-0,{0.5*(\myy+\myz)*1cm}) --
(#5|-0,{0.5*(\myy+\myz)*1cm});
\path[name intersections={of=AB and hori, name=start}];
\draw [decorate, decoration={zigzag,amplitude={0.5*(\myy+\myz)*1cm},segment
  length={2*(\myx/\myy)*(\myy+\myz)*1cm}},#1] 
  (start-1) -- (start-1-|#5);
}

\begin{document}
\begin{tikzpicture}

\coordinate (a0) at (0,0);
\coordinate (a1) at (18,0);
\coordinate (a2) at (18,4);
\coordinate (Bbase) at (4,-1);

\draw (a0) -- (a1);
\draw (a0 |- a2) -- (a2);
\node (A) at (2,1) {A};
\draw (A) -- (Bbase |- a2) node[above] {B};

\coordinate (B) at (Bbase |- a2);  

\DrawZigZags[blue]{A}{B}{a0}{19.1,0}

\end{tikzpicture}
\end{document}

enter image description here

  • Your line looks perfect. However, what is the relation between the two points A and B and your parameters 20mm, 80mm and also the points (3,2) -- (19.1,2)? How did you choose them? – BowPark Apr 05 '18 at 15:19
  • @BowPark I added a macro of this type. –  Apr 05 '18 at 16:26
  • Thank you so much. I wrote the details of the comments in the question. – BowPark Apr 05 '18 at 21:54