From 9cc199af20c5a3b07204fd5691cbe549d4a936aa Mon Sep 17 00:00:00 2001 From: Antti H S Laaksonen Date: Sat, 18 Feb 2017 20:45:58 +0200 Subject: [PATCH] Rotating coordinates --- luku29.tex | 145 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 96 insertions(+), 49 deletions(-) diff --git a/luku29.tex b/luku29.tex index dcf606a..a4914f6 100644 --- a/luku29.tex +++ b/luku29.tex @@ -675,62 +675,109 @@ from the center point, using the Euclidean and Manhattan distances: \end{tikzpicture} \end{center} -\subsubsection{Furthest points} +\subsubsection{Rotating coordinates} Some problems are easier to solve if the Manhattan distance is used instead of the Euclidean distance. -For example, consider a problem where we are given -$n$ points $(x_1,y_1),(x_2,y_2),\ldots,(x_n,y_n)$ -and our task is to calculate the maximum distance -between any two points. +As an example, consider a problem where we are given +$n$ points in the two-dimensional plane +and our task is to calculate the maximum Manhattan +distance between any two points. -This is a difficult problem if the Euclidean distance -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 +For example, consider the following set of points: \begin{center} -\begin{tabular}{cl} -& $\max(x_a-x_b+y_a-y_b,\,x_a-x_b-y_a+y_b)$ \\ -$=$ & $\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{tikzpicture}[scale=0.65] +\draw[color=gray] (-1,-1) grid (4,4); -\begin{samepage} -\subsubsection{Rotating coordinates} +\filldraw (0,2) circle (2.5pt); +\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 -is to rotate all coordinates 45 degrees so that -a point $(x,y)$ becomes $(a(x+y),a(y-x))$, -where $a=1/\sqrt{2}$. -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]; +\node at (0,1.5) {$A$}; +\node at (3,2.5) {$C$}; +\node at (1,-0.5) {$B$}; +\node at (3,0.5) {$D$}; \end{tikzpicture} \end{center} -\end{samepage} -Implementing many algorithms is easier when we may assume that all -objects are squares with horizontal and vertical sides. \ No newline at end of file +The maximum Manhattan distance is 5 +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. \ No newline at end of file