title: Point-Line distance calculation
body: <p>I tryed to write code for calculating point-line distance. I found a lot of calculations on the internet but I am not sure if I understand right. I found some equations for lines and it seems like equations for calculating point-plane distance in 3D. I think it is the same but in 2 dimensional.</p>
  
  <p>I have some experiences with 3D point-plane distance calculations. I calculated parameters <strong>A,B,C,D</strong> from <strong>3 points vec3</strong> (plane definition) and to get distance just apply this equation with <strong>vec3</strong> point <strong>(x,y,z)</strong>.</p>
  
  <p>Plane equation: <strong>Ax + By + Cz = D</strong></p>
  
  <p>Line equation should be: <strong>Ax + By = C</strong></p>
  
  <p>Line equation should work similary by applying equation with <strong>vec2</strong> point <strong>(x,y)</strong>.</p>
  
  <p><strong>My problem is</strong> how to calculate parameters <strong>A,B,C</strong> from this equation with <strong>2 points vec2</strong> (line definition)?</p>
  
  <p><strong>Any simply programming/mathematic explanation?</strong></p>
  
  <p>Plane with 3 points:</p>
  
  <pre><code>Plane(vec3 p0, vec3 p1, vec3 p2) {
      vec3 v = p1 - p0;
      vec3 u = p2 - p0;
      vec3 n = cross(v, u);
  
      normalize(n);
  
      //Result A,B,C,D
      A = n.x;
      B = n.y;
      C = n.z;
      D = dot(vec3(-n.x, -n.y, -n.z), p0);
  }
  </code></pre>
  
  <p>Plane with 2 points:</p>
  
  <pre><code>Line(vec2 p0, vec2 p1) {
  
      //...
  
      //Result A,B,C
      A = (?);
      B = (?);
      C = (?);
  }
  </code></pre>
  
  <p>Thanks.</p>
  
  <p><strong>PS: Sorry for my english. :/</strong></p>
  
  <p><strong>Update:</strong></p>
  
  <p><strong>Solved!</strong></p>
  
  <p>I found answer after hour of calculations and it's similar to plane equation.</p>
  
  <p><strong>Programming:</strong></p>
  
  <pre><code>Line(vec2 p0, vec2 p1) {
      vec2 l = p1 - p0;
  
      vec2 n = l.cross();
      n.normalize();
  
      //Result a,b,c 
      a = n.x;
      b = n.y;
      c = vec2::dot(vec2(-n.x, -n.y), p0);
  }
  </code></pre>
  
  <p>Difference is in cross product of vec2. It's someting like this:</p>
  
  <pre><code>vec2 cross(vec2 p) { //Only one parameter
      return vec2(y, -x);
  }
  </code></pre>
  
  <p><strong>Mathematic:</strong></p>
  
  <p>P1 - start point of line</p>
  
  <p>P2 - end point of line</p>
  
  <p>1) <strong><em>N = x(P2 - P1)</em></strong></p>
  
  <p>Where "x" is cross product of vector (swap elemets and negate element x) --->  x(V) = [Vy, -Vx]</p>
  
  <p>(I'm not sure is this operation is official but result of that should be vector which is perpendicular to that parameter)</p>
  
  <p>2) <strong><em>N = N / |N|</em></strong> <em>(normalize vector N)</em></p>
  
  <p><em>Where |N| is length of vector N</em></p>
  
  <p>3) <em>Result A:</em> <strong><em>a = Nx</em></strong></p>
  
  <p>4) <em>Result B:</em> <strong><em>b = Ny</em></strong></p>
  
  <p>5) <strong><em>N' = -N</em></strong></p>
  
  <p>6) <em>Result C:</em> <strong><em>c = (N').(P1)</em></strong></p>
  
  <p><em>Where "(N').(P1)" is dot product of vectors N' and P1</em></p>
  
  <p><strong>PS:</strong></p>
  
  <p>This formula <strong>d = (|(x_2-x_1)x(x_1-x_0)|)/(|x_2-x_1|)</strong> is right. It works. But I need to use equation <strong>ax + by + c = 0</strong> because I need to know if it's on left or right side from line (positive of negaive distance) and it's better for programming. Thanks for answers.</p>
  
  <p>Still sorry for my english. :D</p>