首页 > 编程语言 >C# 点到直线的垂直距离

C# 点到直线的垂直距离

时间:2023-10-10 19:57:04浏览次数:32  
标签:pstart point C# Vector3 pend vec new 点到 垂直距离

 private void btnShortLen_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Stopwatch sw0 = new System.Diagnostics.Stopwatch();
            sw0.Start();

            int i = 0;
            while (i < 100000)
            {
                Point pi1 = new Point(0, 0);
                Point pst = new Point(0, 4);
                Point ped = new Point(4, 0);
                double dis = GetMinDistance(pst, ped, pi1);
                i++;
            }
            sw0.Stop();
            var s0 = sw0.ElapsedMilliseconds; //4  此方法省时

            System.Diagnostics.Stopwatch sw1 = new System.Diagnostics.Stopwatch();
            sw1.Start(); i = 0;
            while (i < 100000)
            {
                Vector3 point_a = new Vector3(0f, 0f, 1.0f);
                Vector3 point_b = new Vector3(0f, 4.0f, 1.0f);
                Vector3 point_c = new Vector3(4f, 0f, 1.0f);
                VectorCalculate vec_Calculate = new VectorCalculate(point_b, point_c, point_a);
                float res = vec_Calculate.DistanceCalculate();
                i++;
            }
            sw1.Stop();
            var s1 = sw1.ElapsedMilliseconds;  //10
        }
        /****点到直线的距离*** 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(PointF pstart, PointF pend, PointF poxy)
        {
            double dis = 0;
            if (pstart.X == pend.X)
            {
                dis = Math.Abs(poxy.X - pstart.X);
                return dis;
            }
            double lineK = (pend.Y - pstart.Y) / (pend.X - pstart.X);
            double lineC = (pend.X * pstart.Y - pstart.X * pend.Y) / (pend.X - pstart.X);
            dis = Math.Abs(lineK * poxy.X - poxy.Y + lineC) / (Math.Sqrt(lineK * lineK + 1));
            return dis;
        }
    }
    class VectorCalculate
    {

        public VectorCalculate(Vector3 point_a, Vector3 point_b, Vector3 point_c)
        {
            _point_a = point_a;
            _point_b = point_b;
            _point_c = point_c;
        }
        public float DistanceCalculate()
        {
            Vector3 vec_ab = _point_b - _point_a;
            Vector3 vec_ac = _point_c - _point_a;
            float distance = 0.0f;
            Vector3 vec_Normalize = Vector3.Normalize(vec_ab);
            float projection_length = Vector3.Dot(vec_ac, vec_Normalize); //计算出投影向量的模
            Vector3 vec_Projection = Vector3.Multiply(projection_length, vec_Normalize); //计算出投影向量
            distance = Vector3.Distance(vec_ac, vec_Projection);
            return distance;
        }

        private Vector3 _point_a;
        private Vector3 _point_b;
        private Vector3 _point_c;
    }

 

标签:pstart,point,C#,Vector3,pend,vec,new,点到,垂直距离
From: https://www.cnblogs.com/dongxizhen/p/17755558.html

相关文章

  • SQLAlchemy学习-12.查询之 order_by 按desc 降序排序
    前言sqlalchemy的query默认是按id升序进行排序的,当我们需要按某个字段降序排序,就需要用到order_by。order_by排序默认情况下sqlalchemy的query默认是按id升序进行排序的res=session.query(Project).all()print(res)#[<Project(id='1',project_name='string'.........
  • python计算 ico hash 值
    apt-getinstallpython3apt-getinstallpython3-pippipinstallmmh3importrequestsimportbase64importmmh3URL='https://www.baidu.com/favicon.ico'r=requests.get(URL)r1=r.contentr2=base64.encodebytes(r1)r3=mmh3.hash(r2)print('http.fav......
  • 好用的spack(为程序安装依赖的库)
    spackload**=moduleload**manspack##manuelspackfind##已安装的库spacklist##列出所有的库spackinstall##安装某库spackuninstall##卸载某库[email protected]%[email protected]##固定版本号的库......
  • CTFer blogs--Web-fileinclude
    本题来源攻防世界解题思路:首先分析代码,将cookie中‘language’的值传入lan在后续又使用include调用了lan这个变量,因此可以从此处写入读取flag.php的payload可以使用burpsuite进行抓包后添加cookie值name:languagevalue:php://filter/read=convert.base64-encode/resource=/var......
  • [WUSTCTF2020]朴实无华 1
    原理intval绕过MD5加密值和原值相等绕过命令执行空格过滤解题过程intval($num)<2020&&intval($num+1)>2021绕过:https://blog.csdn.net/qq_47804678/article/details/128814377参考文章:https://blog.csdn.net/m0_62879498/article/details/124632041......
  • CSP模拟51联测13 B.狗
    CSP模拟51联测13B.狗目录CSP模拟51联测13B.狗题目大意题目描述输入格式输出格式样例样例1inputoutput思路题目大意题目描述小G养了很多狗。小G一共有\(n\timesn\)条狗,在一个矩阵上。小G想让狗狗交朋友,一条狗狗最多只能交一个朋友,不必所有狗狗都有朋友。但是狗狗交朋友......
  • .NET6 startup.cs 注入 本地缓存,AddTransient ,AddScoped ,AddSingleton生命周期
    .NET6startup.cs注入本地缓存//使用缓存usingMicrosoft.Extensions.Caching.Memory;services.AddMemoryCache();//自定义缓存类,类继承接口services.AddScoped<IMemoryCacheHelper,MemoryCacheHelper>();service.cs里使用构造函数注入生成对象方法里调用对象的写,获取......
  • Springmvc执行流程
         ......
  • CF2400计数
    感觉其他都没它重要,开写。CF1628D1/2看题解前:游戏挺好玩,玩着玩着就可以推出式子:\(f_{i,j}=\frac{f_{i-1,j}+f_{i,j}}{2}\)边界情况大概是\(i=j\)时\(f_{i,j}=i\),\(j=0\)时\(f_{i,j}=0\)直接暴力递推即可过D1,也是我想到的部分。看题解后:形式化dp式子,发现是个下三角......
  • THUPC2021 鬼街
    Day\(\text{M}_r(\text{O}_2)\)。这东西原来叫减半报警器??首先这题肯定和数论没啥关系,因为\(10^5\)之内的\(\max\omega(n)=6\),即一个数至多只有\(6\)个质因子。然后我们发现如果\(x\)有\(k\)个不同的质因子,这\(k\)个质因子代表的房子内闹鬼总次数达到了\(y\),那么......