Knowledge - the basis of computational geometry
Lecture
point
We point \ (\ mathbf r \) as the \ (\ mathbf 0 \) to \ (\ mathbf r \) vector \ (\ vec {\ mathbf r } \)
#define ftype long double
struct point2d {
ftype x, y;
point2d() {}
point2d(ftype x, ftype y): x(x), y(y) {}
point2d& operator+=(const point2d &t) {
x += t.x;
y += t.y;
return *this;
}
point2d& operator-=(const point2d &t) {
x -= t.x;
y -= t.y;
return *this;
}
point2d& operator*=(ftype t) {
x *= t;
y *= t;
return *this;
}
point2d& operator/=(ftype t) {
x /= t;
y /= t;
return *this;
}
point2d operator+(const point2d &t) const {
return point2d(*this) += t;
}
point2d operator-(const point2d &t) const {
return point2d(*this) -= t;
}
point2d operator*(ftype t) const {
return point2d(*this) *= t;
}
point2d operator/(ftype t) const {
return point2d(*this) /= t;
}
};
point2d operator*(ftype a, point2d b) {
return b * a;
}
Dot
- \(\mathbf a \cdot \mathbf b = \mathbf b \cdot \mathbf a\)
- \((\alpha \cdot \mathbf a)\cdot \mathbf b = \alpha \cdot (\mathbf a \cdot \mathbf b)\)
- \((\mathbf a + \mathbf b)\cdot \mathbf c = \mathbf a \cdot \mathbf c + \mathbf b \cdot \mathbf c\)
If a unit vector:
\ [\ mathbf e_x = \ begin {pmatrix} 1 \\\ 0 \\\ 0 \ end {pmatrix}, \ mathbf e_y = \ begin {pmatrix} 0 \\\ 1 \\\ 0 \ end {pmatrix}, \ mathbf e_z = \ begin {pmatrix
} 0 \\\ 0 \\\ 1 \ end {pmatrix} \]. we define \ (\ mathbf r = (x ; y; z) \) represents \ (r = x \ CDOT \ mathbf e_x + Y \ CDOT \ mathbf e_Y + Z \ CDOT \ mathbf E - z \) .
since
\ [\ mathbf e_x \ cdot \ mathbf e_x = \ mathbf e_y \ cdot \ mathbf e_y = \ mathbf e_z \ cdot \ mathbf e_z = 1, \\\ \ mathbf e_x \
cdot \ mathbf e_y = \ mathbf e_y \ cdot \ mathbf e_z = \ mathbf e_z \ cdot \ mathbf e_x = 0 \] so the \ (\ mathbf a = (x_1 ; y_1; z_1 ) \) and \ (\ mathbf b = (x_2 ; y_2; z_2) \) have
\[\mathbf a\cdot \mathbf b = (x_1 \cdot \mathbf e_x + y_1 \cdot\mathbf e_y + z_1 \cdot\mathbf e_z)\cdot( x_2 \cdot\mathbf e_x + y_2 \cdot\mathbf e_y + z_2 \cdot\mathbf e_z) = x_1 x_2 + y_1 y_2 + z_1 z_2\]
ftype dot(point2d a, point2d b) {
return a.x * b.x + a.y * b.y;
}
ftype dot(point3d a, point3d b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
Some definitions:
- Of NORM \ (\ mathbf A \) (length squared): \ (| \ mathbf A | ^ 2 = \ mathbf A \ CDOT \ mathbf A \)
- Length of \(\mathbf a\): \(|\mathbf a| = \sqrt{\mathbf a\cdot \mathbf a}\)
- Projection of \(\mathbf a\) onto \(\mathbf b\)(投影): \(\dfrac{\mathbf a\cdot\mathbf b}{|\mathbf b|}\)
- Angle between vectors(夹角): \(\arccos \left(\dfrac{\mathbf a\cdot \mathbf b}{|\mathbf a| \cdot |\mathbf b|}\right)\)
- From the point by point description it can be used to determine the positive and negative acute angle (acute) an obtuse angle (Obtuse) at right angles (orthogonal).
ftype norm(point2d a) {
return dot(a, a);
}
double abs(point2d a) {
return sqrt(norm(a));
}
double proj(point2d a, point2d b) {
return dot(a, b) / abs(b);
}
double angle(point2d a, point2d b) {
return acos(dot(a, b) / abs(a) / abs(b));
}
Cross product
definition:
Define triple product Triple Product \ (\ mathbf A \ CDOT (\ mathbf B \ Times \ mathbf C) \) is the volume above the parallelepiped body, so that we can get \ (\ mathbf b \ times \ mathbf c \) of size and orientation.
nature:
- \(\mathbf a\times \mathbf b = -\mathbf b\times \mathbf a\)
- \((\alpha \cdot \mathbf a)\times \mathbf b = \alpha \cdot (\mathbf a\times \mathbf b)\)
- \(\mathbf a\cdot (\mathbf b\times \mathbf c) = \mathbf b\cdot (\mathbf c\times \mathbf a) = -\mathbf a\cdot( \mathbf c\times \mathbf b)\)
- \((\mathbf a + \mathbf b)\times \mathbf c = \mathbf a\times \mathbf c + \mathbf b\times \mathbf c\).
对任意的 \(\mathbf r\) 有:
\[\mathbf r\cdot( (\mathbf a + \mathbf b)\times \mathbf c) = (\mathbf a + \mathbf b) \cdot (\mathbf c\times \mathbf r) = \mathbf a \cdot(\mathbf c\times \mathbf r) + \mathbf b\cdot(\mathbf c\times \mathbf r) = \mathbf r\cdot (\mathbf a\times \mathbf c) + \mathbf r\cdot(\mathbf b\times \mathbf c) = \mathbf r\cdot(\mathbf a\times \mathbf c + \mathbf b\times \mathbf c)\]
这证明了第三点 \((\mathbf a + \mathbf b)\times \mathbf c = \mathbf a\times \mathbf c + \mathbf b\times \mathbf c\) - \(|\mathbf a\times \mathbf b|=|\mathbf a| \cdot |\mathbf b| \sin \theta\)
because
\ [\ mathbf e_x \ times \ mathbf e_x = \ mathbf e_y \ times \ mathbf e_y = \ mathbf e_z \ times \ mathbf e_z = \ mathbf 0, \\\ \ mathbf e_x \ times \ mathbf e_y = \ mathbf e_z, ~ \ mathbf e_y \ times \ mathbf e_z
= \ mathbf e_x, ~ \ mathbf e_z \ times \ mathbf e_x = \ mathbf e_y \] then we can calculate the \ (\ mathbf (x_1 a = ; y_1; z_1) \) and \ ( \ mathbf b = (x_2; y_2 ; z_2) \) cross product results:
\[\mathbf a\times \mathbf b = (x_1 \cdot \mathbf e_x + y_1 \cdot \mathbf e_y + z_1 \cdot \mathbf e_z)\times (x_2 \cdot \mathbf e_x + y_2 \cdot \mathbf e_y + z_2 \cdot \mathbf e_z) =\]
\[(y_1 z_2 - z_1 y_2)\mathbf e_x + (z_1 x_2 - x_1 z_2)\mathbf e_y + (x_1 y_2 - y_1 x_2)\]
Determinant expression with the words:
\[\mathbf a\times \mathbf b = \begin{vmatrix}\mathbf e_x & \mathbf e_y & \mathbf e_z \\\ x_1 & y_1 & z_1 \\\ x_2 & y_2 & z_2 \end{vmatrix},~a\cdot(b\times c) = \begin{vmatrix} x_1 & y_1 & z_1 \\\ x_2 & y_2 & z_2 \\\ x_3 & y_3 & z_3 \end{vmatrix}\]
Two-dimensional cross product (namely the pseudo-scalar product) may be defined as
\ [| \ mathbf e_z \ cdot
(\ mathbf a \ times \ mathbf b) | = | x_1 y_2 - y_1 x_2 | \] an intuitive way of understanding to calculate \ (| \ mathbf a | \ cdot | \ mathbf b | \ sin \ theta \) a \ (\ mathbf a \) turn \ (90 ^ \ circ \) to give \ (\ widehat {\ mathbf a } = (- y_1; x_1) \ ) then. \ (| \ widehat {\ mathbf A} \ CDOT \ mathbf B | = | x_1y_2 - Y_1 x_2 | \) .
point3d cross(point3d a, point3d b) {
return point3d(a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
}
ftype triple(point3d a, point3d b, point3d c) {
return dot(a, cross(b, c));
}
ftype cross(point2d a, point2d b) {
return a.x * b.y - a.y * b.x;
}
Lines and Planes
A straight line can be represented as a starting point \ (\ mathbf r_0 \) and a direction vector \ (\ mathbf D \) , or two points \ (\ mathbf A \) , \ (\ mathbf B \) . Corresponding to the equation
\ [(\ mathbf r - \
mathbf r_0) \ times \ mathbf d = 0 \\ (\ mathbf r - \ mathbf a) \ times (\ mathbf b - \ mathbf a) = 0. \] a plane can be determined three points: \ (\ mathbf A \) , \ (\ mathbf B \) , \ (\ mathbf C \) . Or an initial point \ (\ mathbf r_0 \) and a set in this plane in the vector \ (\ mathbf D_1 \) , \ (\ mathbf D_2 \) is determined:
\ [(\ mathbf R & lt - \ mathbf A) \ CDOT ((\ mathbf b - \ mathbf a) \ times (\ mathbf c - \ mathbf a)) = 0 \\ (\ mathbf r - \ mathbf r_0) \ cdot (\ mathbf d_1 \ times \ mathbf d_2) = 0 \ ]
Line intersection
\(l_1:\mathbf r = \mathbf a_1 + t \cdot \mathbf d_1\) 带入 \(l_2:(\mathbf r - \mathbf a_2)\times \mathbf d_2=0\)
\[ (\mathbf a_1 + t \cdot \mathbf d_1 - \mathbf a_2)\times \mathbf d_2=0 \quad\Rightarrow\quad t = \dfrac{(\mathbf a_2 - \mathbf a_1)\times\mathbf d_2}{\mathbf d_1\times \mathbf d_2} \]
point2d intersect(point2d a1, point2d d1, point2d a2, point2d d2) {
return a1 + cross(a2 - a1, d2) / cross(d1, d2) * d1;
}
The intersection of three planes
You give the initial point of the three planes \ (\ mathbf a_i \) and normal vector \ (\ mathbf n_i \) thus obtained equation:
\ [\ Cases the begin {} \ mathbf R & lt \ CDOT \ mathbf of n_1 = \ mathbf A_1 \ CDOT \ mathbf n_1, \\\ \ mathbf r \ cdot \ mathbf n_2 = \ mathbf a_2 \ cdot \ mathbf n_2, \\\ \ mathbf r \ cdot \ mathbf n_3 = \ mathbf a_3 \ cdot \ mathbf n_3 \ end {cases} \]
with Cramer rules for solving:
point3d intersect(point3d a1, point3d n1, point3d a2, point3d n2, point3d a3, point3d n3) {
point3d x(n1.x, n2.x, n3.x);
point3d y(n1.y, n2.y, n3.y);
point3d z(n1.z, n2.z, n3.z);
point3d d(dot(a1, n1), dot(a2, n2), dot(a3, n3));
return point3d(triple(d, y, z),
triple(x, d, z),
triple(x, y, d)) / triple(n1, n2, n3);
}
template
Two genres, i.e., a straight line is a vector showing two points \ (\ mathbf A \) , \ (\ mathbf B \) , i.e., the other is a linear equation \ (a_1 x + b_1 y + c_1 = 0 \)