Small improvements

This commit is contained in:
Antti H S Laaksonen 2017-04-22 15:13:42 +03:00
parent f03fbd49a3
commit 480f58d386
1 changed files with 33 additions and 32 deletions

View File

@ -55,7 +55,7 @@ how to divide the quadrilateral into triangles?
It turns out that sometimes we cannot just pick It turns out that sometimes we cannot just pick
two arbitrary opposite vertices. two arbitrary opposite vertices.
For example, in the following situation, For example, in the following situation,
the division line is outside the quadrilateral: the division line is \emph{outside} the quadrilateral:
\begin{center} \begin{center}
\begin{tikzpicture}[scale=0.45] \begin{tikzpicture}[scale=0.45]
@ -85,8 +85,7 @@ It is clear for a human which of the lines is the correct
choice, but the situation is difficult for a computer. choice, but the situation is difficult for a computer.
However, it turns out that we can solve the problem using However, it turns out that we can solve the problem using
another method that is much easier to implement another method that is much easier to use.
and does not involve any special cases.
Namely, there is a general formula Namely, there is a general formula
\[x_1y_2-x_2y_1+x_2y_3-x_3y_2+x_3y_4-x_4y_3+x_4y_1-x_1y_4,\] \[x_1y_2-x_2y_1+x_2y_3-x_3y_2+x_3y_4-x_4y_3+x_4y_1-x_1y_4,\]
that calculates the area of a quadrilateral that calculates the area of a quadrilateral
@ -132,11 +131,11 @@ following point and vector:
The complex number class \texttt{complex} in C++ is The complex number class \texttt{complex} in C++ is
useful when solving geometric problems. useful when solving geometric problems.
Using the class we can represent points and vectors Using the class we can represent points and vectors
as complex numbers, and the class also contains tools as complex numbers, and the class contains tools
that are useful in geometry. that are useful in geometry.
In the following code, \texttt{C} is the type of In the following code, \texttt{C} is the type of
a coordinate and \texttt{P} is the type of a point or vector. a coordinate and \texttt{P} is the type of a point or a vector.
In addition, the code defines the macros \texttt{X} and \texttt{Y} In addition, the code defines the macros \texttt{X} and \texttt{Y}
that can be used to refer to x and y coordinates. that can be used to refer to x and y coordinates.
@ -165,15 +164,16 @@ P s = v+u;
cout << s.X << " " << s.Y << "\n"; // 5 3 cout << s.X << " " << s.Y << "\n"; // 5 3
\end{lstlisting} \end{lstlisting}
An appropriate coordinate type is In practice,
an appropriate coordinate type is usually
\texttt{long long} (integer) or \texttt{long double} \texttt{long long} (integer) or \texttt{long double}
(real number), depending on the situation. (real number).
Integers should be used whenever possible, It is a good idea to use integer whenever possible,
because calculations with integers are exact. because calculations with integers are exact.
If real numbers are needed, If real numbers are needed,
precision errors should be taken into account precision errors should be taken into account
when comparing them. when comparing numbers.
A safe way to check if numbers $a$ and $b$ are equal A safe way to check if real numbers $a$ and $b$ are equal
is to compare them using $|a-b|<\epsilon$, is to compare them using $|a-b|<\epsilon$,
where $\epsilon$ is a small number (for example, $\epsilon=10^{-9}$). where $\epsilon$ is a small number (for example, $\epsilon=10^{-9}$).
@ -182,7 +182,7 @@ where $\epsilon$ is a small number (for example, $\epsilon=10^{-9}$).
In the following examples, the coordinate type is In the following examples, the coordinate type is
\texttt{long double}. \texttt{long double}.
The function \texttt{abs(v)} calculates the length The function $\texttt{abs}(v)$ calculates the length
$|v|$ of a vector $v=(x,y)$ $|v|$ of a vector $v=(x,y)$
using the formula $\sqrt{x^2+y^2}$. using the formula $\sqrt{x^2+y^2}$.
The function can also be used for The function can also be used for
@ -199,7 +199,7 @@ P b = {3,-1};
cout << abs(b-a) << "\n"; // 3.16228 cout << abs(b-a) << "\n"; // 3.16228
\end{lstlisting} \end{lstlisting}
The function \texttt{arg(v)} calculates the The function $\texttt{arg}(v)$ calculates the
angle of a vector $v=(x,y)$ with respect to the x axis. angle of a vector $v=(x,y)$ with respect to the x axis.
The function gives the angle in radians, The function gives the angle in radians,
where $r$ radians equals $180 r/\pi$ degrees. where $r$ radians equals $180 r/\pi$ degrees.
@ -207,7 +207,7 @@ The angle of a vector that points to the right is 0,
and angles decrease clockwise and increase and angles decrease clockwise and increase
counterclockwise. counterclockwise.
The function \texttt{polar(s,a)} constructs a vector The function $\texttt{polar}(s,a)$ constructs a vector
whose length is $s$ and that points to an angle $a$. whose length is $s$ and that points to an angle $a$.
In addition, a vector can be rotated by an angle $a$ In addition, a vector can be rotated by an angle $a$
by multiplying it by a vector with length 1 and angle $a$. by multiplying it by a vector with length 1 and angle $a$.
@ -228,7 +228,8 @@ cout << arg(v) << "\n"; // 0.963648
\index{cross product} \index{cross product}
The \key{cross product} $a \times b$ of vectors The \key{cross product} $a \times b$ of vectors
$a=(x_1,y_1)$ and $b=(x_2,y_2)$ equals $x_1 y_2 - x_2 y_1$. $a=(x_1,y_1)$ and $b=(x_2,y_2)$ is calculated
using the formula $x_1 y_2 - x_2 y_1$.
The cross product tells us whether $b$ The cross product tells us whether $b$
turns left (positive value), does not turn (zero) turns left (positive value), does not turn (zero)
or turns right (negative value) or turns right (negative value)
@ -265,7 +266,7 @@ The following picture illustrates the above cases:
\end{center} \end{center}
\noindent \noindent
For example, in the first picture For example, in the first case
$a=(4,2)$ and $b=(1,2)$. $a=(4,2)$ and $b=(1,2)$.
The following code calculates the cross product The following code calculates the cross product
using the class \texttt{complex}: using the class \texttt{complex}:
@ -285,7 +286,7 @@ of the result is $x_1 y_2 - x_2 y_1$.
\subsubsection{Point location} \subsubsection{Point location}
Cross products can be used for testing Cross products can be used to test
whether a point is located on the left or right whether a point is located on the left or right
side of a line. side of a line.
Assume that the line goes through points Assume that the line goes through points
@ -320,9 +321,9 @@ points $s_1$, $s_2$ and $p$ are on the same line.
\index{line segment intersection} \index{line segment intersection}
Consider a problem where we are given two line segments Consider the problem of checking
$ab$ and $cd$ and our task is to check whether they whether two given line segments
intersect. The possible cases are: $ab$ and $cd$ intersect. The possible cases are:
\textit{Case 1:} \textit{Case 1:}
The line segments are on the same line The line segments are on the same line
@ -408,7 +409,8 @@ Hence, we can use cross products to check this.
\subsubsection{Point distance from a line} \subsubsection{Point distance from a line}
The area of a triangle can be calculated Another feature of the cross product is that
the area of a triangle can be calculated
using the formula using the formula
\[\frac{| (a-c) \times (b-c) |}{2},\] \[\frac{| (a-c) \times (b-c) |}{2},\]
where $a$, $b$ and $c$ are the vertices of the triangle. where $a$, $b$ and $c$ are the vertices of the triangle.
@ -438,13 +440,12 @@ it is both
$\frac{1}{2} |s_2-s_1| d$ and $\frac{1}{2} |s_2-s_1| d$ and
$\frac{1}{2} ((s_1-p) \times (s_2-p))$. $\frac{1}{2} ((s_1-p) \times (s_2-p))$.
Thus, the shortest distance is Thus, the shortest distance is
\[ d = \frac{(s_1-p) \times (s_2-p)}{|s_2-s_1|} .\] \[ d = \frac{(s_1-p) \times (s_2-p)}{|s_2-s_1|} .\]
\subsubsection{Point inside a polygon} \subsubsection{Point inside a polygon}
Let us now consider a problem where our task is to Let us now consider the problem of
find out whether a point is located inside or outside testing whether a point is located inside or outside
a polygon. a polygon.
For example, in the following picture point $a$ For example, in the following picture point $a$
is inside the polygon and point $b$ is outside is inside the polygon and point $b$ is outside
@ -463,7 +464,7 @@ the polygon.
\end{center} \end{center}
A convenient way to solve the problem is to A convenient way to solve the problem is to
send a ray from the point to an arbitrary direction send a \emph{ray} from the point to an arbitrary direction
and calculate the number of times it touches and calculate the number of times it touches
the boundary of the polygon. the boundary of the polygon.
If the number is odd, If the number is odd,
@ -531,7 +532,7 @@ For example, the area of the polygon
is is
\[\frac{|(2\cdot5-5\cdot4)+(5\cdot3-7\cdot5)+(7\cdot1-4\cdot3)+(4\cdot3-4\cdot1)+(4\cdot4-2\cdot3)|}{2} = 17/2.\] \[\frac{|(2\cdot5-5\cdot4)+(5\cdot3-7\cdot5)+(7\cdot1-4\cdot3)+(4\cdot3-4\cdot1)+(4\cdot4-2\cdot3)|}{2} = 17/2.\]
The idea in the formula is to go through trapezoids The idea of the formula is to go through trapezoids
whose one side is a side of the polygon, whose one side is a side of the polygon,
and another side lies on the horizontal line $y=0$. and another side lies on the horizontal line $y=0$.
For example: For example:
@ -560,12 +561,11 @@ and if $x_{i+1}<x_{i}$, the area is negative.
The area of the polygon is the sum of areas of The area of the polygon is the sum of areas of
all such trapezoids, which yields the formula all such trapezoids, which yields the formula
\[|\sum_{i=1}^{n-1} (x_{i+1}-x_{i}) \frac{y_i+y_{i+1}}{2}| = \[|\sum_{i=1}^{n-1} (x_{i+1}-x_{i}) \frac{y_i+y_{i+1}}{2}| =
\frac{1}{2} |\sum_{i=1}^{n-1} (x_i y_{i+1} - x_{i+1} y_i)|.\] \frac{1}{2} |\sum_{i=1}^{n-1} (x_i y_{i+1} - x_{i+1} y_i)|.\]
Note that the absolute value of the sum is taken, Note that the absolute value of the sum is taken,
because the value of the sum may be positive or negative because the value of the sum may be positive or negative,
depending on whether we walk clockwise or counterclockwise depending on whether we walk clockwise or counterclockwise
along the boundary of the polygon. along the boundary of the polygon.
@ -616,12 +616,12 @@ is $6+7/2-1=17/2$.
\section{Distance functions} \section{Distance functions}
\index{distance function} \index{distance function}
\index{Euklidean distance} \index{Euclidean distance}
\index{Manhattan distance} \index{Manhattan distance}
A \key{distance function} defines the distance between A \key{distance function} defines the distance between
two points. two points.
The usual distance function in geometry is the The usual distance function is the
\key{Euclidean distance} where the distance between \key{Euclidean distance} where the distance between
points $(x_1,y_1)$ and $(x_2,y_2)$ is points $(x_1,y_1)$ and $(x_2,y_2)$ is
\[\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}.\] \[\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}.\]
@ -723,7 +723,7 @@ between points $B$ and $C$:
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
A useful technique related to Manhattan distances A useful technique related to the Manhattan distance
is to rotate all coordinates 45 degrees so that is to rotate all coordinates 45 degrees so that
a point $(x,y)$ becomes $(x+y,y-x)$. a point $(x,y)$ becomes $(x+y,y-x)$.
For example, after rotating the above points, For example, after rotating the above points,
@ -766,7 +766,8 @@ And the maximum distance is as follows:
Consider two points $p_1=(x_1,y_1)$ and $p_2=(x_1,y_1)$ whose rotated 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)$. coordinates are $p'_1=(x'_1,y'_1)$ and $p'_2=(x'_2,y'_2)$.
The Manhattan distance is Now there are two ways to express the Manhattan distance
between $p_1$ and $p_2$:
\[|x_1-x_2|+|y_1-y_2| = \max(|x'_1-x'_2|,|y'_1-y'_2|)\] \[|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)$, For example, if $p_1=(1,0)$ and $p_2=(3,3)$,
@ -775,7 +776,7 @@ and the Manhattan distance is
\[|1-3|+|0-3| = \max(|1-6|,|-1-0|) = 5.\] \[|1-3|+|0-3| = \max(|1-6|,|-1-0|) = 5.\]
The rotated coordinates provide a simple way The rotated coordinates provide a simple way
to operate with Manhattan distances, because we can to operate with the Manhattan distance, because we can
consider x and y coordinates separately. consider x and y coordinates separately.
To maximize the Manhattan distance between two points, To maximize the Manhattan distance between two points,
we should find two points whose we should find two points whose