Rotating coordinates

This commit is contained in:
Antti H S Laaksonen 2017-02-18 20:45:58 +02:00
parent be2fc4eb38
commit 9cc199af20
1 changed files with 96 additions and 49 deletions

View File

@ -675,62 +675,109 @@ from the center point, using the Euclidean and Manhattan distances:
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
\subsubsection{Furthest points} \subsubsection{Rotating coordinates}
Some problems are easier to solve if the Some problems are easier to solve if the
Manhattan distance is used instead of the Euclidean distance. Manhattan distance is used instead of the Euclidean distance.
For example, consider a problem where we are given As an example, consider a problem where we are given
$n$ points $(x_1,y_1),(x_2,y_2),\ldots,(x_n,y_n)$ $n$ points in the two-dimensional plane
and our task is to calculate the maximum distance and our task is to calculate the maximum Manhattan
between any two points. distance between any two points.
This is a difficult problem if the Euclidean distance For example, consider the following set of points:
should be maximized,
but it is easy to maximize the
Manhattan distance,
because it is either
\[\max A - \min A \hspace{20px} \textrm{or} \hspace{20px} \max B - \min B,\]
where
\[A = \{x_i+y_i : i = 1,2,\ldots,n\}\]
and
\[B = \{x_i-y_i : i = 1,2,\ldots,n\}.\]
\begin{samepage}
The reason for this is that the Manhattan distance
\[|x_a-x_b|+|y_a-y_b]\]
can be written
\begin{center} \begin{center}
\begin{tabular}{cl} \begin{tikzpicture}[scale=0.65]
& $\max(x_a-x_b+y_a-y_b,\,x_a-x_b-y_a+y_b)$ \\ \draw[color=gray] (-1,-1) grid (4,4);
$=$ & $\max(x_a+y_a-(x_b+y_b),\,x_a-y_a-(x_b-y_b))$
\end{tabular}
\end{center}
assuming that $x_a \ge x_b$.
\end{samepage}
\begin{samepage} \filldraw (0,2) circle (2.5pt);
\subsubsection{Rotating coordinates} \filldraw (3,3) circle (2.5pt);
\filldraw (1,0) circle (2.5pt);
\filldraw (3,1) circle (2.5pt);
A useful technique related to the Manhattan distance \node at (0,1.5) {$A$};
is to rotate all coordinates 45 degrees so that \node at (3,2.5) {$C$};
a point $(x,y)$ becomes $(a(x+y),a(y-x))$, \node at (1,-0.5) {$B$};
where $a=1/\sqrt{2}$. \node at (3,0.5) {$D$};
The coefficient $a$ is chosen so that
the distances between the points remain the same.
After the rotation, the region within a distance of $d$
from a point is a square with horizontal and vertical sides:
\begin{center}
\begin{tikzpicture}
\draw[fill=gray!20] (0,1) -- (-1,0) -- (0,-1) -- (1,0) -- (0,1);
\draw[fill] (0,0) circle [radius=0.05];
\node at (2.5,0) {$\Rightarrow$};
\draw[fill=gray!20] (5-0.71,0.71) -- (5-0.71,-0.71) -- (5+0.71,-0.71) -- (5+0.71,0.71) -- (5-0.71,0.71);
\draw[fill] (5,0) circle [radius=0.05];
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
\end{samepage}
Implementing many algorithms is easier when we may assume that all The maximum Manhattan distance is 5
objects are squares with horizontal and vertical sides. between points $B$ and $C$:
\begin{center}
\begin{tikzpicture}[scale=0.65]
\draw[color=gray] (-1,-1) grid (4,4);
\filldraw (0,2) circle (2.5pt);
\filldraw (3,3) circle (2.5pt);
\filldraw (1,0) circle (2.5pt);
\filldraw (3,1) circle (2.5pt);
\node at (0,1.5) {$A$};
\node at (3,2.5) {$C$};
\node at (1,-0.5) {$B$};
\node at (3,0.5) {$D$};
\path[draw=red,thick,line width=2pt] (1,0) -- (1,3) -- (3,3);
\end{tikzpicture}
\end{center}
A useful technique related to Manhattan distances
is to rotate all coordinates 45 degrees so that
a point $(x,y)$ becomes $(x+y,y-x)$.
For example, after rotating the above points,
the result is:
\begin{center}
\begin{tikzpicture}[scale=0.6]
\draw[color=gray] (0,-3) grid (7,3);
\filldraw (2,2) circle (2.5pt);
\filldraw (6,0) circle (2.5pt);
\filldraw (1,-1) circle (2.5pt);
\filldraw (4,-2) circle (2.5pt);
\node at (2,1.5) {$A$};
\node at (6,-0.5) {$C$};
\node at (1,-1.5) {$B$};
\node at (4,-2.5) {$D$};
\end{tikzpicture}
\end{center}
And the maximum distance is as follows:
\begin{center}
\begin{tikzpicture}[scale=0.6]
\draw[color=gray] (0,-3) grid (7,3);
\filldraw (2,2) circle (2.5pt);
\filldraw (6,0) circle (2.5pt);
\filldraw (1,-1) circle (2.5pt);
\filldraw (4,-2) circle (2.5pt);
\node at (2,1.5) {$A$};
\node at (6,-0.5) {$C$};
\node at (1,-1.5) {$B$};
\node at (4,-2.5) {$D$};
\path[draw=red,thick,line width=2pt] (1,-1) -- (4,2) -- (6,0);
\end{tikzpicture}
\end{center}
Consider two points $p_1=(x_1,y_1)$ and $p_2=(x_1,y_1)$ whose rotated
coordinates are $p'_1=(x'_1,y'_1)$ and $p'_2=(x'_2,y'_2)$.
The Manhattan distance is
\[|x_1-x_2|+|y_1-y_2| = \max(|x'_1-x'_2|,|y'_1-y'_2|)\]
For example, if $p_1=(1,0)$ and $p_2=(3,3)$,
the rotated coordinates are $p'_1=(1,-1)$ and $p'_2=(6,0)$
and the Manhattan distance is
\[|1-3|+|0-3| = \max(|1-6|,|-1-0|) = 5.\]
The rotated coordinates provide a simple way
to operate with Manhattan distances, because we can
consider x and y coordinates separately.
To maximize the Manhattan distance between two points,
we should find two points whose
rotated coordinates maximize the value of
\[\max(|x'_1-x'_2|,|y'_1-y'_2|).\]
This is easy, because either the horizontal or vertical
difference of the rotated coordinates has to be maximum.