Integer Points and Pick Theorem

Source: Internet
Author: User

 

PickThe area of the polygon whose vertex is an integer is S, the Integer Points inside the polygon are N, and the Integer Points on the polygon boundary are L

N + 1/2-1 = S.

The Calculation of N and L is given by the following program:

Typedef struct Point

{

Int x, y;

} POINT;

Int gcd (int a, int B) // calculate the maximum public factor of a and B

{

If (B = 0) return;

Else return gcd (B, a % B );

}

The number of points on the side of a polygon is given in the following sections:

Int OnEdge (int n, POINT * p)

{

Int I, ret = 0;

For (I = 0; I <n; I ++)

Ret + = gcd (fabs (p [I]. x-p [(I + 1) % n]. x), fabs (p [I]. y-p [(I + 1) % n]. y ));

Return ret;

}

The number of mesh points inside a polygon is given in the following section:

Int InSide (int n, POINT * p)

{

Int I, area = 0;

For (I = 0; I <n; I ++) area + = p [(I + 1) % n]. y * (p [I]. x-p [(I + 2) % n]. x); // calculated area

Return (fabs (area)-OnEdge (n, p)/2 + 1;

}

 

Problem description

The grid point is an ordered (x, y), where x and y are integers. Given the vertex coordinates of a triangle (which happens to be a grid point), you need to calculate the number of vertices completely in the triangle (the vertex on the triangle edge and the triangle vertex do not have to be calculated ).

Input

Multiple groups of test data are input. Each group of test data is composed of six integers x1, y1, x2, y2, x3, and y3, including (x1, y1), (x2, y2), and (x3, y3) is the vertex coordinate of a triangle. All triangles in the input are non-degraded (positive area),-15000 ≤ x1, y1, x2, y2, x3, y3 ≤ 15000. When the input number is x1 = y1 = x2 = y2 = x3 = y3 = 0, it indicates that the input is over and does not have to be processed.

Output

For each group of test data, the number of points inside the triangle is output on a single row.

Input sample output sample

0 0 1 0 0 1 0

0 0 5 0 5 6

0 0 0 0 0 0

 

Analysis

The Pick theorem can be used directly in this question: area = OnEdge/2 + InSide-1, where area is the area of the vertices polygon, and OnEdge is the number of cells on the polygon, inSide is the number of points InSide the polygon.

The area of a polygon can be calculated using the cross product. Note that the area may be negative and must be converted. Two grid points A (x0, y0) and B (x1, y1) are given ). Set C (X, Y) to a node on line AB. So, x = x0 + Lambda (x1-x0), y = y0 + Lambda (y1-y0), (0 ≤ λ ≤ 1 ). To make x and y are integers, λ must be a fraction, and the denominator of λ is the public factor of x1-x0 and y1-y0, so it can be obtained by the maximum public factor algorithm gcd.

 

Reference Program

 

 

# Include <cmath>

# Include <iostream>

# Include <algorithm>

Using namespace std;

Typedef struct Point

{

Int x, y;

} POINT;

Int gcd (int a, int B)

{

If (B = 0) return;

Else return gcd (B, a % B );

}

Int Int_area (POINT a, POINT B, POINT c) // parallelogram Area

{

Return (B. x-a.x) * (c. y-a.y)-(B. y-a.y) * (c. x-a.x );

}

Int edgenum (POINT a, POINT B)

{

Int dx, dy;

Dx = a. x-b.x;

If (dx <0) dx =-dx;

Dy = a. y-b.y;

If (dy <0) dy =-dy;

Return gcd (dx, dy );

}

Int main ()

{

POINT a, B, c;

Int area, OnEdge, InSide;

While (cin>. x>. y> B. x> B. y> c. x> c. y & (. x |. y | B. x | B. y | c. x | c. y ))

{

Area = Int_area (a, B, c );

If (area <0) area =-area;

OnEdge = edgenum (a, B) + edgenum (B, c) + edgenum (c, );

InSide = (area-OnEdge + 2)/2; // applies the Pick theorem. area is twice the area of a triangle.

Cout <InSide <endl;

}

Return 0;

}

 

Walk in the cloud

Related Article

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.