point.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "stdafx.h"
  2. #include "point.h"
  3. #include <math.h>
  4. #include <tuple>
  5. NAMESPACE_POINT_BEGIN (NAMESPACE_POINT)
  6. point::point()
  7. :x(0) ,y(0)
  8. {
  9. }
  10. point point::min_(const point&a,const point&b)
  11. {
  12. return b<a?b:a;
  13. }
  14. point point::max_(const point&a,const point&b)
  15. {
  16. return a<b?b:a;
  17. }
  18. void point::set(const point&p)
  19. {
  20. x=p.x;
  21. y=p.y;
  22. }
  23. void point::set(double x_,double y_)
  24. {
  25. x=x_;
  26. y=y_;
  27. }
  28. void point::swap(point&p)
  29. {
  30. point tmp=*this;
  31. *this=p;
  32. p=tmp;
  33. }
  34. //比较两个点的大小,先比较x,如果x相等,比较y
  35. bool point::operator<(const point&p)const
  36. {
  37. double c=x-p.x;
  38. if(c!=0)
  39. return c<0;
  40. return y-p.y<0;
  41. }
  42. bool point::eq(double l,double r,double deta)
  43. {
  44. return fabs(l-r)<=deta;
  45. }
  46. //如果无解
  47. bool point::empty()const
  48. {
  49. return x==0 && y==0;
  50. }
  51. bool point::invalid() const
  52. {
  53. return (eq(x,0,1e-10) && eq(y,0,1e-10)) || (eq(x,-1000,1e-10) && eq(y,-1000,1e-10));
  54. }
  55. bool point::operator==(const point&r)const
  56. {
  57. return eq(x,r.x,1e-10) && eq(y,r.y,1e-10);
  58. }
  59. double point::dist_direct(const point&o)const
  60. {
  61. double d=dist(o);
  62. return o<*this?-d:d;
  63. }
  64. double point::dist_direct(double x,double y)const
  65. {
  66. return dist_direct(point(x,y));
  67. }
  68. //计算两个点之间的距离
  69. double point::dist(const point&o)const
  70. {
  71. return dist(o.x,o.y);
  72. }
  73. //计算此点距离其他点的距离
  74. double point::dist(double x,double y)const
  75. {
  76. double dx=this->x-x;
  77. double dy=this->y-y;
  78. return sqrt(dx*dx+dy*dy);
  79. }
  80. double point::cos_k(const point&o)const
  81. {
  82. double dx=o.x-x;
  83. double dy=o.y-y;
  84. return dx/sqrt(dx*dx+dy*dy);
  85. }
  86. double point::sin_k(const point&o)const
  87. {
  88. double dx=o.x-x;
  89. double dy=o.y-y;
  90. return dy/sqrt(dx*dx+dy*dy);
  91. }
  92. point point::middle(const point&o)const
  93. {
  94. return point((x+o.x)/2,(y+o.y)/2);
  95. }
  96. std::tuple<double,double,double> point::get_abc(const point&o)const
  97. {
  98. double cos=fabs(cos_k(o));
  99. if(cos==0)
  100. return std::make_tuple(1,0,-x);
  101. if(cos==1)
  102. return std::make_tuple(0,1,-y);
  103. double dx=o.x-x;
  104. double dy=o.y-y;
  105. double k=dy/dx;
  106. double deno=sqrt(k*k+1);
  107. return std::make_tuple(k/deno,-1/deno,(y-k*x)/deno);
  108. //return std::make_tuple(k,-1,y-k*x);
  109. }
  110. NAMESPACE_POINT_END(NAMESPACE_POINT)