point.h 2.0 KB

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