How to find a normal vector on a surface from a point out of the surface? For example, if we consider a shape in 2D like a circle at center $(x_0,y_0)$ with radius R so the equation would be:
$$f(x,y)=(x-x_0)^2+(y-y_0)^2-R^2$$
now the normal on the surface is
$${\bf \hat n}=\frac{\nabla f}{\|\nabla f\|}=\frac{(x-x_0,y-y_0)}{\sqrt{(x-x_0)^2+(y-y_0)^2}}$$
In this problem, I don't know the point on the circle to compute the normal. I have a point outside of the circle and I would like to find the normal vector from that point to the circle.
One idea is consider the point on the surface as a parametric form
$$x=x_0+R\cos t,\\
y=y_0+R\sin t$$
then consider the normal vector as a function of $t$ and then find $t$. In this way, I have to solve fourth order equation, which makes my algorithm very expensive. I was wondering if there is an easier method which doesn't need to solve an equation.
 A: Given a curve a $c$ and with normal $n_0$ at $P_0$ and $P$ outside, the condition is given by 
$$P_0\subseteq \{(P+\vec n_o\cdot t) \cap c\}$$
For the circle it suffices that the line is through the center of the circle.
In general, for a generic curve $c$, the problem is hard to solve. 
A: In 2D, you can calculate curve normals using the curve tangent, by rotating the tangent 90 degrees.
For example, if you have a curve 
$$\vec{v}(t) = \bigl( x(t) ,\, y(t) \bigr)$$
then $\vec{r}(t)$ is perpendicular to $\vec{v}(t)$:
$$\vec{r}(t) = \left( -\frac{\partial\,y(t)}{\partial\,t}, \frac{\partial\,x(t)}{\partial\,t} \right)$$

An example. Let's say you have a circle of radius $R$ centered at $(x_0 , y_0)$:
$$\vec{v}(t) = \begin{cases}
x(t) = x_0 + R \cos t \\
y(t) = y_0 + R \sin t \end{cases}$$
Then,
$$\vec{r}(t) = \begin{cases}
\displaystyle -\frac{\partial\,y(t)}{\partial\,t} = -R \sin t \\
\displaystyle \frac{\partial\,x(t)}{\partial\,t} = R \cos t \end{cases}$$
If you want the unit normal $\hat{r}(t)$, you need to normalize the result:
$$\hat{r}(t) = \frac{\vec{r}(t)}{\lVert\vec{r}(t)\rVert} = \frac{\vec{r}(t)}{\sqrt{\vec{r}(t) \cdot \vec{r}(t)}} = \left ( -\sin t ,\, \cos t \right )$$
Let's say you have a point $\vec{p} = ( x , y )$, and you want to find the point $\vec{v}(t)$ on the curve where a line perpendicular to the curve passes through the point $\vec{p}$. In other words,
$$\vec{p} = \vec{v}(t) + L \vec{r}(t)$$
This is actually a system of two equations,
$$\bbox[#fffff7, 0.5em]{\begin{cases}
\displaystyle x = x(t) - L \frac{\partial y(t)}{\partial t} \\
\displaystyle y = y(t) + L \frac{\partial x(t)}{\partial t}
\end{cases}}$$
that you need to solve for $t$, and optionally for $L$. $L$ is the distance from the curve (in units of the length of the perpendicular vector $\vec{r}(t)$).
For the above curve, we have
$$\begin{cases}
x = x_0 + R \cos t - L R \sin t \\
y = y_0 + R \sin t + L R \cos t \\
\end{cases}$$
There are two solutions,
$$L = \pm \frac{\sqrt{(x - x_0)^2 + (y - y_0)^2 - R^2}}{R}$$
$$t = \operatorname{atan2}\left(
\frac{(y - y_0) R \mp (x - x_0)\sqrt{(x - x_0)^2 + (y - y_0)^2 - R^2}}{(x - x_0)^2 + (y - y_0)^2} ,\;\;
\frac{(x - x_0) R \pm (y - y_0)\sqrt{(x - x_0)^2 + (y - y_0)^2 - R^2}}{(x - x_0)^2 + (y - y_0)^2} \right)$$
where you choose either all upper signs, or all lower signs. $\operatorname{atan2}(\delta_y ,\, \delta_x)$ is essentially the same as $\arctan(\delta_y / \delta_x)$, except that $\operatorname{atan2}$ yields the correct quadrant also.

If we have a parametrized surface in 3D, for example
$$\vec{s}(u, v) = \bigl( x(u,v) ,\, y(u,v) ,\, z(u,v) \bigr)$$
then we have two tangent vectors:
$$\begin{aligned}
\vec{s}_u(u, v) &= \left( \frac{\partial x(u,v)}{\partial u} ,\, \frac{\partial y(u,v)}{\partial u} ,\, \frac{\partial z(u,v)}{\partial u} \right) \\
\vec{s}_v(u, v) &= \left( \frac{\partial x(u,v)}{\partial v} ,\, \frac{\partial y(u,v)}{\partial v} ,\, \frac{\partial z(u,v)}{\partial v} \right) \end{aligned}$$
and the normal to the surface at $\vec{s}(u, v)$ is perpendicular to both tangents, and easy to obtain via cross product:
$$\vec{r}(u, v) = \vec{s}_u(u, v) \times \vec{s}_v(u, v)$$
ie.
$$\vec{r}(u, v) = \left [ \begin{array}{c} \left(\frac{\partial y(u,v)}{\partial u}\right)\left(\frac{\partial z(u,v)}{\partial v}\right) - \left(\frac{\partial z(u,v)}{\partial u}\right)\left(\frac{\partial y(u,v)}{\partial v}\right) \\
\left(\frac{\partial z(u,v)}{\partial u}\right)\left(\frac{\partial x(u,v)}{\partial v}\right) - \left(\frac{\partial x(u,v)}{\partial u}\right)\left(\frac{\partial z(u,v)}{\partial v}\right) \\
\left(\frac{\partial x(u,v)}{\partial u}\right)\left(\frac{\partial y(u,v)}{\partial v}\right) - \left(\frac{\partial y(u,v)}{\partial u}\right)\left(\frac{\partial x(u,v)}{\partial v}\right) \\
\end{array} \right ]$$
