Two-dimensional coordinate system of the dot product, cross product, polygon area
The dot product is defined
With vector \ (\ vec {a} \ ) and \ (VEC {B} \ \) , the definition of the dot product \ (\ vec {a} \ cdot \ vec {b} \) is a real number , a value of the vector \ (\ vec {a} \) in the vector \ (\ vec {b} \ ) on the projected length of the vector is multiplied by \ (\ vec {b} \ ) of the mold
The dot product of nature
Commutative, associative
Dot product calculation
Direct calculation
\ [| \ Begin {and} \ ln \ begin {b} | = | \ Begin {a} | \ Times | \ begin {b} | \ times cos <\ begin {and} \ \ begin {b}> \]
Calculating coordinates
On the coordinate system set \ (\ vec {a} = (x_ {1}, \ y_ {1}), \ \ vec {b} = (x_ {2}, \ y_ {2}) \)
Then \ (\ vec {a} \ cdot \ vec {b} = x_ {1} x_ {2} + y_ {1} y_ {2} \)
The definition of cross product
With vector \ (\ vec {a} \ ) and \ (\ VEC {B} \) , define the cross product \ (\ vec {a} \ times \ vec {b} \) is the new vector .
Which mold length \ (\ vec {a} \ ) and \ (\ vec {b} \ ) enclosed by a parallelogram area.
Direction \ (\ vec {a} \ ) and \ (\ vec {b} \ ) are vertical.
If \ (\ vec {b} \ ) in \ (\ vec {a} \ ) in the counterclockwise direction, i.e., the left-handed presented, then the \ (\ vec {a} \ times \ vec {b} \) is positive; conversely It is negative.
The cross product of nature
Is the cross product mold parallelogram area, the seeking of a triangle in the area of the rectangular coordinate system can be used to calculate the cross product, i.e.,
\ [S_ {OAB} = \ frac {1} {2} | \ begin {a} \ times \ begin {b} | \]
Cross product is not commutative, since the direction change, i.e.
\ [\ vec {a} \ times \ vec {b} = - \ vec {b} \ times \ vec {a} \]
Calculating a two-dimensional cross product mold
Direct calculation
\ [| \ Begin {and} \ times \ begin {b} | = | \ Begin {a} | \ Times | \ begin {b} | \ times sin <\ begin {and} \ \ begin {b}> \]
Calculating coordinates
On the coordinate system set \ (\ vec {a} = (x_ {1}, \ y_ {1}), \ \ vec {b} = (x_ {2}, \ y_ {2}) \)
After deduced \ (| \ vec {a} \ times \ vec {b} | = | x_ {1} y_ {2} - x_ {2} y_ {1} | \)
It will have to remove the absolute value of the directed area
If the value is positive, indicating \ (\ vec {b} \ ) in \ (\ vec {a} \ ) counterclockwise; if the value is negative, explaining \ (\ vec {b} \ ) in \ (\ VEC {a} \) in the clockwise direction.
Calculated polygon area
The basic idea is to divide the polygon into a plurality of triangles, and the area of the cross-product calculation. The idea for the establishment of concave and convex polygons.
Note polygon vertices are arranged counterclockwise \ (P_ {0}, \ P_ {1}, \ .., \ P_ {n - 1} \)
For ease of calculation, select the coordinate origin \ (O (0, \ 0 ) \) as the source point, individually calculating \ (\ VEC {OP_ {I}} \ Times \ VEC {OP_ {I +. 1}} \) , total sum can be.
The formula is
\[ \sum_{i = 0}^{n - 1}(x_{i}y_{i + 1} - x_{i + 1}y_{i}) \]
When \ (i = n - 1 \ ) , the next coordinates to return to \ ((X_ {0}, Y_ {0}) \) , where Japanese sentence can also be a modulo operation.
Complexity \ (O_ {n} \)
Since the area of the cross product having direction, the excess area will be offset, so this algorithm is correct, strict proof is omitted here.
\(code\)
int n;
struct Cor
{
int x, y;
}cor[105];
inline int read()
{
char c = getchar();
int ans = 0, f = 1;
while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();}
while(isdigit(c)) {ans = ans * 10 + c - '0'; c = getchar();}
return ans * f;
}
int main()
{
while(scanf("%d", &n) && n) {
for(int i = 0; i < n; ++i)
cor[i].x = read(), cor[i].y = read();
double ans = 0.0;
for(int i = 0; i < n; ++i){
ans += 0.5 * (cor[i % n].x * cor[(i + 1) % n].y - cor[i % n].y * cor[(i + 1) % n].x);
}
printf("%.1f\n", ans);
}
return 0;
}