point.h 2.3 KB

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