计算坐标点之间的最小距离

时间:2012-04-12 22:11:16

标签: vb.net vbscript

我在构建VBcode时遇到问题,以获得一组坐标点之间的最小距离。我实际尝试的是找到一组坐标点(A(x1,y1),Bx2,y2),C(x3,y3),D(x4,y4),E(x5,y5))与另一组坐标点(i(x1,y1),j(x2,y2),k(x3,y3),l(x4,y4),m(x5,y5))之间的最小距离。 我希望你能理解我想要解释的内容。

有人可以帮帮我吗?


Public Function DoSearch(ByVal SearchCritera As Bracket, ByVal ListToSearchFrom As System.Collections.Generic.List(Of TRacksDefinitions.Racks.Bracket)) As System.Collections.Generic.List(Of TRacksDefinitions.Search.SearchBracket) Implements TRacksDefinitions.Search.ISearch.DoSearch


        _results.Clear()
        For Each b As Bracket In ListToSearchFrom
            'LAST POINT DISTANCE., WT DIST, number of points, similarity of points (-1 if not used),
            Dim dist() As Double = {0, 0, 0, 0, 0}
            Dim dx As Double = b.RearPoints(b.RearPoints.Length - 2).X - SearchCritera.RearPoints(SearchCritera.RearPoints.Length - 2).X
            Dim dy As Double = b.RearPoints(b.RearPoints.Length - 2).Y - SearchCritera.RearPoints(SearchCritera.RearPoints.Length - 2).Y
            Dim dz As Double = b.RearPoints(b.RearPoints.Length - 2).Z - SearchCritera.RearPoints(SearchCritera.RearPoints.Length - 2).Z

            dist(0) += Math.Sqrt(dx ^ 2 + dy ^ 2 + dz ^ 2)
            dist(1) += Math.Abs(SearchCritera.Wallthickness - b.Wallthickness)
            dist(2) += Math.Abs(SearchCritera.RearPoints.Count - b.RearPoints.Count)
            If SearchCritera.RearPoints.Count = b.RearPoints.Count Then
                Dim d1, d2 As Decimal

                ' Dim sum As Double = 0
                For i As Integer = 0 To b.RearPoints.Count - 1
                    d1 = Math.Abs(SearchCritera.RearPoints(i).X - b.RearPoints(i).X)
                    d2 = Math.Abs(SearchCritera.RearPoints(i).Y - b.RearPoints(i).Y)

                  ?????????????????

                Next
            Else
                dist(3) = -1
            End If

@LarsTech 以上是我到目前为止创建的代码,下一步是计算最小距离标准。

搜索条件:后点是我们从solidworks获得的,而b.rearpoints是数据库中存在的那个,我们比较它们都找到了一个与数据库中非常相似的那个。

2 个答案:

答案 0 :(得分:0)

你需要一个距离公式:

Public Function GetDistance(ByVal startPoint As Point, ByVal endPoint As Point) As Integer
  Return Math.Sqrt((Math.Abs(endPoint.X - startPoint.X) ^ 2) + _
                   (Math.Abs(endPoint.Y - startPoint.Y) ^ 2))
End Function

然后你只需循环遍历所有点,找出最小的距离:

Dim listOne As New List(Of Point)
listOne.Add(New Point(10, 10))
listOne.Add(New Point(20, 20))
listOne.Add(New Point(30, 30))
listOne.Add(New Point(40, 40))
listOne.Add(New Point(50, 50))

Dim listTwo As New List(Of Point)
listTwo.Add(New Point(50, 10))
listTwo.Add(New Point(50, 20))
listTwo.Add(New Point(50, 30))
listTwo.Add(New Point(50, 40))
'listTwo.Add(New Point(50, 50))

Dim minDistance As Nullable(Of Integer)
For Each p1 As Point In listOne
  For Each p2 As Point In listTwo
    Dim distance As Integer = GetDistance(p1, p2)
    If minDistance Is Nothing OrElse distance < minDistance Then
      minDistance = distance
    End If
  Next
Next

MessageBox.Show("Minimum Distance = " & minDistance.ToString)

答案 1 :(得分:0)

在二维中,取决于点的分布,如果N超过9,则将空间划分为int(sqrt(N)-1)^ 2相同大小的“框”可能是有意义的。如果任何方框包含超过一百个左右的点,则从人口最多的方框中随机选取十个左右的点,找到任意一对之间的最小距离,然后用最小距离或方框大小减小的方框重新分区。因子int(sqrt(k)-1)其中k是该框的总体[如果一个框包含那么多的点,那么在该框的大小范围内必须有两个。

在最坏的情况下,这种算法可能会表现不佳,因为可能存在非常密集的点集中,这些点在某种程度上永远不会被任意选择用于距离测试,并且可能需要一段时间才能将空间细分到足以将它们分开。然而,在实践中,可能会在某些时候选择这些点,从而可以快速细分集合。