3

I am trying to get TikZ to draw two lines, with arrowheads, on the Cartesian plane. I have drawn a square, with gray edges, and I want the tip of the arrowheads to be on an edge of the square. In particular, I want to include the six commands following the comment

"The following code is for placing arrowheads at the ends of the line segments."

TikZ is not compiling them. (This can be an illustrative example using the intersections package.)

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


\begin{document}



\begin{tikzpicture}[outer sep=0pt,p/.style={circle, fill,inner sep=1.5pt}]

\draw[draw=gray!30,latex-latex] (-3.75,0) +(-0.25cm,0) -- (3.75,0) -- +(0.25cm,0) node[below right] {$x$};

\clip (-3.75,-3.75) rectangle (3.75,3.75);

\draw[gray,dashed,line width=0.1pt] (-3.75,3.75) -- (3.75,3.75);
\draw[gray,dashed,line width=0.1pt] (-3.75,-3.75) -- (3.75,-3.75);
\draw[gray,dashed,line width=0.1pt] (-3.75,-3.75) -- (-3.75,3.75);
\draw[gray,dashed,line width=0.1pt] (3.75,-3.75) -- (3.75,3.75);

\draw[draw=blue!30,-latex] (0,0) -- (142:5);
\draw[draw=blue!30,-latex] (0,0) -- (-38:5);
\draw[draw=green!50,-latex] (0,0) -- (52:5);
\draw[draw=green!50,-latex] (0,0) -- (-128:5);

\coordinate[p,label={[fill=white]below right:$O$}] (O) at (0,0);

\coordinate (A) at (0:1);
\coordinate (B) at (52:1);
\path pic[draw, angle radius=5mm,"$\phi$",angle eccentricity=1.25] {angle = A--O--B};

\coordinate (a) at (180:1);
\coordinate (b) at (142:1);
\path pic[draw, angle radius=5mm,"$\theta$",angle eccentricity=1.25] {angle = b--O--a};

\coordinate (P) at (142:1);
\coordinate (Q) at (52:1);

\coordinate (R) at ($(O)!4mm! -45:(P)$);
\draw (R) -- ($(O)!(R)!(P)$);
\draw (R) -- ($(O)!(R)!(Q)$);

%The following code makes the right-angle mark and "colors" the inside of it white.
\begin{scope}[on background layer]
\draw[draw=gray!30,latex-latex] (0,3.75) +(0,0.25cm) node[above right] {$y$} -- (0,-3.75) -- +(0,-0.25cm);
\filldraw[fill=white] (O.center) -- ($(O)!(R)!(P)$) -- (R) -- ($(O)!(R)!(Q)$) -- cycle;
\end{scope}


%The following code is for placing arrowheads at the ends of the line segments.
%\path[name intersections={of=(-3.75,3.75) -- (3.75,3.75) and (0,0) -- (52:5), by=intersection-1}];
%\path[name intersections={of=(3.75,3.75) -- (3.75,-3.75) and (0,0) -- (-38:5), by=intersection-2}];
%\path[name intersections={of=(-3.75,-3.75) -- (3.75,-3.75) and (0,0) -- (-128:5), by=intersection-3}];
%\path[name intersections={of=(-3.75,3.75) -- (-3.75,-3.75) and (0,0) -- (142:5), by=intersection-4}];

%\draw[draw=green!50,latex-latex] (intersection-1) -- (intersection-3);
%\draw[draw=blue!30,latex-latex] (intersection-2) -- (intersection-4);

\end{tikzpicture}

\end{document}
user74973
  • 4,071

2 Answers2

3

Here's a Metapost effort for comparison. The idea is that you set the angle phi and everything else adjusts automatically.

enter image description here

prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);
u := 1cm;
% defime the paths we need and a value for phi..
path xx, yy, box, ray;
xx = (left--right) scaled 4u;
yy = xx rotated 90;
box = unitsquare shifted -(1/2,1/2) scaled 7u;
ray = origin -- right scaled 7u;
numeric phi;
phi = 57.3; 

% first draw axes and box
drawoptions(withcolor .7 white);
drawdblarrow xx;
drawdblarrow yy;
draw box dashed evenly;
drawoptions();

% now draw the angle marks so they are underneath the main lines
% angle marks assume 0 < phi < 90...
path angle_mark[]; 
angle_mark1 = (1u,0) {up} .. (1u,0) rotated phi;
angle_mark2 = (-u,0) {up} .. (-u,0) rotated (phi-90);
angle_mark3 = unitsquare scaled 3/8 u rotated phi;
draw angle_mark1;
draw angle_mark2;
unfill angle_mark3; 
draw angle_mark3;

% now the main lines
for i=0 upto 3:
  drawarrow ray rotated (phi+90i) cutafter box withcolor .42[if odd(i): blue else: red fi, white];
endfor

% finally the labels
label(btex $\phi$   etex, point 1/2 of angle_mark1 scaled 1.23);
label(btex $\theta$ etex, point 1/2 of angle_mark2 scaled 1.23);
label.rt (btex $x$ etex, point 1 of xx);
label.top(btex $y$ etex, point 1 of yy);
fill fullcircle scaled dotlabeldiam;
picture O;
O = thelabel(btex $O$ etex, (u/2,0) rotated (180+45+phi));
unfill bbox O; draw O;

endfig;
end.
Thruston
  • 42,268
1

Here is a shorter method.

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

\begin{document}

\begin{tikzpicture}[outer sep=0pt,p/.style={circle, fill,inner sep=1.5pt}]

\draw[draw=gray!30,latex-latex] (-3.75,0) +(-0.8cm,0) -- (3.75,0) -- +(0.25cm,0) node[below right] {$x$};

\coordinate[p,label={[fill=white]below right:$O$}] (O) at (0,0);

\draw[draw=blue!30,-latex] (0,0) -- (142:5)coordinate (aa); \draw[draw=blue!30,-latex] (0,0) -- (-38:4.5)coordinate (cc); \draw[draw=green!50,-latex] (0,0) -- (52:4.5)coordinate (dd); \draw[draw=green!50,-latex] (0,0) -- (-128:4.5)coordinate (bb);

\draw[gray,dashed,line width=0.1pt] (aa|-bb) rectangle (cc|-dd);

\coordinate[p,label={[fill=white]below right:$O$}] (O) at (0,0);

\coordinate (A) at (0:1); \coordinate (B) at (52:1); \path pic[draw, angle radius=5mm,"$\phi$",angle eccentricity=1.25] {angle = A--O--B};

\coordinate (a) at (180:1); \coordinate (b) at (142:1); \path pic[draw, angle radius=5mm,"$\theta$",angle eccentricity=1.25] {angle = b--O--a};

\coordinate (P) at (142:1); \coordinate (Q) at (52:1);

\coordinate (R) at ($(O)!4mm! -45:(P)$); \draw (R) -- ($(O)!(R)!(P)$); \draw (R) -- ($(O)!(R)!(Q)$);

%The following code makes the right-angle mark and "colors" the inside of it white. \begin{scope}[on background layer] \draw[draw=gray!30,latex-latex] (0,3.75) +(0,0.25cm) node[above right] {$y$} -- (0,-3.75) -- +(0,-0.25cm); \filldraw[fill=white] (O.center) -- ($(O)!(R)!(P)$) -- (R) -- ($(O)!(R)!(Q)$) -- cycle; \end{scope}

\end{tikzpicture}

\end{document}

enter image description here

No clipping, no drawing the rectangle piece by piece, no intersection business. Just define coordinates aa, bb etc, and then use

\draw[gray,dashed,line width=0.1pt] (aa|-bb) rectangle (cc|-dd);

to draw the rectangle. I have adjusted the length of those lines from 5 to 4.5 BTW.

Since I have some confusion still in my mind, you can draw other lines too like

\draw[olive,latex-latex] (aa) -- (bb);
\draw[olive,latex-latex] (aa) -- (dd);

enter image description here

Edit

To keep \clip and find intersections, you have to name the path when it is being drawn. Check the following code.

\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,quotes,backgrounds,intersections}
%% come back here

\begin{document}

\begin{tikzpicture}[outer sep=0pt,p/.style={circle, fill,inner sep=1.5pt}]

\draw[draw=gray!30,latex-latex] (-3.75,0) +(-0.25cm,0) -- (3.75,0) -- +(0.25cm,0) node[below right] {$x$};

\clip (-3.75,-3.75) rectangle (3.75,3.75);

\draw[gray,dashed,line width=0.1pt,name path =A] (-3.75,3.75) -- (3.75,3.75); \draw[gray,dashed,line width=0.1pt,name path = E] (-3.75,-3.75) -- (3.75,-3.75); \draw[gray,dashed,line width=0.1pt,name path =G] (-3.75,-3.75) -- (-3.75,3.75); \draw[gray,dashed,line width=0.1pt,name path=C] (3.75,-3.75) -- (3.75,3.75);

\draw[draw=blue!30,-latex,name path = H] (0,0) -- (142:5); \draw[draw=blue!30,-latex,name path = D] (0,0) -- (-38:5); \draw[draw=green!50,-latex,name path=B] (0,0) -- (52:5); \draw[draw=green!50,-latex,name path =F] (0,0) -- (-128:5);

\coordinate[p,label={[fill=white]below right:$O$}] (O) at (0,0);

\coordinate (A) at (0:1); \coordinate (B) at (52:1); \path pic[draw, angle radius=5mm,"$\phi$",angle eccentricity=1.25] {angle = A--O--B};

\coordinate (a) at (180:1); \coordinate (b) at (142:1); \path pic[draw, angle radius=5mm,"$\theta$",angle eccentricity=1.25] {angle = b--O--a};

\coordinate (P) at (142:1); \coordinate (Q) at (52:1);

\coordinate (R) at ($(O)!4mm! -45:(P)$); \draw (R) -- ($(O)!(R)!(P)$); \draw (R) -- ($(O)!(R)!(Q)$);

\begin{scope}[on background layer] \draw[draw=gray!30,latex-latex] (0,3.75) +(0,0.25cm) node[above right] {$y$} -- (0,-3.75) -- +(0,-0.25cm); \filldraw[fill=white] (O.center) -- ($(O)!(R)!(P)$) -- (R) -- ($(O)!(R)!(Q)$) -- cycle; \end{scope}

%The following code is for placing arrowheads at the ends of the line segments.
\path[name intersections={of= A and B, by=aa}];
\path[name intersections={of=C  and D, by=bb}];
\path[name intersections={of= E and F, by=cc}];
\path[name intersections={of= G and H, by=dd}];
\draw[draw=green!30,latex-latex] (O) -- (aa);
\draw[draw=blue!30,latex-latex] (O) -- (bb);
\draw[draw=green!50,latex-latex] (O) -- (cc);
\draw[draw=blue!30,latex-latex] (O) -- (dd);



\end{tikzpicture}

\end{document}

enter image description here

  • I like the code; I would like to refer to it for other situations. With your code, the lines are bounded by a rectangle, which is not a square. (The code \draw[gray,dashed,line width=0.1pt] (aa|-bb) rectangle (cc|-dd); instructs TikZ to plot a rectangle with a lower left vertex of aa|-bb - a coordinate at the point of intersection of a vertical line through aa and a horizontal line through bb - and an upper right vertex of cc|-dd - a coordinate at the point of intersection of a vertical line through cc and a horizontal line through dd. – user74973 Apr 30 '15 at 13:53
  • In this case, I would like to keep the lines in a square and not just any rectangle. Using the intersections package is quite convenient for this. I have the code - I just don't know why it is not getting compiled. – user74973 Apr 30 '15 at 13:57
  • May you fix the code? I put "%" in front of the last six commands so that the code would compile. What is wrong with the way I issued the commands? – user74973 Apr 30 '15 at 20:51
  • @user74973 See the last edit. –  May 01 '15 at 02:00