zoukankan      html  css  js  c++  java
  • 极角排序

    /*
    名词解释:
    在平面内取一个定点O,引一条射线OX,叫作极轴,再选定一个一个长度单位和角度的正方向(通常取逆时针)
    对于平面内任何任何一点M,用r表示线段OM的长度,θ表示从Ox到OM的角度,ρ叫做点M的极径θ叫做点M的极角,有序数对 (ρ,θ)就叫点M的极坐标
    
    */
    
    //四种极角排序代码详解
    #include<complex>
    struct point
    {
        double x,y;
    };
    
    double cross(double x1,double y1,double x2,double y2) //计算叉积
    {
        return (x1*y2-x2*y1);
    }
    double compare(point a,point b,point c)
    {
        return cross((b.x-a.x),(b.y-a.y),(c.x-a.x),(c.y-a.y));
    }
    //1.利用complex类按极角从小到大排序
    bool com0(const point&a,const point&b) //利用complex类按极角从小到大排序
    {
        complex<double>c1(a.x,a.y);//头文件#include<complex>
        complex<double>c2(b.x,b.y);
        if(arg(c1)==arg(c2))
            return a.x<b.x;
        return arg(c1)<arg(c2);
    }
    
    //2.利用atan2()函数按极角从小到大排序
    bool cmp1(point a,point b)//利用atan2()函数按极角从小到大排序
    {
        if(atan2(a.y,a.x)!=atan2(b.y,b.x))
            return atan2(a.y,a.x)<atan2(b.y,b.x);
        else return a.x<b.x;
    
    }
    
    //利用叉积按极角从小到大排序
    bool cmp2(point a,point b)//利用叉积按极角从小到大排序
    {
        point c;//原点
        c.x=0;
        c.y=0;
        if(compare(c,a,b)==0)
            return a.x<b.x;
        else return compare(c,a,b)>0;
    }
    
    //4.先按象限从小到大排序,再按极角从小到大排序
    
    int quadrant(point a)//象限排序
    {
        if(a.x>0&&a.y>=0)return 1;
        if(a.x<=0&&a.y>0)return 2;
        if(a.x<0&&a.y<=0)return 3;
        if(a.x>=0&&a.y>0)return 4;
    }
    bool cmp3(point a,point b)//先按象限从小到大排序 再按极角从小到大排序
    {
        if(quadrant(a)==quadrant(b))
            return cmp1(a,b);
        else quadrant(a)<quadrant(b);
    }


    Scrambled Polygon
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 9216   Accepted: 4369

    Description

    A closed polygon is a figure bounded by a finite number of line segments. The intersections of the bounding line segments are called the vertices of the polygon. When one starts at any vertex of a closed polygon and traverses each bounding line segment exactly once, one comes back to the starting vertex. 

    A closed polygon is called convex if the line segment joining any two points of the polygon lies in the polygon. Figure 1 shows a closed polygon which is convex and one which is not convex. (Informally, a closed polygon is convex if its border doesn't have any "dents".) 

    The subject of this problem is a closed convex polygon in the coordinate plane, one of whose vertices is the origin (x = 0, y = 0). Figure 2 shows an example. Such a polygon will have two properties significant for this problem. 

    The first property is that the vertices of the polygon will be confined to three or fewer of the four quadrants of the coordinate plane. In the example shown in Figure 2, none of the vertices are in the second quadrant (where x < 0, y > 0). 

    To describe the second property, suppose you "take a trip" around the polygon: start at (0, 0), visit all other vertices exactly once, and arrive at (0, 0). As you visit each vertex (other than (0, 0)), draw the diagonal that connects the current vertex with (0, 0), and calculate the slope of this diagonal. Then, within each quadrant, the slopes of these diagonals will form a decreasing or increasing sequence of numbers, i.e., they will be sorted. Figure 3 illustrates this point. 
     

    Input

    The input lists the vertices of a closed convex polygon in the plane. The number of lines in the input will be at least three but no more than 50. Each line contains the x and y coordinates of one vertex. Each x and y coordinate is an integer in the range -999..999. The vertex on the first line of the input file will be the origin, i.e., x = 0 and y = 0. Otherwise, the vertices may be in a scrambled order. Except for the origin, no vertex will be on the x-axis or the y-axis. No three vertices are colinear.

    Output

    The output lists the vertices of the given polygon, one vertex per line. Each vertex from the input appears exactly once in the output. The origin (0,0) is the vertex on the first line of the output. The order of vertices in the output will determine a trip taken along the polygon's border, in the counterclockwise direction. The output format for each vertex is (x,y) as shown below.

    Sample Input

    0 0
    70 -50
    60 30
    -30 -50
    80 20
    50 -60
    90 -20
    -30 -40
    -10 -60
    90 10
    

    Sample Output

    (0,0)
    (-30,-40)
    (-30,-50)
    (-10,-60)
    (50,-60)
    (70,-50)
    (90,-20)
    (90,10)
    (80,20)
    (60,30)
    

    Source

    [Submit]   [Go Back]   [Status]   [Discuss]


    #include<iostream>
    #include<cmath>
    #include<stdio.h>
    #include<complex>
    #include<algorithm>
    #define max(a,b) (a)>(b)?(a):(b)
    #define min(a,b) (a)<(b)?(a):(b)
    #define EPS 1e-8
    using namespace std;
    struct point {
        double x,y;    
    };
    point convex[50];
    
    double cross(const point &p1, const point &p2, const point &q1, const point &q2)
    {
        return (q2.y - q1.y)*(p2.x - p1.x) - (q2.x - q1.x)*(p2.y - p1.y);    
    }
    
    bool cmp(const point &a, const point &b)
    {
        point origin;
        origin.x = origin.y = 0;
        return cross(origin,b,origin,a) < 0;
    }
    
    
    int main()
    {
        int cnt = 0;
        while (scanf("%lf%lf",&convex[cnt].x,&convex[cnt].y) != EOF) {
            ++cnt;
        }
        sort(convex+1,convex+cnt,cmp);
        for (int i(0); i<cnt; ++i) {
            cout<<"("<<convex[i].x<<","<<convex[i].y<<")"<<endl;    
        }
        return 0;    
    }


  • 相关阅读:
    归档模式与非归档模式之间的转换
    Cognos 常见错误解决办法
    Informatica 错误信息解决办法汇总
    转摘:解决动态列固定行月季年组合式报表
    数据库名、实例名、全局数据库名、服务名的区别
    转摘:维度与指标混合嵌入(合计)设计
    WinForm使用XML动态加载多语言
    windows中最重要的三个动态链接库及功能
    什么是.Net的异步机制(Invoke,BeginInvoke,EndInvoke) step 2
    深入理解.net服务器控件
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9387385.html
Copyright © 2011-2022 走看看