Problem description
The distance from a point to a straight line or line segment
Implementation ideas
Suppose there is a point coordinate P (x0, y0), a line segment AB, A coordinate (x1, y1), and B coordinate (x2, y2), find the distance d from point P to line segment AB or the straight line where it is located, and the vertical foot C (x, y) of point P on the straight line.
This requires reviewing high school mathematics knowledge.
First, you need to convert the coordinates of points A and B into the general formula of the linear equation Ax+By+C = 0, and the process will not be deduced.
Parameter calculation:
A=y2-y1;
B=x1-x2;
C=x2*y1-x1*y2;
1. The distance formula from a point to a straight line:
d= ( Ax0 + By0 + C ) / sqrt ( A*A + B*B );
2. Calculation formula for C (x, y) of the hang foot:
x = ( B*B*x0 - A*B*y0 - A*C ) / ( A*A + B*B );
y = ( -A*B*x0 + A*A*y0 – B*C ) / ( A*A + B*B );
Program implementation:
-(CGPoint)pedalPoint: (CGPoint)p1 : (CGPoint )p2: (CGPoint)x0{ float A=; float B=; float C=**; float x=(B*B*-A*B*-A*C)/(A*A+B*B); float y=(-A*B*+A*A*-B*C)/(A*A+B*B); //Distance from point to linefloat d=(A*+B*+C)/sqrt(A*A+B*B); CGPoint ptCross=ccp(x,y); NSLog(@”d======%f”,d); NSLog(@”A=======%f,B=======%f,C=======%f”,A,B,C); NSLog(@”Drooping feet======x=%f,y=%f”,x,y); return ptCross; }
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.