How to convert a 1D coordinate into 2D indexes in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
	Public Shared Sub Main()
        Dim array2d As Integer(,) = {
				{ 1,  2,   3,  6,  0},
				{-5, -4,   0,  7,  9},
				{ 1, 18, 100, 14,  6},
				{ 9, 10,  27, 12, 13}}
		
        Dim array1d As Integer() = {1, 2, 3, 6, 0, -5, -4, 0, 7, 9, 1, 18, 100, 14, 6, 9, 10, 27, 12, 13}
		
        Dim cols As Integer = array2d.GetUpperBound(1) + 1
        Dim index As Integer = 17
        
		Dim i As Integer = index / cols
		Dim j As Integer = index - (i * cols) ' index Mod cols
        
		Console.WriteLine("i = " & i & " j = " & j)
        Console.WriteLine(array1d(index))
        Console.WriteLine(array2d(i, j))
    End Sub
End Class




' run:
'
' i = 3 j = 2
' 27
' 27
'

 



answered Sep 18, 2023 by avibootz
edited Sep 18, 2023 by avibootz

Related questions

1 answer 148 views
1 answer 164 views
1 answer 133 views
1 answer 154 views
1 answer 165 views
1 answer 159 views
1 answer 149 views
...