point.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #ifndef _POINT_HPP_
  2. #define _POINT_HPP_
  3. #include <math.h>
  4. #include <tuple>
  5. struct point
  6. {
  7. point();
  8. point(double x_,double y_)
  9. :x(x_)
  10. ,y(y_)
  11. {
  12. }
  13. static point min(const point&a,const point&b)
  14. {
  15. return b<a?b:a;
  16. }
  17. static point max(const point&a,const point&b)
  18. {
  19. return a<b?b:a;
  20. }
  21. void set(const point&p)
  22. {
  23. x=p.x;
  24. y=p.y;
  25. }
  26. void set(double,double);
  27. void swap(point&p);
  28. bool operator<(const point&p)const
  29. {
  30. double c=x-p.x;
  31. if(c!=0)
  32. return c<0;
  33. return y-p.y<0;
  34. }
  35. static bool eq(double l,double r,double deta)
  36. {
  37. return fabs(l-r)<=deta;
  38. }
  39. bool empty()const
  40. {
  41. return x==0 && y==0;
  42. }
  43. bool operator==(const point&r)const
  44. {
  45. return x==r.x && y==r.y;
  46. }
  47. double dist_direct(const point&o)const
  48. {
  49. double d=dist(o);
  50. return o<*this?-d:d;
  51. }
  52. //计算this到(x,y)的距离
  53. double dist_direct(double x,double y)const
  54. {
  55. return dist_direct(point(x,y));
  56. }
  57. double dist(const point&o)const
  58. {
  59. return dist(o.x,o.y);
  60. }
  61. //计算this到(x,y)的距离
  62. double dist(double x,double y)const
  63. {
  64. double dx=this->x-x;
  65. double dy=this->y-y;
  66. return sqrt(dx*dx+dy*dy);
  67. }
  68. double cos_k(const point&o)const
  69. {
  70. double dx=o.x-x;
  71. double dy=o.y-y;
  72. return dx/sqrt(dx*dx+dy*dy);
  73. }
  74. double sin_k(const point&o)const
  75. {
  76. double dx=o.x-x;
  77. double dy=o.y-y;
  78. return dy/sqrt(dx*dx+dy*dy);
  79. }
  80. point middle(const point&o)const
  81. {
  82. return point((x+o.x)/2,(y+o.y)/2);
  83. }
  84. std::tuple<double,double,double> get_abc(const point&o)const
  85. {
  86. double cos=fabs(cos_k(o));
  87. if(cos==0)
  88. return std::make_tuple(1,0,-x);
  89. if(cos==1)
  90. return std::make_tuple(0,1,-y);
  91. double dx=o.x-x;
  92. double dy=o.y-y;
  93. double k=dy/dx;
  94. double deno=sqrt(k*k+1);
  95. return std::make_tuple(k/deno,-1/deno,(y-k*x)/deno);
  96. //return std::make_tuple(k,-1,y-k*x);
  97. }
  98. public:
  99. double x,y;
  100. };
  101. #endif