Small improvements
This commit is contained in:
parent
f03fbd49a3
commit
480f58d386
|
@ -55,7 +55,7 @@ how to divide the quadrilateral into triangles?
|
|||
It turns out that sometimes we cannot just pick
|
||||
two arbitrary opposite vertices.
|
||||
For example, in the following situation,
|
||||
the division line is outside the quadrilateral:
|
||||
the division line is \emph{outside} the quadrilateral:
|
||||
\begin{center}
|
||||
\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.
|
||||
|
||||
However, it turns out that we can solve the problem using
|
||||
another method that is much easier to implement
|
||||
and does not involve any special cases.
|
||||
another method that is much easier to use.
|
||||
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,\]
|
||||
that calculates the area of a quadrilateral
|
||||
|
@ -132,11 +131,11 @@ following point and vector:
|
|||
The complex number class \texttt{complex} in C++ is
|
||||
useful when solving geometric problems.
|
||||
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.
|
||||
|
||||
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}
|
||||
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
|
||||
\end{lstlisting}
|
||||
|
||||
An appropriate coordinate type is
|
||||
In practice,
|
||||
an appropriate coordinate type is usually
|
||||
\texttt{long long} (integer) or \texttt{long double}
|
||||
(real number), depending on the situation.
|
||||
Integers should be used whenever possible,
|
||||
(real number).
|
||||
It is a good idea to use integer whenever possible,
|
||||
because calculations with integers are exact.
|
||||
If real numbers are needed,
|
||||
precision errors should be taken into account
|
||||
when comparing them.
|
||||
A safe way to check if numbers $a$ and $b$ are equal
|
||||
when comparing numbers.
|
||||
A safe way to check if real numbers $a$ and $b$ are equal
|
||||
is to compare them using $|a-b|<\epsilon$,
|
||||
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
|
||||
\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)$
|
||||
using the formula $\sqrt{x^2+y^2}$.
|
||||
The function can also be used for
|
||||
|
@ -199,7 +199,7 @@ P b = {3,-1};
|
|||
cout << abs(b-a) << "\n"; // 3.16228
|
||||
\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.
|
||||
The function gives the angle in radians,
|
||||
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
|
||||
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$.
|
||||
In addition, a vector can be rotated by an 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}
|
||||
|
||||
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$
|
||||
turns left (positive value), does not turn (zero)
|
||||
or turns right (negative value)
|
||||
|
@ -265,7 +266,7 @@ The following picture illustrates the above cases:
|
|||
\end{center}
|
||||
|
||||
\noindent
|
||||
For example, in the first picture
|
||||
For example, in the first case
|
||||
$a=(4,2)$ and $b=(1,2)$.
|
||||
The following code calculates the cross product
|
||||
using the class \texttt{complex}:
|
||||
|
@ -285,7 +286,7 @@ of the result is $x_1 y_2 - x_2 y_1$.
|
|||
|
||||
\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
|
||||
side of a line.
|
||||
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}
|
||||
|
||||
Consider a problem where we are given two line segments
|
||||
$ab$ and $cd$ and our task is to check whether they
|
||||
intersect. The possible cases are:
|
||||
Consider the problem of checking
|
||||
whether two given line segments
|
||||
$ab$ and $cd$ intersect. The possible cases are:
|
||||
|
||||
\textit{Case 1:}
|
||||
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}
|
||||
|
||||
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
|
||||
\[\frac{| (a-c) \times (b-c) |}{2},\]
|
||||
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_1-p) \times (s_2-p))$.
|
||||
Thus, the shortest distance is
|
||||
|
||||
\[ d = \frac{(s_1-p) \times (s_2-p)}{|s_2-s_1|} .\]
|
||||
|
||||
\subsubsection{Point inside a polygon}
|
||||
|
||||
Let us now consider a problem where our task is to
|
||||
find out whether a point is located inside or outside
|
||||
Let us now consider the problem of
|
||||
testing whether a point is located inside or outside
|
||||
a polygon.
|
||||
For example, in the following picture point $a$
|
||||
is inside the polygon and point $b$ is outside
|
||||
|
@ -463,7 +464,7 @@ the polygon.
|
|||
\end{center}
|
||||
|
||||
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
|
||||
the boundary of the polygon.
|
||||
If the number is odd,
|
||||
|
@ -531,7 +532,7 @@ For example, the area of the polygon
|
|||
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.\]
|
||||
|
||||
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,
|
||||
and another side lies on the horizontal line $y=0$.
|
||||
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
|
||||
all such trapezoids, which yields the formula
|
||||
|
||||
\[|\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)|.\]
|
||||
|
||||
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
|
||||
along the boundary of the polygon.
|
||||
|
||||
|
@ -616,12 +616,12 @@ is $6+7/2-1=17/2$.
|
|||
\section{Distance functions}
|
||||
|
||||
\index{distance function}
|
||||
\index{Euklidean distance}
|
||||
\index{Euclidean distance}
|
||||
\index{Manhattan distance}
|
||||
|
||||
A \key{distance function} defines the distance between
|
||||
two points.
|
||||
The usual distance function in geometry is the
|
||||
The usual distance function is the
|
||||
\key{Euclidean distance} where the distance between
|
||||
points $(x_1,y_1)$ and $(x_2,y_2)$ is
|
||||
\[\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}.\]
|
||||
|
@ -723,7 +723,7 @@ between points $B$ and $C$:
|
|||
\end{tikzpicture}
|
||||
\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
|
||||
a point $(x,y)$ becomes $(x+y,y-x)$.
|
||||
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
|
||||
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|)\]
|
||||
|
||||
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.\]
|
||||
|
||||
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.
|
||||
To maximize the Manhattan distance between two points,
|
||||
we should find two points whose
|
||||
|
|
Loading…
Reference in New Issue