cgal/Packages/Distance_2/doc_tex/kernel/Distance_2/main.tex

73 lines
2.6 KiB
TeX

\cleardoublepage
\chapter{Squared Distances}
There is a family of functions called \ccStyle{CGAL_squared_distance} that
compute the square of the Euclidean distance between two geometric objects.
The squared distance between two two-dimensional points \ccStyle{p1} and
\ccStyle{p2} is defined as $d_{x}^{2} + d_{y}^{2}$, where $d_{x}$
\ccTexHtml{$\equiv$}{==}
\ccStyle{p2.x()-p1.x()} and $d_{y}$\ccTexHtml{$\equiv$}{==} \ccStyle{p2.y()-p1.y()}.
For arbitrary two-dimensional geometric objects \ccStyle{obj1} and
\ccStyle{obj2} the squared distance is defined as the minimal
\ccStyle{CGAL_squared_distance(p1, p2)}, where \ccStyle{p1} is a point of
\ccStyle{obj1} and \ccStyle{p2} is a point of \ccStyle{obj2}.
Note that for objects like triangles and polygons that have an inside (a
bounded region), this inside is part of the object.
So, the squared distance from a point inside a triangle to that triangle is
zero, not the squared distance to the closest edge of the triangle.
The general format of the functions is:
\ccUnchecked{
\ccGlobalFunctionTemplate{R}
{R::FT CGAL_squared_distance(Type1<R> obj1, Type2<R> obj2);}
}
\noindent
where the types \ccStyle{Type1} and \ccStyle{Type2} can be any of the
following:
\begin{itemize}
\item \ccStyle{CGAL_Point_2}
\item \ccStyle{CGAL_Line_2}
\item \ccStyle{CGAL_Ray_2}
\item \ccStyle{CGAL_Segment_2}
\item \ccStyle{CGAL_Triangle_2}
%\item \ccStyle{CGAL_Iso_Rectangle_2}
%\item \ccStyle{CGAL_Polygon_2}
\end{itemize}
Those routines are defined in the header file
\ccStyle{CGAL/squared_distance.h}.
\subsection{Why the square?}
There are routines that compute the square of the Euclidean distance, but no
routines that compute the distance itself. Why?
First of all, the two values can be derived from each other quite easily (by
taking the square root or taking the square). So, supplying only the one and
not the other is only a minor inconvenience for the user.
Second, often either value can be used. This is for example the case when
(squared) distances are compared.
Third, the library wants to stimulate the use of the squared distance instead
of the distance. The squared distance can be computed in more cases and the
computation is cheaper.
We do this by not providing the perhaps more natural routine,
The problem of a distance routine is that it needs the \ccStyle{sqrt}
operation.
This has two drawbacks.
\begin{itemize}
\item
The \ccStyle{sqrt} operation can be costly. Even if it is not very costly for
a specific number type and platform, avoiding it is always cheaper.
\item
There are number types on which no \ccStyle{sqrt} operation is defined,
especially integer types and rationals.
\end{itemize}