ACM-幾何学的なScarambed Polygonを計算します.


Scambed Polygon
タイトル:http://poj.org/problem?id=2007
Time Limit:1000 MSメモリLimit:30000 K
Total Submissions:6513 Acctepted:3092
Description
A closed polygon is a figre bounded by a finite number of line segments.The intersections of the bounding line segments are the vertices of the polygon.When starts the any vertex of the closed borted bodble.
A closed polygon is caled convex if the line segment jining any two points of the polygon lies in the polygon.Figre 1 shows a closed polygon which is convex.(Informally,a boosed polygoods)
ACM-计算几何之Scrambled Polygon——poj2007_第1张图片
The subject of this problem is a closed convex polygon in the coordinate plne,one of whose vertices is the origgin(x=0,y=0)Figre 2 shows an example.Such a polygon will have two properties sigfinicant profinicant.proble.com.
The first property is that the vertices of the polygon will be confined to three or fewer of the four quadrand of the coordinane.In the example shown in Figure 2,none of the vertices are the the seconded quadher.
To describe the second property、suppose you「Tae a trip」around the polygon:start at(0,0)、visit all other vertices exactlyonece、and arrive at(0,0).As You visisieach vertex(heotatototototototototototototototototototototototototototos,0)、thethethethetheaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaattttttttttttttttttthis diagonal.The n,within each quadrand,the slaopes of these diagonals will form a decrease or increase sequence of numbers,i.e.they will be sorted.Figure 3 illustraes this point.
ACM-计算几何之Scrambled Polygon——poj2007_第2张图片
ACM-计算几何之Scrambled Polygon——poj2007_第3张图片
Input
The input lists the vertices of a closed convex polygon in the plinine.The number of ininininininputwill be a least three but no more than 50.Each line contains the x and y coordidinast of one thethe the the the thethe the ininininineeeex.Eacx.Eacx.Each thethethethethethethethethethethethethethethethethethethethe thethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethe inininininininininininininininini.e.x=0 and y=0.Otherwise,the vertices may be in a scrambed order.Except for the origgin,no vertex will be on the x-axis.No three vertices ar.
Output
The output lists the vertices of the given polygon、one vertex per line.Each vertex from the input appars exactly once in the output.The orgin(0,0)is the the first the the the outfortrader.The.in the counterclockwise direction.The output format for each vertex is(x,y)as shown below.
Sample Input
0
70-50
60
-30-50
80
50-60
90-20
-30-40
-10-60
90
Sample Output
(0,0)
(-30、-40)
(-30、-50)
(-10,-60)
(50、-60)
(70、-50)
(90、-20)
(90,10)
(80,20)
(60,30)
幾何学を計算して,一連の点を与え,極角に沿って並べ替えた.
Graamが凸包を求める前置テーマに該当するはずです.
基礎がしっかりしている.注意入力時は停止と判定します.
精度に注意すればOKです.
極角の並べ替えのいくつかの方法を言ってください.これらは全部人のブログを見ます.
回転:http://www.cnblogs.com/devtang/archive/2012/02/01/2334977.html
1.フォークの正負を利用してcmpを作成します.
double cross(point p0,point p1,point p2)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
// sort    
bool cmp(const point &a, const point &b)//     
{
    point origin;
    //     
    origin.x = origin.y = 0;
    return cross(origin,b,a)<EPS;
}
2.complexの内装関数を利用する.
#include<complex>
#define x real()
#define y imag()
#include<algorithm>
using namespace std;

bool cmp(const Point& p1, const Point& p2)
{
    return arg(p1) < arg(p2);
}
3.arctanを利用して極角の大きさを計算します.(範囲『-180,180』)
bool cmp(const Point& p1, const Point& p2)
{     
    return atan2(p1.y, p1.x) < atan2(p2.y, p2.x);
}
4.象限を利用して極角を加え、叉積する.
bool cmp(const point &a, const point &b)//      ,      ,       
{
    if (a.y == 0 && b.y == 0 && a.x*b.x <= 0)return a.x>b.x;
    if (a.y == 0 && a.x >= 0 && b.y != 0)return true;
    if (b.y == 0 && b.x >= 0 && a.y != 0)return false;
    if (b.y*a.y <= 0)return a.y>b.y;
    point one;
    one.y = one.x = 0;
    return cross(one,a,one,b) > 0 || (cross(one,a,one,b) == 0 && a.x < b.x);    
}
はい、これぐらいです.
/**************************************
***************************************
*        Author:Tree                 *
*From  :http://blog.csdn.net/lttree  *
* Title : Scrambled Polygon           *
*Source: poj 2007                     *
* Hint  :                           *
***************************************
**************************************/
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define EPS 1e-8
struct point
{
    double x,y;
}p[51];

double cross(point p0,point p1,point p2)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
// sort    
bool cmp(const point &a, const point &b)//     
{
    point origin;
    //     
    origin.x = origin.y = 0;
    return cross(origin,b,a)<EPS;
}
int main()
{
    int i,n;
    n=0;
    while( scanf("%lf%lf",&p[n].x,&p[n].y)!=EOF )
    {
        ++n;
    }
    //     ,      
    sort(p+1,p+n,cmp);

    for(i=0;i<n;++i)
        printf("(%.0lf,%.0lf)
",p[i].x,p[i].y); return 0; }