Pick Theorem

#include<bits/stdc++.h>
#define ll long long

struct PickTheorem 
{
    vector< pair<ll, ll> > vertex;
    int n_vertices;
    
    PickTheorem (vector< pair<ll, ll> > &vertex_) {
        vertex = vertex_;
        n_vertices = vertex.size();
        vertex.push_back(vertex[0]);
    }
    
    double getArea() {
        double area = 0;
        
        for (int i = 0; i < n_vertices; ++i) {
            area += vertex[i].first * vertex[i+1].second - vertex[i].second * vertex[i+1].first;
        }
        
        area = 1./2. * fabs(area);
        return area;
    }
    
    ll getPointsInEdges() {
        ll n_point = 0;
        
        for (int i = 0; i < n_vertices; ++i) {
            n_point += abs(__gcd(vertex[i].first - vertex[i+1].first, vertex[i].second - vertex[i+1].second));
        }
        
        return n_point;
    }
    
    ll getPointsInside() {
        // S = I + B/2 -1
        ll n_point = 0;
        double area = getArea();
        ll pointsInBoundary = getPointsInEdges();
        
        ll pointsInside = (area - pointsInBoundary / 2. + 1) + 0.5;
        
        return pointsInside;
    }
};

Leave a Reply