Distance from point to straight line

Source: Internet
Author: User
Tags ipoint

C # version

/***** Distance from a point to a straight line ***

* The Linear Equations of points (x1, Y1) and points (X2, Y2) are: Kx-y + (x2y1-x1y2)/(x2-x1) = 0
* Set the straight line slope to k = (y2-y1)/(x2-x1), c = (x2y1-x1y2)/(x2-x1)
* The distance from point P (x0, y0) to the straight line AX + by + c = 0de is: D = | ax0 + by0 + c |/SQRT (A * A + B * B)
* The shortest distance from a vertex (X3, Y3) to a straight line between a vertex (x1, Y1) and a vertex (X2, Y2) is:
* Distance = | K * X3-Y3 + c |/SQRT (K * k + 1)
*/
Public static double getmindistance (ipoint pt1, ipoint pt2, ipoint pt3)
{
Double Dis = 0;
If (pt1.x = pt2.x)
{
Dis = math. Abs (pt3.x-pt1.x );
Return DIS;
}
Double linek = (pt2.y-pt1.y)/(pt2.x-pt1.x );
Double linec = (pt2.x * pt1.y-pt1.x * pt2.y)/(pt2.x-pt1.x );
Dis = math. Abs (linek * pt3.x-pt3.y + linec)/(math. SQRT (linek * linek + 1 ));
Return DIS;

}

 

Vb version

Public Function getverticalpoint (byval X1 as double, byval Y1 as double, byval X2 as double, byval Y2 as double, byval X3 as double, byval Y3 as double, byref X as double, byref y as double) as Boolean
'(X, y) returns the vertical foot. (x1, Y1) is the test point. (X2, Y2) (X3, Y3) is the linear point.
On Error goto proc_error
Dim result as new mapxlib. Point
If X2 = X3 then' vertical line
X = x2
Y = Y1
Getverticalpoint = true
Exit Function
Else
Dim KK as double
Kk = (Y3-Y2)/(X3-X2)
X = (Y1-y2 + KK * X2 + (1/KK) * X1)/(1/KK) + KK)
Y = KK * X-X2 * KK + y2
Getverticalpoint = true
Exit Function
End if
Getverticalpoint = false
Exit Function
Proc_error:
Msgbox err. Description
End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.