驗證多邊形是否爲凸多邊形

驗證多邊形是否爲凸多邊形

 

2108 Shape of HDU

#include <stdio.h>
#define _DEBUG 0

int crossMulti(int x0,int y0,int x1,int y1,int x2,int y2){//差積
 return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);
}
int main(){
 int n,i;
 int f_x,f_y;//第1個點
 int s_x,s_y;//第2個點
 int p0_x,p0_y;//上2個點
 int p1_x,p1_y;//上一個點
 int cur_x,cur_y;//當前點
 double area = 0;
#if _DEBUG == 12108 Shape of HDU 
 freopen("2108.in","r",stdin);
#endif
 while(scanf("%d",&n) && n != 0){  
  scanf("%d %d",&f_x,&f_y);
  scanf("%d %d",&s_x,&s_y);
  p0_x = f_x;p0_y = f_y;
  p1_x = s_x;p1_y = s_y;
  bool flag = true;
  for(i=2;i<n;++i){
   scanf("%d %d",&cur_x,&cur_y);
   if(crossMulti(p0_x,p0_y,p1_x,p1_y,cur_x,cur_y) < 0){
    flag = false;
    if(i != n-1){//取完本行數據,注意
     char tmp[100];
     gets(tmp);
    }
    break;
   } 
   p0_x = p1_x; p0_y = p1_y; 
   p1_x = cur_x; p1_y = cur_y;
  }  
  if(crossMulti(p0_x,p0_y,p1_x,p1_y,f_x,f_y) < 0 
   || crossMulti(p1_x,p1_y,f_x,f_y,s_x,s_y) <0){//第1、2個點的,特別要注意
   flag = false;
  }  
  if(flag){
   printf("convex\n");
  }else
   printf("concave\n");
 }
 return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章