zoukankan      html  css  js  c++  java
  • 点到直线的距离

     c#版本

             /****点到直线的距离***

             * 过点(x1,y1)和点(x2,y2)的直线方程为:KX -Y + (x2y1 - x1y2)/(x2-x1) = 0
             * 设直线斜率为K = (y2-y1)/(x2-x1),C=(x2y1 - x1y2)/(x2-x1)
             * 点P(x0,y0)到直线AX + BY +C =0DE 距离为:d=|Ax0 + By0 + C|/sqrt(A*A + B*B)
             * 点(x3,y3)到经过点(x1,y1)和点(x2,y2)的直线的最短距离为:
             * 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版本

    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)返回垂足;(x1,y1)为测试点;(x2,y2)(x3,y3)为直线点
    On Error GoTo PROC_ERROR
        Dim result As New MapXLib.Point
        If x2 = x3 Then '垂直线
            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

  • 相关阅读:
    Excel Formulas-Vlookup
    C#字符串与unicode互相转换
    string.IsNullOrWhiteSpace
    CREATE SEQUENCE sqlserver
    error CS1056
    WebExceptionStatus
    运维踩坑记
    C# 快捷命令
    sqlserver2019安装教程
    sql server 数据库mdf文件和log文件过大问题
  • 原文地址:https://www.cnblogs.com/lauer0246/p/1510363.html
Copyright © 2011-2022 走看看