Polygon Area Calculation method [simple polygon] [C ++]

Source: Internet
Author: User

# Include <iostream>
# Include <cmath>
# Include <windows. h>

Const int num = 4;
// The sides of a polygon cannot be intersecting. Otherwise, the calculation will fail.
// If the area is positive, the front of the multiple sides can be determined. If the area is negative, it indicates the back of the polygon.
Double getarea (point * Ppoint, int nnum)
{
Double S = 0;
For (INT I = 0; I <nNum-1; I ++)
S + = Ppoint [I]. x * Ppoint [I + 1]. Y-Ppoint [I + 1]. x * Ppoint [I]. Y;
S + = Ppoint [nNum-1]. x * Ppoint [0]. Y-Ppoint [0]. x * Ppoint [nNum-1]. Y;
S/= 2.0;
Return S;
}

Int main ()
{
Point Points [num] = {
{0, 0 },
{5, 10 },
{20, 10 },
{15, 5 },
};
Double SS;
Ss = FABS (getarea (points, num ));
STD: cout <SS <STD: Endl;
Return 0;
}

It's okay to use a few simple points for computation. You can use a pen to verify a simple situation.

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.