eoj1127 calculating the area of a geometric freeform polygon

Source: Internet
Author: User

Title: eoj1127

"The Spring breeze blows everywhere,
The algorithm does not matter;
I really can't go back to my hometown
There is another acre of land.
Thank you! (band playing) "

Saying that some students are very good mentality, every day to know the game, the exam is so simple topic, but also foggy, but also to such a few limerick.
Well, the teacher's responsibility is to help you solve the problem, since you want to farm, it will be a piece of you.
This is a polygonal shape of the field, originally Partychen, now ready to give you. However, nothing is so simple, you must first tell me how much area the land is, and if the answer is correct, you can really get the land.
Are you worried? is to let you know, farming is also need algorithmic knowledge! After the good practice it ...

Input

The input data contains multiple test instances, one row per test instance, and the beginning of each line is an integer n (3<=n<=100), which represents the number of sides of the polygon (and, of course, the number of vertices), followed by the coordinates of the n vertices given in a counterclockwise order (x1, y1, x2, Y2...xn, Yn), in order to simplify the problem, all coordinates here are represented by integers.
All integers in the input data are within a 32-bit integer range, and n=0 represents the end of the data and does not handle it.

Output

For each test instance, output the corresponding polygon area, and the result is exactly one decimal point after the decimal.
The output for each instance takes up one row.

Sample Input

3 0 0 1 0 0 1
4 1 0 0 1-1 0 0-1
0

Sample Output

0.5
2.0

Topic Analysis:

Calculates the area of a freeform polygon. Tool: Vector fork multiplication. The Vector fork is twice times the direction area of the triangle. Therefore, you can select any point, such as the origin and the two endpoints of each edge to form a triangle, through the positive and negative direction of the area can be derived from the area of the arbitrary polygon, can do a graphical visual understanding of the theorem.

AC Code:

<span style= "FONT-SIZE:18PX;" > #include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace Std;struct point{//Point Class    double X, y;    Point (int xx=0,int yy=0): X (xx), Y (yy) {}};struct vec{//vector class, which can be combined with vector classes and dot classes. But this distinguishes a better understanding of    double x, y;    Vec () {}    Vec (const point& a,const point& b) {//converts two points into vector        x=a.x-b.x;        y=a.y-b.y;    }    Friend Double operator* (Vec A,vec b) {//overloaded fork multiplication        return a.x*b.y-a.y*b.x;    }} V[105]; int main () {    int n;    Point o={0.0,0.0};    while (~SCANF ("%d", &n), N) {for        (int i=0;i<n;++i) {            double A, b;            scanf ("%lf%lf", &a,&b);            V[i]={point{a,b},o};        }        Double ans=0.0;        for (int i=0;i<n-1;++i) {            ans+=v[i]*v[i+1];        }        ANS+=V[N-1]*V[0];        printf ("%.1f\n", Fabs (0.5*ans));    }    return 0;} </span>



eoj1127 calculating the area of a geometric freeform polygon

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.