Advertisement
karbaev

point-to-line

Mar 8th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. //расстояние от точки до прямой
  2. #include <CMATH>
  3.  
  4. void getLine(double x1, double y1, double x2, double y2, double &a, double &b, double &c)
  5. {
  6.     // (x- p1X) / (p2X - p1X) = (y - p1Y) / (p2Y - p1Y)
  7.     a = y2 - y1;
  8.     b = x2 - x1;
  9.     c = x1 * y2 - x2 * y1;
  10. }
  11.  
  12. double dist(double pct1X, double pct1Y, double pct2X, double pct2Y, double pct3X, double pct3Y)
  13. {
  14.     double a, b, c;
  15.     getLine(pct2X, pct2Y, pct3X, pct3Y, a, b, c);
  16.     return abs(a * pct1X + b * pct1Y + c) / sqrt(a * a + b * b);
  17. }
  18.  
  19. bool isPointOnLine (double x, double y, double x1, double y1, double x2, double y2) // returns true if point on the line
  20.     {
  21.     double vx = x2 - x1;
  22.     double vy = y2 - y1;
  23.     double vcx = x - x1;
  24.     double vcy = y - y1;
  25.     //если вектора параллельны (лежат на одной прямой)
  26.     if (vx * vcy == vy * vcx) return true;
  27.     return false;
  28.     }
  29.  
  30.  
  31. int main(int argc, char* argv[])
  32. {
  33.     double d = dist(1,2,3,4,5,6);
  34.  
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement