• <small id='hP8Qq'></small><noframes id='hP8Qq'>

      <bdo id='hP8Qq'></bdo><ul id='hP8Qq'></ul>

    <i id='hP8Qq'><tr id='hP8Qq'><dt id='hP8Qq'><q id='hP8Qq'><span id='hP8Qq'><b id='hP8Qq'><form id='hP8Qq'><ins id='hP8Qq'></ins><ul id='hP8Qq'></ul><sub id='hP8Qq'></sub></form><legend id='hP8Qq'></legend><bdo id='hP8Qq'><pre id='hP8Qq'><center id='hP8Qq'></center></pre></bdo></b><th id='hP8Qq'></th></span></q></dt></tr></i><div id='hP8Qq'><tfoot id='hP8Qq'></tfoot><dl id='hP8Qq'><fieldset id='hP8Qq'></fieldset></dl></div>
    1. <tfoot id='hP8Qq'></tfoot>
      <legend id='hP8Qq'><style id='hP8Qq'><dir id='hP8Qq'><q id='hP8Qq'></q></dir></style></legend>

        如何在 C、C#/.NET 2.0 或 Java 的所有情况下计算点和线段之间的最短 2D 距离?

        how to calculate shortest 2D distance between a point and a line segment in all cases in C, C# / .NET 2.0 or Java?(如何在 C、C#/.NET 2.0 或 Java 的所有情况下计算点和线段之间的最短 2D 距离?)
      1. <i id='9JlBc'><tr id='9JlBc'><dt id='9JlBc'><q id='9JlBc'><span id='9JlBc'><b id='9JlBc'><form id='9JlBc'><ins id='9JlBc'></ins><ul id='9JlBc'></ul><sub id='9JlBc'></sub></form><legend id='9JlBc'></legend><bdo id='9JlBc'><pre id='9JlBc'><center id='9JlBc'></center></pre></bdo></b><th id='9JlBc'></th></span></q></dt></tr></i><div id='9JlBc'><tfoot id='9JlBc'></tfoot><dl id='9JlBc'><fieldset id='9JlBc'></fieldset></dl></div>
        <tfoot id='9JlBc'></tfoot>

            • <legend id='9JlBc'><style id='9JlBc'><dir id='9JlBc'><q id='9JlBc'></q></dir></style></legend>
                  <bdo id='9JlBc'></bdo><ul id='9JlBc'></ul>
                    <tbody id='9JlBc'></tbody>
                • <small id='9JlBc'></small><noframes id='9JlBc'>

                • 本文介绍了如何在 C、C#/.NET 2.0 或 Java 的所有情况下计算点和线段之间的最短 2D 距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能重复:
                  点到线段的最短距离

                  我正在寻找一种方法来计算所有情况下的最小距离.我发现的解决方案的问题是:

                  i am looking for a way to calculate the minimum distance in all cases. the problems with solutions i found are:

                  1. 具有图形概念图的解决方案显示点始终与线段垂直,因此它位于线段端点之间".我的几何技能很糟糕,所以我无法验证这些解决方案是否适用于所有情况.

                  1. Solutions with graphical conceptual drawings show point always on perpendicular from line segment so it's "between line segment's end points". My geometry skills are horrible so i can't verify that these solutions work in all cases.

                  算法解决方案是:使用 fortran 或我不完全理解的其他语言,b:被人们标记为不完整,c:调用未以任何方式描述的方法/函数(被认为是微不足道的).

                  Algorithm solutions are a: with fortran or some other language i don't fully understand, b: are flagged as incomplete by people, c: calling methods/functions that are not described in any way (considered trivial).

                  2 a、b、c 的好例子是

                  Good example of 2 a, b and c is

                  点到线段的最短距离

                  我将二维线段作为双坐标对 (x1, y1), (x2,y2) 并将点作为双坐标 (x3,y3).C#/Java/C 解决方案都受到赞赏.

                  i have the 2D line segment as double-type co-ordinate pair (x1, y1), (x2,y2) and point as double type co-ordinate (x3,y3). C#/Java/C solutions are all appreciated.

                  感谢您的回答&BR:马蒂

                  Thanks for your answers & BR: Matti

                  推荐答案

                  也回答了 点和线段之间的最短距离,因为它收集了所有语言的解决方案.答案也放在这里,因为这个问题专门询问 C# 解决方案.这是从 http://www.topcoder.com/修改的tc?d1=tutorials&d2=geometry1&module=Static :

                  Answered also Shortest distance between a point and a line segment because that gathers solutions in all languages. Answer put also here because this questions asks specifically a C# solution. This is modified from http://www.topcoder.com/tc?d1=tutorials&d2=geometry1&module=Static :

                  //Compute the dot product AB . BC
                  private double DotProduct(double[] pointA, double[] pointB, double[] pointC)
                  {
                      double[] AB = new double[2];
                      double[] BC = new double[2];
                      AB[0] = pointB[0] - pointA[0];
                      AB[1] = pointB[1] - pointA[1];
                      BC[0] = pointC[0] - pointB[0];
                      BC[1] = pointC[1] - pointB[1];
                      double dot = AB[0] * BC[0] + AB[1] * BC[1];
                  
                      return dot;
                  }
                  
                  //Compute the cross product AB x AC
                  private double CrossProduct(double[] pointA, double[] pointB, double[] pointC)
                  {
                      double[] AB = new double[2];
                      double[] AC = new double[2];
                      AB[0] = pointB[0] - pointA[0];
                      AB[1] = pointB[1] - pointA[1];
                      AC[0] = pointC[0] - pointA[0];
                      AC[1] = pointC[1] - pointA[1];
                      double cross = AB[0] * AC[1] - AB[1] * AC[0];
                  
                      return cross;
                  }
                  
                  //Compute the distance from A to B
                  double Distance(double[] pointA, double[] pointB)
                  {
                      double d1 = pointA[0] - pointB[0];
                      double d2 = pointA[1] - pointB[1];
                  
                      return Math.Sqrt(d1 * d1 + d2 * d2);
                  }
                  
                  //Compute the distance from AB to C
                  //if isSegment is true, AB is a segment, not a line.
                  double LineToPointDistance2D(double[] pointA, double[] pointB, double[] pointC, 
                      bool isSegment)
                  {
                      double dist = CrossProduct(pointA, pointB, pointC) / Distance(pointA, pointB);
                      if (isSegment)
                      {
                          double dot1 = DotProduct(pointA, pointB, pointC);
                          if (dot1 > 0) 
                              return Distance(pointB, pointC);
                  
                          double dot2 = DotProduct(pointB, pointA, pointC);
                          if (dot2 > 0) 
                              return Distance(pointA, pointC);
                      }
                      return Math.Abs(dist);
                  } 
                  

                  这篇关于如何在 C、C#/.NET 2.0 或 Java 的所有情况下计算点和线段之间的最短 2D 距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)
                  1. <i id='XRyjN'><tr id='XRyjN'><dt id='XRyjN'><q id='XRyjN'><span id='XRyjN'><b id='XRyjN'><form id='XRyjN'><ins id='XRyjN'></ins><ul id='XRyjN'></ul><sub id='XRyjN'></sub></form><legend id='XRyjN'></legend><bdo id='XRyjN'><pre id='XRyjN'><center id='XRyjN'></center></pre></bdo></b><th id='XRyjN'></th></span></q></dt></tr></i><div id='XRyjN'><tfoot id='XRyjN'></tfoot><dl id='XRyjN'><fieldset id='XRyjN'></fieldset></dl></div>
                    • <tfoot id='XRyjN'></tfoot>

                      <legend id='XRyjN'><style id='XRyjN'><dir id='XRyjN'><q id='XRyjN'></q></dir></style></legend>

                      <small id='XRyjN'></small><noframes id='XRyjN'>

                        <tbody id='XRyjN'></tbody>

                        <bdo id='XRyjN'></bdo><ul id='XRyjN'></ul>