poj 2954 Triangle(Pick定理)

链接:http://poj.org/problem?id=2954

Triangle

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5043   Accepted: 2164

Description

lattice point is an ordered pair (xy) where x and y are both integers. Given the coordinates of the vertices of a triangle (which happen to be lattice points), you are to count the number of lattice points which lie completely inside of the triangle (points on the edges or vertices of the triangle do not count).

Input

The input test file will contain multiple test cases. Each input test case consists of six integers x1y1x2y2x3, and y3, where (x1y1), (x2y2), and (x3y3) are the coordinates of vertices of the triangle. All triangles in the input will be non-degenerate (will have positive area), and −15000 ≤ x1y1x2y2x3y3 ≤ 15000. The end-of-file is marked by a test case with x1 =  y1 =x2 = y2 = x3 = y3 = 0 and should not be processed.

Output

For each input case, the program should print the number of internal lattice points on a single line.

Sample Input

0 0 1 0 0 1
0 0 5 0 0 5
0 0 0 0 0 0

Sample Output

0
6

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

继续用pick定理,area=i + b / 2 -1

注意判结束时不可(a+b+c+d)看是否为零,因为有负数

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <math.h>
 5 #include <iostream>
 6 #include <algorithm>
 7
 8 using namespace std;
 9
10 typedef struct
11 {
12     double x,y;
13 }point;
14
15 double crossProduct(point a,point b,point c)
16 {
17     return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
18 }
19
20 int gcd(int a,int b)
21 {
22     return b ? gcd(b,a%b) : a;
23 }
24
25 point p[4];
26
27 int onEdge(int n)
28 {
29     int sum=0;
30     p[n]=p[0];
31     for(int i=0; i<n; i++)
32     {
33         sum+=gcd(abs((int)(p[i].x-p[i+1].x)),abs((int)(p[i].y-p[i+1].y)));
34     }
35     return sum;
36 }
37
38 int main()
39 {
40     while(scanf("%lf%lf%lf%lf%lf%lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y,&p[2].x,&p[2].y)!=EOF
41           && p[0].x!=0||p[0].y!=0||p[1].x!=0||p[1].y!=0||p[2].x!=0||p[2].y!=0)
42     {
43         double area=fabs(crossProduct(p[0],p[1],p[2]))/2.0;
44         int edge=onEdge(3);
45         printf("%d\n",(int)area+1-edge/2);
46     }
47     return 0;
48 }

时间: 2024-10-13 11:18:18

poj 2954 Triangle(Pick定理)的相关文章

POJ 1265 Area POJ 2954 Triangle Pick定理

Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5227   Accepted: 2342 Description Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionage. To protect its brand-new resear

POJ 2954 Triangle (皮克定理, 三角形叉乘求面积)

Triangle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5303 Accepted: 2297 Description A lattice point is an ordered pair (x, y) where x and y are both integers. Given the coordinates of the vertices of a triangle (which happen to be lat

poj 1265 Area (Pick定理+求面积)

链接:http://poj.org/problem?id=1265 Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4969   Accepted: 2231 Description Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionag

poj 1265 Area(pick 定理)

链接:poj 1265 题意:从原点出发.给出一些dx,dy移动增量,终于形成一个多边形, 求多边形内部的格点数目,边上的格点数目 .以及面积. 补充知识: 1.以格子点为顶点的线段.覆盖的点的个数为gcd(|dx|,|dy|).当中,|dx|,|dy|分别为线段横向增量和纵向增量. 2.Pick定理:设平面上以格子点为顶点的多边形的内部点个数为a.边上点个数为b.面积为S,则 S = a + b/2 -1. 3.随意一个多边形的面积等于以多边形边上的某点为固定点.按顺序求其余点相邻两个点与该点

poj 1265 Area Pick定理

题目链接 pick定理: 一个计算点阵中顶点在格点上的多边形面积公式:S=a+b÷2-1,其中a表示多边形内部的点数,b表示多边形边界上的点数,s表示多边形的面积 知道这个这题就没有难度了. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include &

poj 2954 Triangle

pick公式+gcd公式 1 #include<iostream> 2 #include<map> 3 #include<string> 4 #include<cstring> 5 #include<cstdio> 6 #include<cstdlib> 7 #include<cmath> 8 #include<queue> 9 #include<vector> 10 #include<alg

Area - POJ 1265(pick定理求格点数+求多边形面积)

题目大意:以原点为起点然后每次增加一个x,y的值,求出来最后在多边形边上的点有多少个,内部的点有多少个,多边形的面积是多少. 分析: 1.以格子点为顶点的线段,覆盖的点的个数为GCD(dx,dy),其中,dxdy分别为线段横向占的点数和纵向占的点数.如果dx或dy为0,则覆盖的点数为dy或dx.2.Pick公式:平面上以格子点为顶点的简单多边形的面积=边上的点数/2+内部的点数+1.3.任意一个多边形的面积等于按顺序求相邻两个点与原点组成的向量的叉积之和. 代码如下: -------------

【POJ】Triangle(pick定理)

http://poj.org/problem?id=2954 表示我交了20+次... 为什么呢?因为多组数据我是这样判断的:da=sum{a[i].x+a[i].y},然后!da就表示没有数据了QAQ我居然查了如此久都没查出来!!!!注意负数啊负数啊啊啊啊啊啊啊 本题是pick定理:当多边形的顶点均为整数时,面积=内部整点+边上整点/2-1 然后本题要求内部整点 #include <cstdio> #include <cstring> #include <cmath>

【POJ 2954】 Triangle

[POJ 2954] Triangle 很涨姿势的一道题 涉及三点 1.三角形算法 已知三点 S = (AB X BC)/2 (PS:海伦公式<已知三边> S = sqrt(p(p-a)(p-b)(p-c) p = (a+b+c)/2) 2.已知两点求两点间格点 即为求两点横纵坐标差的最大公倍数(-1-–不包含顶点) (gcd( abs(x2-x1),abs(y2-y1) ) (-1)) 3.格点多边形的面积 s = d/2+t-1(d->多边形边界的格点数 t->多边形内部格点数