6

I am trying to draw a line with a given angle and have this line intersect with a vertical line.

When I draw an ordinary line from point B with an angle of 150 degrees and length=2, everything is fine. This is the green line,

Then I try to intersect a line from B, using the same angle and an arbitrary length of 6, with the vertical line AC. I put a node D at the intersection. Node D is not at the expected place. For illustration I drew the line B-D as well. It is clear the angle is different, and incorrect in the case I intersect.

Below the MWE:

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\coordinate [label=below: {A}] (A) at (0,0);
\coordinate [label=below: {B}] (B) at (3,0);
\coordinate [label=above: {C}] (C) at (0,3);
\draw (B) -- (A) --(C);
\draw[very thick, green] (B) -- +(150:2);
\node [left] (D) at (intersection of  B--+150:6  and A--C) {$D$};
\draw [red] (B) -- (D);
\end{tikzpicture}
\end{document}

enter image description here

  • I don't think you can do coordinate calculations with the coordinates in this way. When you use (intersection of B--+150:6 and A--C) the 150:6 is taken absolutely, not relative to the point B. – Mark Wibrow Sep 20 '15 at 16:28
  • Actually it appears that this isn't the case as if you add \draw (0:0) -- (150:6) -- (B); the lines clearly don't overlap. Inaccuracies in the calculations I suppose. – Mark Wibrow Sep 20 '15 at 16:31
  • I'll add an explanation of the problem in some minutes. – Gonzalo Medina Sep 20 '15 at 16:40
1

Your MWE has two wrong assumption:

  • First, as explained Gonzalo Medina in his answer that coordinate + 150:6 is measured from origin and not from coordinate B, so it need to be shifted right for value of B
  • Second, coordinate D determined by node doesn't lie on line A--C but in middle of the node, which is placed left this line, i.e. it not designate intersection.

Considering those, your code is easy to correct. Actualy, for correction are available more possibilities:

  1. instead node use coordinate with label for designate intersection:

\coordinate[label=left:$D$] at (intersection of B--[shift=(B)]150:6 and C--A);

  1. use node and for coordinate in drawing red line use D.west

  2. use node and for coordinate in drawing red line use D -| A

Any of them doesn't require tikz library intersection. In code below I consider the second possibility:

\documentclass[border=3mm,
               tikz,
               ]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\coordinate [label=below: {A}] (A) at (0,0);
\coordinate [label=below: {B}] (B) at (3,0);
\coordinate [label=above: {C}] (C) at (0,3);
\draw (B) -- (A) --(C);
\draw[very thick,green] (B) -- +(150:3);
\node [left] (D) at (intersection of B--[shift=(B)]150:6 and C--A) {$D$};
\draw [red] (B) -- (D.east);
\end{tikzpicture}
\end{document}

It gives the following result:

enter image description here

  • This is a clean and correct solution. I am not sure I have PGF installed on every machine where I use Latex, so I might change the code to this. – jlinkels Sep 21 '15 at 1:45
4

It is better to use the intersections library for such jobs.

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\coordinate [label=below: {A}] (A) at (0,0);
\coordinate [label=below: {B}] (B) at (3,0);
\coordinate [label=above: {C}] (C) at (0,3);
\draw (B) -- (A);
\draw[name path = vert] (A)--(C);
\draw[very thick, green] (B) -- +(150:2cm);

%For intersections
\path[name path = angled] (B) -- +(150:6cm);
\path [name intersections={of=vert and angled, by={a}}];

\node [left] (D) at (a) {$D$};
\draw [red] (B) -- (a);
\end{tikzpicture}
\end{document}

enter image description here

\draw [red] (B) -- (a); is used as the node D uses left and all nodes have inner sep, making the line wrong.

  • Thanks, this provides a workaround and assures I can continue my document as intended. The question remains what I am doing wrong. As far as I can see my intersection attempt is equal to the first example in the TikZ manual: Karl's Students. Karl intersects the orange line with a 30 degree line. – jlinkels Sep 20 '15 at 14:44
  • Thank you also for the fine tuning on the node's inner sep. I noticed that, but for simplicity I in the MWE I ignored it. Indeed it is better to find a coordinate from the intersection and place a node there. Nevertheless, the red line should have a 150 degree angle. – jlinkels Sep 20 '15 at 14:45
  • @jlinkels In my tikz manual Karl's students don't do that ;). This is not a workaround but the correct method which is accurate. You are not doing anything wrong (except not using the library) but things have changed. – user11232 Sep 20 '15 at 14:51
  • Well, the example looks surprisingly a lot like an intersection not from the intersection library: \draw[orange,very thick] (1,0) -- (intersection of 1,0--1,1 and 0,0--30:1cm); But it seems for Karl it works because he originates the line from the origin. – jlinkels Sep 21 '15 at 1:23
  • Hmmm, I see that I am using pgfmanual 2.0 and not 3.0. In 3.0 it is different. Karl doesn't use this syntax anymore. And the intersections library is available in 3.0, not in 2.0. – jlinkels Sep 21 '15 at 1:38
2

The answer by Harish Kumar provides the accurate and recommended method to achieve the right point, namely, using the intersections library, but doesn't explain the problem with the original code. Here's an explanation.

The problem here is that using

\node [left] (D) at (intersection of  B--+150:6  and A--C) {$D$};

you are finding the intersection of the line A--C with the line B-K, where K is the point situated 6 units away at an angle of 150 from the origin, not from B.

If you want to introduce a shift from B you can use

B--[shift={(150:6)}]B

This improves the situation; however, due to inaccuracies in the calculations still doesn't get the right point:

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\coordinate [label=below: {A}] (A) at (0,0);
\coordinate [label=below: {B}] (B) at (3,0);
\coordinate [label=above: {C}] (C) at (0,3);
\draw (B) -- (A) --(C);
\draw[very thick, green] (B) -- +(150:2);
\node (K) at (150:6) {K}; 
\node [left] (D) at (intersection of  B--[shift={(150:6)}]B and A--C) {$D$};
\draw [red] (B) -- (D);
\end{tikzpicture}
\end{document}

enter image description here

I guess those inaccuracies are the reason why this approach isn't well documented in the PGF manual.

The most proper approach here is to use the intersections library:

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\coordinate [label=below: {A}] (A) at (0,0);
\coordinate [label=below: {B}] (B) at (3,0);
\coordinate [label=above: {C}] (C) at (0,3);
\draw (B) -- (A);
\draw[name path = vert] (A)--(C);
\draw[very thick, green] (B) -- +(150:2cm);

\path[name path = angled] (B) -- +(150:6cm);
\draw[red,name intersections={of=vert and angled, by={a}}]
  node [left] (D) at (a) {$D$}
  (B) -- (a);
\end{tikzpicture}
\end{document}

enter image description here

  • If you instead of \node [left] (D) at (intersection of B--[shift={(150:6)}]B and A--C) {$D$}; use \node [left] (D) at (intersection of B--[shift=(B)]150:6 and C--A) {$D$}; you will obtain accurate result. – Zarko Sep 20 '15 at 22:27
  • @Zarko I get the same inexcat result as the one in the first image of my answer. Weird. What version of PGF/TikZ do you use? – Gonzalo Medina Sep 20 '15 at 23:02
  • I use recent TikZ: 3.0.1. End result is really accurate. – Zarko Sep 20 '15 at 23:07
  • @Zarko With this \documentclass[11pt]{article} \usepackage{amsmath} \usepackage{tikz} \begin{document} \begin{tikzpicture} \coordinate [label=below: {A}] (A) at (0,0); \coordinate [label=below: {B}] (B) at (3,0); \coordinate [label=above: {C}] (C) at (0,3); \draw (B) -- (A) --(C); \draw[very thick, green] (B) -- +(150:2); \node (K) at (150:6) {K}; \node [left] (D) at (intersection of B--[shift=(B)]150:6 and C--A) {$D$}; \draw [red] (B) -- (D); \end{tikzpicture} \end{document} I get i.imgur.com/ZBl5MPp.png which still shows the problem. Do you get something different? – Gonzalo Medina Sep 20 '15 at 23:13
  • now I figured out, why I have accurate result and you not: I draw red line slightly different: \draw [red] (B) -- (D -| A); With (D -| A) this I eliminate the influence of node size on D coordinate, it is not in the middle of D node anymore, but exactly where it should be -- on intersections of lines.. – Zarko Sep 20 '15 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.