Share via

How to output positions from x and y axis arrays

Mansour_Dalir 1,976 Reputation points
Dec 21, 2023, 10:21 AM

ArrayXY

hi

I have two arrays. The first array is the x axis and the second array is the y axis

dim ArrayLeftRight_X() As Integer(1,4,8,12)

dim ArrayUpDown_Y() as Integer (1,4,8,12)

Need Result

Dim ArrayResult as List(Of Point)

'X Y

ArrayResult (0)=1,1

ArrayResult (1)=4,4

ArrayResult (2)=4,1

ArrayResult (3)=8,4

ArrayResult (4)=8,1

ArrayResult (5)=12,1

ArrayResult (6)=16,4

ArrayResult (7)=4,4

ArrayResult (8)=4,8

......End ... ArrayResult (31)=16,16

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,744 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 32,456 Reputation points Microsoft Vendor
    Dec 21, 2023, 1:43 PM

    Hi @Mansour_Dalir ,

    Swap the order of ArrayX and ArrayY in the two For Each loops.

        Private Function output(ArrayX As Integer(), ArrayY As Integer()) As List(Of Point)
            Dim ArrayResult As New List(Of Point)
            For xIndex As Integer = 0 To ArrayX.Length - 2
                For yIndex As Integer = 0 To ArrayY.Length - 2
                    Dim topLeft As New Point(ArrayX(xIndex), ArrayY(yIndex))
                    Dim bottomRight As New Point(ArrayX(xIndex + 1), ArrayY(yIndex + 1))
                    ArrayResult.Add(topLeft)
                    ArrayResult.Add(bottomRight)
                Next
            Next
            Return ArrayResult
        End Function
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 32,456 Reputation points Microsoft Vendor
    Dec 21, 2023, 11:14 AM

    Hi @Mansour_Dalir ,

    You can use two For Each loops to compose the list of points you want.

        Private Function output(ArrayX As Integer(), ArrayY As Integer()) As List(Of Point)
            Dim ArrayResult As New List(Of Point)
            For Each x As Integer In ArrayX
                For Each y As Integer In ArrayY
                    Dim newPoint As New Point(x, y)
                    ArrayResult.Add(newPoint)
                Next
            Next
            Return ArrayResult
        End Function
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.