使用数组

时间:2015-06-04 05:30:15

标签: arrays vb.net

我正在编写一个Windows应用程序,用纬度和经度查找距离。我在数组中有超过100个值,并得到这个公式来找到距离

Dim theta As Double = lon1 - lon2
Dim dist As Double = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta))
dist = Math.Acos(dist)
dist = rad2deg(dist)
dist = dist * 60 * 1.1515
dist = dist * 1.609344   

我必须安排lat1,lat2,lon1,lon2的值吗?   任何建议如何安排我传递数值的数组?

1 个答案:

答案 0 :(得分:1)

首先,决定一个容器来存储每个坐标,可能是一个结构或类。

Public Class Coordinate
    Public Latitude As Double
    Public Longitude As Double

    Public Sub New(ByVal lon As Double, ByVal lat As Double)
        Latitude = lat
        Longitude = lon
    End Sub
End Class

然后您需要一个集合来存储您的所有坐标,可能是列表或字典(如果您想通过索引引用它们)。

Public Coordinates as As New Dictionary(Of Integer, Coordinate)

然后你的作业,

Coordinates.Add(1, New Coordinate(654.321, 123.456))
Coordinates.Add(2, New Coordinate(321.654, 456.123))

您可以构建函数以接受自定义容器作为输入。

Public Function CalcDistance(ByVal coord1 As Coordinate, ByVal coord2 As Coordinate) As Double
    Dim lon1 As Double = coord1.Longitude
    Dim lat1 As Double = coord1.Latitude
    Dim lon2 As Double = coord2.Longitude
    Dim lat2 As Double = coord2.Latitude
    'Do math!
    Return dist
End Function

最后,调用你的函数,传递它的值,然后发生魔法!

Dim distanceBetween As Double = CalcDistance(Coordinates(1), Coordinates(2))

更新:用户控件似乎是存储数据的一个糟糕的地方,因此我不愿意提供此信息,但由于这是您第二次尝试发布此问题,这是一种使用你所拥有的东西的简单方法。

Public Function CalcDistance(ByVal lon1 As Double, ByVal lat1 As Double, ByVal lon2 As Double, ByVal lat2 As Double) As Double
    'Do Math!
    Return dist
End Function

Dim distanceBetween As Double = CalcDistance(CDbl(ListBox1.Items(0).Value), CDbl(ListBox2.Items(0).Value), CDbl(ListBox1.Items(1).Value), CDbl(ListBox2.Items(1).Value))