Poj 1265 area pick Formula

Source: Internet
Author: User

Calculation of geometric poj pick formula cross-product Polygon Area

Returns the contour of a polygon (given in the form of an edge vector. how many points are in this graph? 2. number of points in the graph. the area of the image.


Idea: first, define the pick formula:

The formula does not mean that we can determine the area of the polygon. First, we do not need to use the pick formula to find the area. Second, we cannot find the whole point in the middle of the polygon. However, the area can be obtained by using the cross product, and the entire point on the side of the polygon can be obtained by using GCD. After a slight deformation, we can solve the number of integral points in the middle of the polygon.


Code (C ++ AC, G ++ wa ):


#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;struct Point{int x,y;Point(int _ = 0,int __ = 0):x(_),y(__) {}Point operator +(const Point &a)const {return Point(x + a.x,y + a.y);}};int cases;int points;inline int Cross(const Point &a,const Point &b){return a.x * b.y - a.y * b.x;}int GCD(int x,int y){return y ? GCD(y,x % y):x;}int main(){for(cin >> cases; cases; --cases) {scanf("%d",&points);int on_side = 0,area = 0;Point now,dir,_next;for(int i = 1; i <= points; ++i) {scanf("%d%d",&dir.x,&dir.y);on_side += GCD(abs(dir.x),abs(dir.y));_next = (now + dir);area += Cross(now,_next);now = _next;}area = abs(area);static int T = 0;printf("Scenario #%d:\n",++T);printf("%d %d %d.%d\n\n",(area + 2 - on_side) / 2,on_side,area >> 1,((area&1) ? (5):(0)));}return 0;}


Poj 1265 area pick Formula

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.