Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is one possible solution [untested]. The <code>AddNearestColumn()</code> method loops through each row and gets a List of distances to the other points in the table. It then loops through the list and gets the smallest value. You should use <code>distances.Sort()</code> in <code>AddNearestColumn()</code> rather than looping manually, but I'll leave that for you. :-)</p> <pre><code>Public Sub ProcessTable(table As Data.DataTable) AddNearestColumn(table) End Sub Private Sub AddNearestColumn(table As Data.DataTable) table.Columns.Add("Nearest", GetType(Double)) For Each r As Data.DataRow In table.Rows Dim distances As List(Of Double) = GetDistancesFromPoint(table, r) Dim nearest As Double = Double.MaxValue For Each distance As Double In distances If (distance &lt; nearest) Then nearest = distance Next r.Item("Nearest") = nearest Next End Sub Private Function GetDistancesFromPoint(table As Data.DataTable, referenceRow As Data.DataRow) As List(Of Double) Dim output As New List(Of Double) For Each r As Data.DataRow In table.Rows Dim referenceId As String = CStr(referenceRow.Item("ID")) Dim rowId As String = CStr(r.Item("ID")) If (rowId = referenceId) Then Continue For Dim x1 As Double = Convert.ToDouble(referenceRow.Item("X")) Dim y1 As Double = Convert.ToDouble(referenceRow.Item("Y")) Dim x2 As Double = Convert.ToDouble(r.Item("X")) Dim y2 As Double = Convert.ToDouble(r.Item("Y")) output.Add(Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)))) Next Return output End Function </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload