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 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
x1, y1, x2, y2,
x3, and y3, where (x1,
y1), (x2, y2), and (x3,
y3) are the coordinates of vertices of the triangle. All triangles in the input will be non-degenerate (will have positive area), and ?15000 ≤
x1, y1, x2, y2,
x3, y3 ≤ 15000. The end-of-file is marked by a test case with
x1y1 = 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

Source

Stanford Local 2004

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

题目大意:给出三个点,求这三点组成的三角形中整数坐标点得个数

题目分析:利用皮克定理,S=a+b/2-1,其中a表示多边形内部的点数,b表示多边形边界上的点数,s表示多边形的面积

则a = S + 1 - b/2

还有两个常用公式:

1.给出三点(x1, y1) (x2, y2) (x3, y3)求三角形面积,用海伦公式太麻烦,现给出叉乘公式 S = abs(((x2-x1) * (y3-y1) - (x3-x1) * (y2-y1)) / 2),abs为取绝对值

2.给出两点(x1, y1) (x2, y2),求这两点组成得线段间整数坐标点得个数 b = gcd(abs(x1 - x2),abs(y1 - y2)),gcd为最大公约数

有了这两个公式这天就好解决了,本题还有一个问题就是输入不能已(x1 + y1 + x2 + y2 + x3 + y3) != 0判断,例如1 1 -2 3 4 -7答案是8

#include <cstdio>
#include <cmath>

int abs(int x)
{
    return x > 0 ? x : -x;
}

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

int main()
{
    int x1, y1, x2, y2, x3, y3;
    while(scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3) != EOF)
    {
        if(x1 == 0 && y1 == 0 && x2 == 0 && y2 == 0 && x3 == 0 && y3 == 0)
            break;
        int x12 = abs(x1 - x2);
        int y12 = abs(y1 - y2);
        int x13 = abs(x1 - x3);
        int y13 = abs(y1 - y3);
        int x23 = abs(x2 - x3);
        int y23 = abs(y2 - y3);
        printf("%d\n", abs(((x2-x1) * (y3-y1) - (x3-x1) * (y2-y1)) / 2) + 1 - (gcd(x12,y12) + gcd(x13,y13) + gcd(x23,y23)) / 2);
    }
}
时间: 2024-10-07 07:44:28

POJ 2954 Triangle (皮克定理, 三角形叉乘求面积)的相关文章

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(Pick定理)

链接:http://poj.org/problem?id=2954 Triangle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5043   Accepted: 2164 Description A lattice point is an ordered pair (x, y) where x and y are both integers. Given the coordinates of the vertices

POJ 1265-Area(计算几何+皮克定理+多边形面积公式)

题目地址:POJ 1265 题意:给定一个格点多边形,求出内部点数in,边上点数on,和面积S. 思路:运用的定理很多. 1.皮克定理:S=in+on/2-1,即in=(2*S+2-on)/2. 2.多边形的面积公式:按顺序求相邻两个点与原点组成的向量的叉积之和. 3.求边上的格点数:以格子点为顶点的线段,覆盖的点的个数为GCD(dx,dy),其中,dxdy分别为线段横向占的点数和纵向占的点数.如果dx或dy为0,则覆盖的点数为dy或dx. #include <stdio.h> #includ

POJ 2954-Triangle(计算几何+皮克定理)

职务地址:POJ 2954 意甲冠军:三个顶点的三角形,给出,内部需求格点数. 思考:就像POJ 1265. #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <sstream> #include <algorithm> #include <set>

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

POJ 2954 /// 皮克定理+叉积求三角形面积

题目大意: 给定三角形的三点坐标 判断在其内部包含多少个整点 题解及讲解 皮克定理 多边形面积s = 其内部整点in + 其边上整点li / 2 - 1 那么求内部整点就是 in = s + 1 - li / 2 网格中两格点(整点)间经过的格点(整点)数 即边上整点 li +1=两点横向和纵向距离的最大公约数 //求线段ab之间的整点数 int lineSeg(P a,P b) { int dx=abs(a.x-b.x), dy=abs(a.y-b.y); if(dx==0 && dy=

poj1265&amp;&amp;2954 [皮克定理 格点多边形]【学习笔记】

Q:皮克定理这种一句话的东西为什么还要写学习笔记啊? A:多好玩啊... PS:除了蓝色字体之外都是废话啊...  Part I 1.顶点全在格点上的多边形叫做格点多边形(坐标全是整数) 2.维基百科 Given a simple polygon constructed on a grid of equal-distanced points (i.e., points with integer coordinates) such that all the polygon's vertices a

【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->多边形内部格点数

POJ 2954

PICK定理:格子上的多边形面积=边界上格子点数/2+内部点数-1. 利用叉积求出面积.再枚举边上的点数.然后按公式求出内部点数就可以了. 关于PICK:http://blog.csdn.net/i_fuqiang/article/details/9817343 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm>