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

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

      1. 获得离线最近的点

        get closest point to a line(获得离线最近的点)

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

          • <i id='SD0wJ'><tr id='SD0wJ'><dt id='SD0wJ'><q id='SD0wJ'><span id='SD0wJ'><b id='SD0wJ'><form id='SD0wJ'><ins id='SD0wJ'></ins><ul id='SD0wJ'></ul><sub id='SD0wJ'></sub></form><legend id='SD0wJ'></legend><bdo id='SD0wJ'><pre id='SD0wJ'><center id='SD0wJ'></center></pre></bdo></b><th id='SD0wJ'></th></span></q></dt></tr></i><div id='SD0wJ'><tfoot id='SD0wJ'></tfoot><dl id='SD0wJ'><fieldset id='SD0wJ'></fieldset></dl></div>
          • <small id='SD0wJ'></small><noframes id='SD0wJ'>

                1. <legend id='SD0wJ'><style id='SD0wJ'><dir id='SD0wJ'><q id='SD0wJ'></q></dir></style></legend>
                2. <tfoot id='SD0wJ'></tfoot>

                    <tbody id='SD0wJ'></tbody>
                  本文介绍了获得离线最近的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想要一个简单的 C# 函数来获取最近的点(从点 P)到线段 AB.一个抽象函数可能看起来像这样.我搜索了 SO,但没有找到可用的(由我自己)解决方案.

                  I'd like to have a straight forward C# function to get a closest point (from a point P) to a line-segment, AB. An abstract function may look like this. I've search through SO but not found a usable (by me) solution.

                  public Point getClosestPointFromLine(Point A, Point B, Point P);
                  

                  推荐答案

                  这里是伪装成伪代码的 Ruby,假设 Point 对象每个都有一个 xy 字段.

                  Here's Ruby disguised as Pseudo-Code, assuming Point objects each have a x and y field.

                  def GetClosestPoint(A, B, P)
                  
                    a_to_p = [P.x - A.x, P.y - A.y]     # Storing vector A->P
                    a_to_b = [B.x - A.x, B.y - A.y]     # Storing vector A->B
                  
                    atb2 = a_to_b[0]**2 + a_to_b[1]**2  # **2 means "squared"
                                                        #   Basically finding the squared magnitude
                                                        #   of a_to_b
                  
                    atp_dot_atb = a_to_p[0]*a_to_b[0] + a_to_p[1]*a_to_b[1]
                                                        # The dot product of a_to_p and a_to_b
                  
                    t = atp_dot_atb / atb2              # The normalized "distance" from a to
                                                        #   your closest point
                  
                    return Point.new( :x => A.x + a_to_b[0]*t,
                                      :y => A.y + a_to_b[1]*t )
                                                        # Add the distance to A, moving
                                                        #   towards B
                  
                  end
                  

                  <小时>

                  或者:

                  来自 Line-Line Intersection,维基百科.首先,找到 Q,这是从 P 朝着正确方向"迈出的第二个点.这给了我们四点.

                  From Line-Line Intersection, at Wikipedia. First, find Q, which is a second point that is to be had from taking a step from P in the "right direction". This gives us four points.

                  def getClosestPointFromLine(A, B, P)
                  
                    a_to_b = [B.x - A.x, B.y - A.y]   # Finding the vector from A to B
                                                          This step can be combined with the next
                    perpendicular = [ -a_to_b[1], a_to_b[0] ]
                                                      # The vector perpendicular to a_to_b;
                                                          This step can also be combined with the next
                  
                    Q = Point.new(:x => P.x + perpendicular[0], :y => P.y + perpendicular[1])
                                                      # Finding Q, the point "in the right direction"
                                                      # If you want a mess, you can also combine this
                                                      # with the next step.
                  
                    return Point.new (:x => ((A.x*B.y - A.y*B.x)*(P.x - Q.x) - (A.x-B.x)*(P.x*Q.y - P.y*Q.x)) / ((A.x - B.x)*(P.y-Q.y) - (A.y - B.y)*(P.y-Q.y)),
                                      :y => ((A.x*B.y - A.y*B.x)*(P.y - Q.y) - (A.y-B.y)*(P.x*Q.y - P.y*Q.x)) / ((A.x - B.x)*(P.y-Q.y) - (A.y - B.y)*(P.y-Q.y)) )
                  
                  end
                  

                  出于性能原因,缓存、跳过步骤等是可能的.

                  Caching, Skipping steps, etc. is possible, for performance reasons.

                  这篇关于获得离线最近的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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# - 按属性获取所有枚举值)

                    <bdo id='ENqDd'></bdo><ul id='ENqDd'></ul>
                    <tfoot id='ENqDd'></tfoot>

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

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

                              <tbody id='ENqDd'></tbody>
                          • <i id='ENqDd'><tr id='ENqDd'><dt id='ENqDd'><q id='ENqDd'><span id='ENqDd'><b id='ENqDd'><form id='ENqDd'><ins id='ENqDd'></ins><ul id='ENqDd'></ul><sub id='ENqDd'></sub></form><legend id='ENqDd'></legend><bdo id='ENqDd'><pre id='ENqDd'><center id='ENqDd'></center></pre></bdo></b><th id='ENqDd'></th></span></q></dt></tr></i><div id='ENqDd'><tfoot id='ENqDd'></tfoot><dl id='ENqDd'><fieldset id='ENqDd'></fieldset></dl></div>