Point to find the shortest distance straight line and a foot

First, the two-point linear equation seek

Two points on a given line: (x1, x2), (y1, y2);

Provided equation: Ax + By + C = 0;

1. seeking slope: k = (y2-y1) / (x2-x1);

2. The straight line equation is: y-y1 = k (x-x1);

Conversions obtained: kx-y + y1-kx1 = 0, namely:

A=k
B=-1
C=y1-kx1=y1-(y2-y1)/(x2-x1)*x1

Second, the demand from the foot of the formula and

1. point (x0, y0) is the straight line distance formula:

d=abs(Ax0+By0+C)/sqrt(A*A+B*B);

2. The pedal set is p, then:

p.x=(B*B*x0-A*B*y0-A*C)/(A*A+B*B)
p.y=(A*A*y0-A*B*x0-B*C)/(A*A+B*B) 

Third, the code

Input and segments, and a foot return distance

. 1  function getDistanceP2L (Point, Line) {
 2  
. 3  var X1 = line.startPoint.x;
 . 4  var Y1 = line.startPoint.y;
 . 5  var X2 = line.endPoint.x;
 . 6  var Y2 = line.endPoint.y;
 . 7  var X0 = point.x;
 . 8  var yO = point.y;
 . 9  
10  var K = X1 X2 == 10000: (Y2-Y1) / (X2-X1); // when x1 = x2, a slope? set a large value 10000
 . 11  var a = K;
 12 is  var B = -1 ;
 13 is  var C = Y1-K *x1;
14 
15 var d=Math.abs(a*x0+b*y0+c)/Math.sqrt(a*a+b*b);
16 
17 var px=(b*b*x0-a*b*y0-a*c)/(a*a+b*b);
18 var py=(a*a*y0-a*b*x0-b*c)/(a*a+b*b); 
19 var p=new Point(px,py);
20 
21 return [d,p];
22 }

 

Guess you like

Origin www.cnblogs.com/snsart/p/10954593.html