点到线的距离

参考:https://www.cnblogs.com/BillySir/p/PointAndLineDistance.html

 

点:(x0,y0)
线:Ax+By+C=0
①距离=ABS(A*x0+B*y0+C) / SQRT(A*A+B*B)

线:y=kx+b
②距离=ABS(k*x0-y0+b) / SQRT(k*k+1)

变量关系
k=-A/B
A=-kB
b=-C/B
C=-bB

线为2个不同点(x1,y1)和(x2,y2)来确定
A=-(y1-y2)/(x1-x2)*B
C=-(y2*x1-y1*x2)/(x1-x2)*B
代入①,得
③距离=ABS[-(y1-y2)/(x1-x2)*x0 + y0 - (y2*x1-y1*x2)/(x1-x2)] / SQRT{1+[(y1-y2)/(x1-x2)]^2}
化简得
④距离=ABS[(y1-y2)*x0 - (x1-x2)*y0 + y2*x1 - y1*x2] / SQRT[(x1-x2)^2+(y1-y2)^2]

变量关系
k=(y1-y2)/(x1-x2)
b=(y2*x1-y1*x2)/(x1-x2)

上一篇:Codeforces Round #614 (Div. 2)D Aroma's Search


下一篇:模板 - 数学 - 同余 - 扩展欧几里得算法/ExtendEuclideanAlgorithm