# Labeling the intersection of two rays

I have the code for rectangle ABCD and two of its diagonals. I am trying to draw rays from adjacent vertices C and D, each of which is perpendicular to a diagonal, label the intersection of the two rays P, and draw a line segment through P to line segment CD that is perpendicular to CD.

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,intersections,quotes,backgrounds}

\begin{document}

\begin{tikzpicture}
\coordinate (A) at (-0.5,-0.75);
\coordinate (B) at (-0.5,0.5);
\coordinate (C) at (1,0.5);
\coordinate (D) at (1,-0.75);

\path[fill=yellow] (A) -- (B) -- (C) -- (D) -- cycle;

\path (C) -- ($(C)!1.5cm!90:(A)$);
\path (D) -- ($(D)!1.5cm!-90:(B)$);

\draw[draw=red!50, line width=0.1pt, name path=ray1] (A) -- (C);
\draw[draw=red!50, line width=0.1pt, name path=ray2] (B) -- (D);

%\coordinate [name intersections={of=ray1 and vertical,by={P}}] ;

\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]below left:$A$}] at (A) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]above left:$B$}] at (B) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]above right:$C$}] at (C) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]below right:$D$}] at (D) {};
\end{tikzpicture}
\end{document}

• You need an \end{document} ;). – cfr Apr 30 '15 at 22:54

May be like this.

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,intersections,quotes,backgrounds}

\begin{document}

\begin{tikzpicture}
\coordinate (A) at (-0.5,-0.75);
\coordinate (B) at (-0.5,0.5);
\coordinate (C) at (1,0.5);
\coordinate (D) at (1,-0.75);

\path[fill=yellow] (A) -- (B) -- (C) -- (D) -- cycle;

\path[draw,name path=ray1] (C) -- ($(C)!1.5cm!90:(A)$);
\path[draw,name path=ray2] (D) -- ($(D)!1.5cm!-90:(B)$);

\draw[draw=red!50, line width=0.1pt] (A) -- (C);
\draw[draw=red!50, line width=0.1pt] (B) -- (D);

\coordinate [name intersections={of=ray1 and ray2,by={P}}] ;

\node[circle,fill,inner sep=1.5pt,outer sep=0pt,label={right:$P$}] at (P) {};
\draw (P) -- ($(D)!(P)!(C)$);

\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]below left:$A$}] at (A) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]above left:$B$}] at (B) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]above right:$C$}] at (C) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]below right:$D$}] at (D) {};
\end{tikzpicture}
\end{document}


• (+1) Interesting. Haven't ever used ($(D)!1.5cm!-90:(B)$) or ($(D)!(P)!(C)$). (Just looked them up.) – cfr Apr 30 '15 at 23:30
• Thanks. I know. That's what the TikZ manual said just now when I looked it up ;). – cfr Apr 30 '15 at 23:42
• @Harish Kumar I prefer your code because you keep most of my code. Doing so is particularly helpful because I can easily determine where I am misusing commands. – user143462 May 1 '15 at 1:19
• @Harish Kumar I see that one reason that I couldn't plot P correctly is that I had "vertical" instead of "ray2"! I also see that you replaced \path (C) -- ($(C)!1.5cm!90:(A)$); with \path[draw,name path=ray1] (C) -- ($(C)!1.5cm!90:(A)$); and that you replaced \path (D) -- ($(D)!1.5cm!-90:(B)$); with \path[draw,name path=ray2] (D) -- ($(D)!1.5cm!-90:(B)$); to get point P as the intersection of ray1 and ray2. This is more efficient code than what I was using. – user143462 May 1 '15 at 1:31
• @Harish Kumar By the way, I removed the draw option from the commands to draw ray1 and ray2 and added \draw (C) -- (P) -- (D); at the end of the code to simply have a triangle sharing its base with the right side of the rectangle. – user143462 May 1 '15 at 1:37

Something like this? Mine is different, I think, from Harish Kumar's so I will post even though he answered first. Note that with my method, the second intersection point is the one you want, so I named the first one Q just so the second would be P.

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{calc,intersections,quotes,backgrounds}

\begin{document}
\tikzset{
my circle/.style={outer sep=0pt, circle, fill, inner sep=1.5pt},
my ray/.style={draw=red!50, line width=.1pt}
}

\begin{tikzpicture}
\coordinate (A) at (-0.5,-0.75);
\coordinate (B) at (-0.5,0.5);
\coordinate (C) at (1,0.5);
\coordinate (D) at (1,-0.75);

\path[fill=yellow] (A) -- (B) -- (C) -- (D) -- cycle;

\pgfmathsetmacro\myresult{atan(1.5/1.25)}
\path [my ray, name path=ray1] (A) -- (C) -- +(-\myresult:1);
\path [my ray, name path=ray2] (B) -- (D) -- +(\myresult:1);

\path [name intersections={of=ray1 and ray2, by={Q,P}}] (P) node [right] {P} ;
\draw (P) -- (C |- P);

\node[my circle,label={[fill=white]below left:$A$}] at (A) {};
\node[my circle,label={[fill=white]above left:$B$}] at (B) {};
\node[my circle,label={[fill=white]above right:$C$}] at (C) {};
\node[my circle,label={[fill=white]below right:$D$}] at (D) {};
\end{tikzpicture}
\end{document}


• Good that you cleaned up the code. :-). I am lazy! – user11232 Apr 30 '15 at 23:38
• @cfr Your code gives me what I want, but I do not know what some of your commands, like \pgfmathsetmacro\myresult{atan(1.5/1.25)}, does. I really wanted to know what was wrong with my code. Harish Kumar found the mistake. – user143462 May 1 '15 at 1:41
• @user143462 It calculates tan-1 for 1.5/1.25. For a right-angled triangle, tan of the angle equals the ratio of the opposite side to the adjacent. If you have the ratio, tan-1 figures out the angle. So that just calculates the angle you need to draw the ray in. I just did it the way that made sense to me i.e. which occurred to me given my knowledge of TikZ. I guess it made sense to me to draw the rays as single paths. – cfr May 1 '15 at 1:45

For whom it interests, here is a MetaPost solution, to be processed with LuaLaTeX.

The intersection point P is computed thanks to the following instruction, making use of the handy intersectionpoint operator of MetaPost between two paths.

P = (C--A) rotatedaround (C,90) intersectionpoint (D--B) rotatedaround (D,-90);


The perpendicular to CD through P is simply obtained by joining P to the middle point of segment CD.

Q = .5[C,D]; … draw P--Q;


I have also added right angle markings, thanks to the anglebetween macro which is part of MetaPost's Metafun format.

\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
\mplibsetformat{metafun}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
u := 2cm; pair A, B, C, D, P, Q;
A = u*(-.5,-.75); B = u*(-.5,.5); C = u*(1,.5); D = u*(1,-.75); Q = .5[C,D];
path rectangle; rectangle = A--B--C--D--cycle;
P = (C--A) rotatedaround (C,90) intersectionpoint (D--B) rotatedaround (D,-90);
beginfig(1);
fill rectangle withcolor yellow;
pickup pencircle scaled .1bp;
drawoptions(withcolor red);
draw B--D; draw A--C;
draw C -- 1.5[C,P]; draw D -- 1.5[D,P];
drawoptions(withcolor black);
freelabeloffset := 5bp;
forsuffixes M = A, B, C, D, P:
drawdot M withpen pencircle scaled 3bp;
freelabel("$" & str M & "$", M, center rectangle);
endfor
draw P--Q;
anglemethod := 2; anglelength := 4bp;
draw anglebetween(C--A, C--P, "");
draw anglebetween(D--B, D--P, "");
draw anglebetween(Q--C, Q--P, "");
endfig;
\end{mplibcode}
\end{document}