point.h 1.9 KB

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