1

我在构建 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 是数据库中存在的点,我们比较两者,找到与数据库中的点非常相似的点。

4

2 回答 2

1

你需要一个距离公式:

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)
于 2012-04-12T23:01:50.410 回答
0

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

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

于 2013-02-09T00:25:13.593 回答