How to draw 90 degree angles in intersection points using tikzpicture environment

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\pgfplotsset{soldot/.style={color=black,only marks,mark=*}}
\pgfplotsset{holdot/.style={color=red,fill=white,very thick,only marks,mark=*}}

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[
legend pos=outer north east,
axis lines = center,
axis equal,
xticklabel style = {font=\tiny},
yticklabel style = {font=\tiny},
xlabel = $x$,
ylabel = $y$,
clip=false,
legend style={cells={align=left}},
legend cell align={left}
]
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}


I would like to draw a 90 degree angles in the two interception points:

The functions are y^3 = x and x^2/(16/3)+y^2/(16)=1.

Thanks!

EDIT. Thanks to Ruixi's useful comment for find a mistake in the function y^3=x!

• That's an interesting question, +1. Will look at it when I'm back in our burrow unless someone else answered it in between.
– user121799
Oct 27 '18 at 2:18
• @marmot the others are not? Hahahaha. Ok, enjoy. Oct 27 '18 at 2:33
• Maybe this awesome Skillmon's answer help? Oct 27 '18 at 2:52
• I don’t think the first function is y^3 = x Oct 27 '18 at 2:58
• @RuixiZhang I agree with you. manooooh, do you want your function, which is y=-3*x/|x|^{2/3}, or y^3=x?
– user121799
Oct 27 '18 at 3:18

Here is a proposal of 2 macros that draw the right angles.

They have 3 mandatory arguments: the point of intersection and the names of the two paths. The optional argument is used to transmit tikz options. I would have liked to group them into a single macro, but I didn't succeed.

They use the intersections and calc libraries of tikz.

% require \usetikzlibrary{intersections,calc}
\newcommand{\rightangleA}[4][]{
\begin{scope}
\path[name path=#2#3#4](#2) circle(6pt);
\path[name intersections={of=#2#3#4 and #3,name=i1}];
\path[name intersections={of=#2#3#4 and #4,name=i2}];
\draw[#1] (i1-1)to($(i1-1)+(i2-1)-(#2)$)to(i2-1)to(#2);
\end{scope}
}
\newcommand{\rightangleB}[4][]{
\begin{scope}
\path[name path=#2#3#4](#2) circle(6pt);
\path[name intersections={of=#2#3#4 and #3,name=i1}];
\path[name intersections={of=#2#3#4 and #4,name=i2}];
\draw[#1] (i1-2)to($(i1-2)+(i2-1)-(#2)$)to(i2-1)to(#2);
\end{scope}
}


\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{tikz,pgfplots}
\usetikzlibrary{intersections,calc}
\pgfplotsset{compat=1.8}
\pgfplotsset{soldot/.style={color=black,only marks,mark=*}}
\pgfplotsset{holdot/.style={color=red,fill=white,very thick,only marks,mark=*}}

\newcommand{\rightangleA}[4][]{
\begin{scope}
\path[name path=#2#3#4](#2) circle(6pt);
\path[name intersections={of=#2#3#4 and #3,name=i1}];
\path[name intersections={of=#2#3#4 and #4,name=i2}];
\draw[#1] (i1-1)to($(i1-1)+(i2-1)-(#2)$)to(i2-1)to(#2);
\end{scope}
}
\newcommand{\rightangleB}[4][]{
\begin{scope}
\path[name path=#2#3#4](#2) circle(6pt);
\path[name intersections={of=#2#3#4 and #3,name=i1}];
\path[name intersections={of=#2#3#4 and #4,name=i2}];
\draw[#1] (i1-2)to($(i1-2)+(i2-1)-(#2)$)to(i2-1)to(#2);
\end{scope}
}

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[
legend pos=outer north east,
axis lines = center,
axis equal,
xticklabel style = {font=\tiny},
yticklabel style = {font=\tiny},
xlabel = $x$,
ylabel = $y$,
clip=false,
legend style={cells={align=left}},
legend cell align={left}
]
\addplot[thick,samples=80,name path=A] {-x/abs(x)^(2/3)};    % From https://tex.stackexchange.com/a/144463/152550
\addplot[thick,samples=80,name path=B] ({sqrt(16/3)*cos((x) r)}, {sqrt(16)*sin((x) r)});
\path[name intersections={of=A and B,by={c,d}}];% intersections of paths
\rightangleA{c}{A}{B}
\rightangleB[blue]{c}{A}{B}
\rightangleA[fill=red,thick]{d}{A}{B}
%\rightangleB{d}{A}{B}
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

• I think that this is by far the best answer here. Simple and working.
– user121799
Oct 27 '18 at 19:28
• @marmot Thank you very much, I would like to improve it in order to make a single macro (and not several) that would allow you to choose the cadran where the right angle appears. Is that a good idea? If so, do you have any idea how to do that? Oct 27 '18 at 19:38
• But you already have nice single macros, Do you want to put everything into a single path command? Even if this is possible, I think it will be less clean than what you already have because you need to first draw the auxiliary path (the circle, which is a great trick BTW!), and then compute the intersections. Personally I do not see a real advantage of doing this.
– user121799
Oct 27 '18 at 22:54

Another math exercise for me! Here you go:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\pgfplotsset{soldot/.style={color=black,only marks,mark=*}}
\pgfplotsset{holdot/.style={color=red,fill=white,very thick,only marks,mark=*}}

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[
legend pos=outer north east,
axis lines = center,
axis equal,
xticklabel style = {font=\tiny},
yticklabel style = {font=\tiny},
xlabel = $x$,
ylabel = $y$,
clip=false,
legend style={cells={align=left}},
legend cell align={left}
]
\pgfmathsetmacro\intersectionx{-1.3157310986}
\pgfmathsetmacro\intersectiony{3.2873325096}
\pgfmathsetmacro\intersectionangle{%
atan((-2*\intersectionx/(16/3))/(2*\intersectiony/16))
}
\pgfmathsetmacro\cornersidelength{0.3}
\filldraw (axis cs:\intersectionx,\intersectiony) circle (1pt);
\draw[thick]
(axis cs:{\intersectionx
-\cornersidelength*sin(\intersectionangle)},
{\intersectiony
+\cornersidelength*cos(\intersectionangle)}) --
(axis cs:{\intersectionx
-\cornersidelength*sin(\intersectionangle)
+\cornersidelength*cos(\intersectionangle)},
{\intersectiony
+\cornersidelength*cos(\intersectionangle)
+\cornersidelength*sin(\intersectionangle)}) --
(axis cs:{\intersectionx
+\cornersidelength*cos(\intersectionangle)},
{\intersectiony
+\cornersidelength*sin(\intersectionangle)});
\filldraw (axis cs:-\intersectionx,-\intersectiony) circle (1pt);
\draw[thick]
(axis cs:{-\intersectionx
+\cornersidelength*cos(\intersectionangle)},
{-\intersectiony
+\cornersidelength*sin(\intersectionangle)}) --
(axis cs:{-\intersectionx
+\cornersidelength*cos(\intersectionangle)
+\cornersidelength*sin(\intersectionangle)},
{-\intersectiony
+\cornersidelength*sin(\intersectionangle)
-\cornersidelength*cos(\intersectionangle)}) --
(axis cs:{-\intersectionx
+\cornersidelength*sin(\intersectionangle)},
{-\intersectiony
-\cornersidelength*cos(\intersectionangle)});
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}


So what is the math magic behind the drawing? Of course, you would need to figure out the coordinates of the intersections by yourself, which is a drawback of the above solution. Here are the derivations:

• Am I in problems if you have to change the function to y^3=x? I plot it and there's is no nice display... Oct 27 '18 at 4:16
• Awesome trick and the code is easy to modify! I have two more functions so I added new macros changing its names, but I cannot deal with intersectionangle (now intersectionanglee). How did you define that? Now my new ellipse becomes x^2+y^2/3=1. I tried with \pgfmathsetmacro\intersectionanglee{% atan((-2*\intersectionxx)/(2*\intersectionyy/3)) } but it is not correct. Please see imgur.com/a/Xq0mRiZ. Oct 27 '18 at 5:09
• @manooooh Derivations added. Oct 27 '18 at 14:52

There are some weird effects when one computes intersections of some paths defined inside an axis outside the axis, so I'm stuck with an ugly solution in which the factors 1.1 and 1.13 are hard coded. If it were not for these effects, one could have a much nicer solution which does not rely on any computations of the user...

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc,intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend pos=outer north east,
axis lines = center,
axis equal,
xticklabel style = {font=\tiny},
yticklabel style = {font=\tiny},
xlabel = $x$,
ylabel = $y$,
clip=false,
legend style={cells={align=left}},
legend cell align={left}
]
\addplot[thick,samples=80,name path=A] {(-3)*x/abs(x)^(2/3)};    % From https://tex.stackexchange.com/a/144463/152550
\addplot[thick,samples=80,name path=B] ({sqrt(16/3)*cos((x) r)}, {sqrt(16)*sin((x) r)});
\path[name intersections={of=A and B}] (0,0) coordinate (O) (1,0)
coordinate(X);
\end{axis}
\path
let \p1=($(X)-(O)$),\p2=($(intersection-1)-(O)$),\n1={\x2/\x1},
\n2={-1/(pow(abs(\n1),2/3))} in
(intersection-1) -- + (-0.3,{-0.3*\n2*1cm/1pt}) coordinate(aux1)
(intersection-1) -- + ({-0.3*\n2*1cm/1pt},0.3) coordinate(aux2)
(intersection-1) -- + ({-0.3cm-0.3*\n2*1cm/1pt},{0.3cm-0.3*\n2*1cm/1pt}) coordinate(aux3);
\path[draw=red] (aux3) -- ($(aux3)+1.13*($(aux2)-(aux3)$)$);
\path[draw=red] (aux3) -- ($(aux3)+1.1*($(aux1)-(aux3)$)$);
\path
let \p1=($(X)-(O)$),\p2=($(intersection-2)-(O)$),\n1={\x2/\x1},
\n2={-1/(pow(abs(\n1),2/3))} in
(intersection-2) -- + (0.3,{0.3*\n2*1cm/1pt}) coordinate(aux1)
(intersection-2) -- + ({0.3*\n2*1cm/1pt},-0.3) coordinate(aux2)
(intersection-2) -- + ({0.3cm+0.3*\n2*1cm/1pt},{-0.3cm+0.3*\n2*1cm/1pt}) coordinate(aux3);
\path[draw=red] (aux3) -- ($(aux3)+1.13*($(aux2)-(aux3)$)$);
\path[draw=red] (aux3) -- ($(aux3)+1.1*($(aux1)-(aux3)$)$);
\end{tikzpicture}
\end{document}


• I just noted that there seems something really weird going on with the pgfplotslibrary fillbetween.
– user121799
Oct 27 '18 at 3:55
• Am I in problems if you have to change the function to y^3=x? I plot it and there's is no nice display... Oct 27 '18 at 4:16
• @manooooh According to what I find here, we are all in trouble. ;-) More seriously, at this point Ruixi's nice answer is clearly superior. (On the other hand, if it were not for these unexpected problems, there would be a nice simple trick that would avoid the necessity to compute things by hand. However, I have no clue how to fix these problems.) In order to plot y=x^{1/3}, you only need to drop the factor 3, i.e. plot {-x/abs(x)^(2/3)};.
– user121799
Oct 27 '18 at 4:22