Sphere Tangent to Plane

I'm trying to draw Sphere Tangent to Plane to looks like that : i'm using :

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
[
scale=1,
point/.style = {draw, circle, fill=black, inner sep=0.7pt},
]

\node (C) at (0,0) [point]{};
\draw (C) circle (\rad);
\node (P)  at +(160:\rad)  [point]{};

% Using the calc library
\draw (P) -- ($(P)!2!-90:(C)$);
\draw (P) -- ($(P)!2!90:(C)$);

% using /tikz/turn
%\draw[->,thick, color=blue] (C) -- (P) -- ([turn]-90:2cm);
% this is the command that I don't understand
%\draw[->,thick, color=red] (P) -- ([turn]90:1cm);

\end{tikzpicture}
\end{document}
• I added a longer explanation on turn at the end of my updated answer. – Gonzalo Medina Aug 14 '15 at 23:18

You can do something simple such as The code:

\documentclass[tikz,border=3pt]{standalone}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[
point/.style = {draw, circle, fill=black, inner sep=0.7pt},
]
\coordinate (O) at (0,0);
\coordinate (N) at (0,\rad);

\draw[dashed]
\draw
\filldraw[fill=gray!10,opacity=0.2]
(-4,1) -- (3,1) -- (3,-1) -- (-4,-1) -- cycle;
\node at (2,0.6) {$P$};
\end{scope}
\draw[dashed]
(N) node[above] {$A$} -- (O) node[below] {$O$};
\node[point] at (N) {};
\end{tikzpicture}

\end{document}

Or. you can go "really" 3D:

\documentclass{article}
\usepackage{tikz}

\newcommand\pgfmathsinandcos{%
\pgfmathsetmacro#1{sin(#3)}%
\pgfmathsetmacro#2{cos(#3)}%
}
\newcommand\LongitudePlane[current plane]{%
\pgfmathsinandcos\sinEl\cosEl{#2} % elevation
\pgfmathsinandcos\sint\cost{#3} % azimuth
\tikzset{#1/.style={cm={\cost,\sint*\sinEl,0,\cosEl,(0,0)}}}
}
\newcommand\LatitudePlane[current plane]{%
\pgfmathsinandcos\sinEl\cosEl{#2} % elevation
\pgfmathsinandcos\sint\cost{#3} % latitude
\pgfmathsetmacro\yshift{\cosEl*\sint}
\tikzset{#1/.style={cm={\cost,0,0,\cost*\sinEl,(0,\yshift)}}} %
}
\newcommand\DrawLongitudeCircle{
\LongitudePlane{\angEl}{#2}
\tikzset{current plane/.prefix style={scale=#1}}
% angle of "visibility"
\pgfmathsetmacro\angVis{atan(sin(#2)*cos(\angEl)/sin(\angEl))} %
\draw[current plane] (\angVis:1) arc (\angVis:\angVis+180:1);
\draw[current plane,dashed] (\angVis-180:1) arc (\angVis-180:\angVis:1);
}
\newcommand\DrawLatitudeCircle{
\LatitudePlane{\angEl}{#2}
\tikzset{current plane/.prefix style={scale=#1}}
\pgfmathsetmacro\sinVis{sin(#2)/cos(#2)*sin(\angEl)/cos(\angEl)}
% angle of "visibility"
\pgfmathsetmacro\angVis{asin(min(1,max(\sinVis,-1)))}
\draw[current plane] (\angVis:1) arc (\angVis:-\angVis-180:1);
\draw[current plane,dashed] (180-\angVis:1) arc (180-\angVis:\angVis:1);
}

%% document-wide tikz options and styles

\tikzset{%
>=latex, % option for nice arrows
inner sep=0pt,%
outer sep=2pt,%
mark coordinate/.style={inner sep=0pt,outer sep=0pt,minimum size=3pt,
fill=black,circle}%
}

\begin{document}

\begin{tikzpicture}

%% some definitions
\def\R{2.5} % sphere radius
\def\angEl{35} % elevation angle
\def\angAz{-105} % azimuth angle
\def\angPhi{-40} % longitude of point P
\def\angBeta{19} % latitude of point P

%% working planes

\pgfmathsetmacro\H{\R*cos(\angEl)} % distance to north pole
\tikzset{xyplane/.style={
cm={cos(\angAz),sin(\angAz)*sin(\angEl),-sin(\angAz),cos(\angAz)*sin(\angEl),(0,-\H)}
}
}
\LatitudePlane[equator]{\angEl}{0}

%% characteristic points
\coordinate (O) at (0,0);
\coordinate (N) at (0,\H);

%% draw xy shifted plane and sphere
\fill[ball color=white] (0,0) circle (\R);
\filldraw[xyplane,shift={(N)},fill=gray!10,opacity=0.2]
(-1.4*\R,-1.7*\R) rectangle (2.2*\R,2.2*\R);
\draw (0,0) circle [radius=\R];
\coordinate[mark coordinate] (N) at (0,\H);

%% draw equator
\DrawLatitudeCircle[\R]{0}

% lines and labels
\draw[dashed]
(N) node[above] {$A$} -- (O) node[below] {$O$};
\node at (2*\R,1.2*\R) {$P$};
\end{tikzpicture}

\end{document}

The result: In the last code I used a variation of Tomas M. Trzeciak's example on Stereographic and cylindrical map projections

Explanation on turn

Regarding the question about the turn key, a little example can be illustrative:

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\draw (-1,-1) grid (5,2);
\draw[line width=2pt,red]
(0,0) -- (1,0) -- ([turn]45:1cm) -- ([turn]-45:1cm) -- ([turn]-45:1cm) -- ++(1,0);
\end{tikzpicture}
\end{document} When you use (<coord1>) -- (<coord2>) -- ([turn]<angle>:<distance>) you are specifying a point that lies <distance> away from (<coord2>) in the direction of the last tangent entering the last point, but with a rotation of <angle> (further details on page 141 of the manual for version 3.0 of PGF).

\draw (C) -- (P) -- ([turn]-90:2cm)

Means join (C) to (P) with a straight line; since the tangent at (P) to this line is the line itself, ([turn]-90:2cm) performs a rotation of -90 degrees and locates the point 2cm away from (P) in this direction. However, when you say

\draw (P) -- ([turn]90:1cm)

this is the same as

\draw (0,0) (P) -- ([turn]90:1cm)

and things work (well, if you were ineterested just in the final red segment) just because turn takes the origin (0,0) as the default first coordinate. Had you centered your circle around a different point, you would've got the wrong result as can be seen in the image in the middle in the figure below. To prevent this is better to say

\draw (C) -- (P) -- ([turn]90:1cm)

if you want both segments drawn or

\draw (C) (P) -- ([turn]90:1cm)

to draw just the last part. In any case, when using turn you previously have to specify two coordinates in the path so the proper angle is calculated.

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}
\coordinate (C) at (0,0);
\coordinate (P)  at +(160:\rad);
\draw (C) circle (\rad);
% Using the calc library
\draw (P) -- ($(P)!2!-90:(C)$);
\draw (P) -- ($(P)!2!90:(C)$);
% using /tikz/turn
\draw[->,thick, color=blue] (C) -- (P) -- ([turn]-90:2cm);
% this is the command that I don't understand
\draw[->,thick, color=red] (P) -- ([turn]90:1cm);
\node at (0,-3cm)
{It works by fortune};

\begin{scope}[xshift=5cm]
\coordinate (C) at (1,0);
\coordinate (P)  at +(160:\rad);
\draw (C) circle (\rad);
% Using the calc library
\draw (P) -- ($(P)!2!-90:(C)$);
\draw (P) -- ($(P)!2!90:(C)$);
% using /tikz/turn
\draw[->,thick, color=blue] (C) -- (P) -- ([turn]-90:2cm);
% this is the command that I don't understand
\draw[->,thick, color=red] (P) -- ([turn]90:1cm);
\node at (0,-3cm)
{It doesn't work};
\end{scope}

\begin{scope}[xshift=11cm]
\coordinate (C) at (1,0);
\coordinate (P)  at +(160:\rad);
\draw (C) circle (\rad);
% Using the calc library
\draw (P) -- ($(P)!2!-90:(C)$);
\draw (P) -- ($(P)!2!90:(C)$);
% using /tikz/turn
\draw[->,thick, color=blue] (C) -- (P) -- ([turn]-90:2cm);
% this is the command that I don't understand
\draw[->,thick, color=red] (C) -- (P) -- ([turn]90:1cm);
\node at (0,-3cm)
{The proper way};
\end{scope}
\end{tikzpicture}

\end{document} \documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}
\coordinate (C) at (0,0);
\coordinate (P)  at +(160:\rad);
\draw (C) circle (\rad);
% Using the calc library
\draw (P) -- ($(P)!2!-90:(C)$);
\draw (P) -- ($(P)!2!90:(C)$);
% using /tikz/turn
\draw[->,thick, color=blue] (C) -- (P) -- ([turn]-90:2cm);
% this is the command that I don't understand
\draw[->,thick, color=red] (P) -- ([turn]90:1cm);

\begin{scope}[xshift=5cm]
\coordinate (C) at (1,0);
\coordinate (P)  at +(160:\rad);
\draw (C) circle (\rad);
% Using the calc library
\draw (P) -- ($(P)!2!-90:(C)$);
\draw (P) -- ($(P)!2!90:(C)$);
% using /tikz/turn
\draw[->,thick, color=blue] (C) -- (P) -- ([turn]-90:2cm);
% this is the command that I don't understand
\draw[->,thick, color=red] (P) -- ([turn]90:1cm);
\end{scope}

\begin{scope}[xshift=11cm]
\coordinate (C) at (1,0);
\coordinate (P)  at +(160:\rad);
\draw (C) circle (\rad);
% Using the calc library
\draw (P) -- ($(P)!2!-90:(C)$);
\draw (P) -- ($(P)!2!90:(C)$);
% using /tikz/turn
\draw[->,thick, color=blue] (C) -- (P) -- ([turn]-90:2cm);
% this is the command that I don't understand
\draw[->,thick, color=red] (C) -- (P) -- ([turn]90:1cm);
\end{scope}
\end{tikzpicture}

\end{document}
• @Educ ask also for meaning of \draw[->,thick, color=red] (P) -- ([turn]90:1cm);, typically with turn we suppose at least two coordinates ? – Salim Bou Aug 14 '15 at 17:58
• @salimbou I hadn't seen the question buried in the code. I'll add an explanation in short. – Gonzalo Medina Aug 14 '15 at 18:01
• @salimbou No, thanks to you. Explanation added. – Gonzalo Medina Aug 14 '15 at 18:16

Something like this?

\documentclass{report}

\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}[scale=2]
\begin{axis}[axis lines=none]
\addplot3[domain=0:360, y domain=0:180,LightGrey] ({sin(y)*cos(x)},{sin(x)*sin(y)},{cos(y)});
\draw (axis cs:-1,-1,1) -- (axis cs:-1,1,1) -- (axis cs:1,1,1) -- (axis cs:1,-1,1)--cycle;
\end{axis}
\end{tikzpicture}
\end{document} 