pku 2954(pick定理的应用)

/*  

*  pick定理的应用

*  pick定律: area = inside + onedge / 2 - 1; 

*/



#include <cmath>

#include <cstdio>

#include <cstdlib>

#include <iostream>



using namespace std;



struct point {

    int x;

    int y;

}A, B, C;



int crossProd(point A, point B, point C) {

    return (B.x-A.x)*(C.y-A.y) - (B.y-A.y)*(C.x-A.x);

} 



int gcd(int a, int b) {

    return b ? gcd(b, a%b) : a;

}



int edgeNum(point A, point B) {

    int x = A.x - B.x;

    if (x < 0) x = 0 - x;

    int y = A.y - B.y;

    if (y < 0) y = 0 - y;

    return gcd(x, y);

}



int main() {

    while (scanf("%d%d%d%d%d%d", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y)) {

        if (A.x==0 && A.y==0 && B.x==0 && B.y==0 && C.x==0 && C.y==0) break;

        int area = crossProd(A, B, C) / 2; //求面积 

        if (area < 0) area = 0 - area;

        int onEdge = 0;

        onEdge = edgeNum(A, B) + edgeNum(B, C) + edgeNum(C, A);//求边上的整点数 

        int inside = area - onEdge / 2 + 1; //pick定律 

        printf ("%d\n", inside);

    }

    return 0;

}

 

你可能感兴趣的:(pku)