给定平面上的N个点,寻找距离最远的两个点

 转:http://blog.csdn.net/wangyangkobe/article/details/6081975

主要是凸包算法、卡壳算法

http://blog.csdn.net/kaytowin/archive/2010/01/06/5140111.aspx

 

http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html

 

http://www.cnblogs.com/devymex/archive/2010/08/09/1795392.html

 

http://iamhuiam.blog.sohu.com/132378792.html

 

 

 

 

  1. #include "stdafx.h"   
  2. #include <iostream>   
  3. #include <vector>   
  4. #include <stack>   
  5. #include <algorithm>   
  6. #include <iterator>   
  7. #include <cmath>   
  8. using namespace std;  
  9. class Point  
  10. {  
  11. public:  
  12.     Point(){}  
  13.     Point(int m_x, int m_y):x(m_x),y(m_y){}  
  14.     int x;  
  15.     int y;  
  16.     friend ostream& operator<< (ostream &out, const Point &point);  
  17. };  
  18. /************************************************************************/  
  19. /* 函数功能:比较两个点(先以y坐标比较,若y相同按x比较)        */  
  20. /************************************************************************/  
  21. bool Cmp(const Point &left, const Point &right)  
  22. {  
  23.     return ((left.y < right.y) || ((left.y == right.y) && (left.x < right.x)));  
  24. }  
  25. /************************************************************************/  
  26. /* 函数功能:求两个向量的内积                                  */  
  27. /************************************************************************/  
  28. int CrossProduct(const Point &pre, const Point &cur, const Point &next)//pre是上一个点,cur是当前点,next是将要选择的点    
  29. {  
  30.     int x1 = cur.x - pre.x;  
  31.     int y1 = cur.y - pre.y;  
  32.     int x2 = cur.x - next.x;  
  33.     int y2 = cur.y - next.y;  
  34.     return (x1*x2 + y1*y2); //<0是满足凸包的点   
  35. }  
  36. ostream& operator<< (ostream &out, const Point &point)  
  37. {  
  38.     out<<"("<<point.x<<","<<point.y<<")";  
  39.     return out;  
  40. }  
  41. /************************************************************************/  
  42. /* 函数功能:求两点间的距离                                    */  
  43. /************************************************************************/  
  44. int Distance(const Point &point1, const Point &point2)  
  45. {  
  46.     return (point1.x - point2.x)*(point1.x - point2.x) + (point1.y - point2.y)*(point1.y - point2.y);  
  47. }  
  48. /************************************************************************/  
  49. /* 函数功能:获取凸包 
  50.    参数vec存放输入的点,result存放凸包上的点*/  
  51. /************************************************************************/  
  52. void GetConvexHull(vector<Point> vec, vector<Point> &result)  
  53. {  
  54.     sort(vec.begin(), vec.end(), Cmp); //排序   
  55.     int size = vec.size();  
  56.     if(size < 3)  
  57.     {  
  58.         copy(vec.begin(), vec.end(), back_inserter(result));  
  59.     }  
  60.     else  
  61.     {  
  62.         result.push_back(vec.at(0));  
  63.         result.push_back(vec.at(1));  
  64.         result.push_back(vec.at(2));  
  65.         int top = 2;  
  66.         for(int i=3; i<size; i++)  
  67.         {  
  68.             while((top>0) && (CrossProduct(result.at(top-1), result.at(top), vec.at(i)) >= 0))  
  69.             {  
  70.                 result.pop_back();  
  71.                 top--;  
  72.             }  
  73.             result.push_back(vec.at(i));  
  74.             top++;  
  75.         }  
  76.     }  
  77. }  
  78. /************************************************************************/  
  79. /* 函数功能:卡壳算法(我也没搞懂)                             */  
  80. /************************************************************************/  
  81. int RotatingCalipers(vector<Point> vec, int n)  
  82. {  
  83.     int j = 1;  
  84.     int maxLength = 0;//存储最大值   
  85.     vec[n] = vec[0];  
  86.     for(int i = 0; i<n; i++)  
  87.     {  
  88.         while(CrossProduct(vec[i+1], vec[j+1], vec[i]) > CrossProduct(vec[i+1], vec[j], vec[i]))  
  89.             j = (j+1)%n;  
  90.         maxLength = max(maxLength, max(Distance(vec[i], vec[j]), Distance(vec[i+1], vec[j+1])));              
  91.     }  
  92.     return maxLength;   
  93. }  
  94. int main()  
  95. {  
  96.     vector<Point> vec;  
  97.     const int N = 20;  
  98.     for(int i=0; i<N; i++)  
  99.     {  
  100.         vec.push_back(Point(rand()%30, rand()%30));  
  101.     }  
  102.     cout<<"平面上的点:"<<endl;  
  103.     copy(vec.begin(), vec.end(), ostream_iterator<Point>(cout, "/n"));  
  104.     cout<<endl;  
  105.     vector<Point> result;  
  106.     GetConvexHull(vec, result);  
  107.       
  108.     cout<<"凸包上的点:"<<endl;  
  109.     copy(result.begin(), result.end(), ostream_iterator<Point>(cout, " "));  
  110.     cout<<endl;  
  111.     int distace = RotatingCalipers(result, result.size()-1);  
  112.     cout<<sqrt(double(distace))<<endl;  
  113. }  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值