14276 - Geometry II   

Description

In the homework, you have learned the basic operations and simple applications of vectors. Now, let's do a more powerful function:
Given the vertices of a polygon in order (may be clockwise or counterclockwise), please calculate its area.

The provided header file is very similar to the one used in the homework. However, you can choose not to implement some of the functions if you think they are not needed. The member functions of Vector are as follows:

  • Vector operator+(const Vector &rhs) const: Add two vectors.
  • Vector operator-(const Vector &rhs) const: Subtract self vector by another vector.
  • double operator*(const Vector &rhs) const: Calculate the dot product of two vectors.
  • double operator^(const Vector &rhs) const: Calculate the cross product of two vectors.
  • double area(const Vector &rhs) const: Calculate the area of the triangle formed by two vectors.
  • Vector projection(const Vector &rhs) const: Calculate the projection of self vector on another vector.

The only function you need to implement is double area(Vector v[], int n). It should return the area of the polygon whose vertices are stored in the array v[]. The vertices are given in (counter)clockwise order, and the last vertex is connected to the first vertex. The parameter n is the number of vertices.

Warning: You should use C++11 or later versions, or else you will get Compile Error! reason

Example

For the sample input, the polygon should look like this.

Input

The first line contains an integers $n$, which represents the number of points.
Each of the next $n$ lines contains two integers $x_i, y_i$, represent the point's position.

$n$
$x_1 \quad y_1$
$x_2 \quad y_2$
$\vdots$
$x_n \quad y_n$

Constraints

  • $-10^6 \le x_i, y_i \le 10^6$
  • For testcase 1, 2, 5, 6, it's guaranteed that the origin $(0, 0)$ is inside the polygon.
  • For testcase 1, 2, 3, 4, $3 \le n \le 10^3$
  • For testcase 5, 6, 7, 8, $4 \le n \le 10^5$

Output

Print the area of the polygon, whose vertices are the input points.

Sample Input  Download

Sample Output  Download

Partial Judge Code

14276.cpp

Partial Judge Header

14276.h

Tags




Discuss