当前位置:编程学习 > C/C++ >>

poj 2007 Scrambled Polygon(凸多边形顶点输出)

描述:从(0,0)点开始输入一个凸多边形,这个凸多边形,占有三个象限,按照逆时针的方式输出各定点。

 

 

\

 

\

 

 

 

输出例子:

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)
 

思路:从上图可以看出各象限都是斜率递增方式,建立4个vector对应四个象限,然后分别将各象限的点存储到相应的vector,最后对vector中的数据排序输出。(思路比较水)

 

代码:

[html] 
#include<iostream> 
#include<cstdio> 
#include<cmath> 
#include<vector> 
#include<algorithm> 
using namespace std; 
 
struct Vertice{ 
    int x,y; 
}; 
 
bool operator<(const Vertice &a, const Vertice &b) 
{     
    return atan2((double)a.y, (double)a.x)<atan2((double)b.y, (double)b.x); 

 
int main() 

    Vertice point; 
    vector<Vertice> ivec[4]; 
    int i=0; 
    while(scanf("%d%d", &point.x, &point.y)!= EOF){ 
        //while(i++<10){ 
        //scanf("%d%d", &point.x, &point.y); 
        if(point.x>0&&point.y>0) 
            ivec[0].push_back(point); 
        else if(point.x<0&&point.y>0) 
            ivec[1].push_back(point); 
        else if(point.x<0&&point.y<0) 
            ivec[2].push_back(point); 
        else if(point.x>0&&point.y<0) 
            ivec[3].push_back(point); 
    } 
    for(int i=0;i<4;i++){ 
        if(ivec[i].size()!=0){ 
            sort(ivec[i].begin(),ivec[i].end()); 
        } 
    } 
 
    cout<<"(0,0)"<<endl; 
    int begin; 
    for(int i=0;i<4;i++){//找出凸边形的起始点 
        if(ivec[i].size()==0){ 
            begin = (i+1)%4; 
            break; 
        } 
 
    } 
 
    for(int i=0;i<3;i++){ 
        int t = (begin+i)%4; 
        if(ivec[t].size()!=0){ 
            vector<Vertice>::iterator it; 
            for(it=ivec[t].begin();it!=ivec[t].end();it++){ 
                cout<<"("<<it->x<<","<<it->y<<")"<<endl; 
            } 
        } 
    } 
 
    system("pause"); 
    return 0; 

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;

struct Vertice{
 int x,y;
};

bool operator<(const Vertice &a, const Vertice &b)
{   
 return atan2((double)a.y, (double)a.x)<atan2((double)b.y, (double)b.x);
}

int main()
{
 Vertice point;
 vector<Vertice> ivec[4];
 int i=0;
 while(scanf("%d%d", &point.x, &point.y)!= EOF){
  //while(i++<10){
  //scanf("%d%d", &point.x, &point.y);
  if(point.x>0&&point.y>0)
   ivec[0].push_back(point);
  else if(point.x<0&&point.y>0)
   ivec[1].push_back(point);
  else if(point.x<0&&point.y<0)
   ivec[2].push_back(point);
  else if(point.x>0&&point.y<0)
   ivec[3].push_back(point);
 }
 for(int i=0;i<4;i++){
  if(ivec[i].size()!=0){
   sort(ivec[i].begin(),ivec[i].end());
  }
 }

 cout<<"(0,0)"<<endl;
 int begin;
 for(int i=0;i<4;i++){//找出凸边形的起始点
  if(ivec[i].size()==0){
   begin = (i+1)%4;
   break;
  }

 }

 for(int i=0;i<3;i++){
  int t = (begin+i)%4;
  if(ivec[t].size()!=0){
   vector<Vertice>::iterator it;
   for(it=ivec[t].begin();it!=ivec[t].end();it++){
    cout<<"("<<it->x<<","<<it->y<<")"<<endl;
   }
  }
 }

 system("pause");
 return 0;
}
 

 

[html] 
View Code  
 
#include <iostream> 
#include <cstdlib> 
#include <cstring> 
#include <cstdio> 
#include <cmath> 
#include <algorithm> 
using namespace std; 
 
#define maxn 55 
#define pi acos(-1) 
 
struct Point 

    int x, y; 
} point[maxn]; 
 
bool operator <(const Point &a, const Point &b) 

    return atan2(a.y, a.x) < atan2(b.y, b.x); 

 
double cal(double a) 

    if (a < 0) 
        return a + 2 * pi; 
    return a; 

 
int main() 

    //freopen("t.txt", "r", stdin); 
    scanf("%d%d", &

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,