Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get specific portion of data from 1D array?

I have a regular NativeArray of colors. I want to efficiently sample and get a specific portion of data from that array depending on the given rect.

Let's say I have this 5x5 (25 in length) size 1D color array and I need to get data from this rect (x:2, y:1, w:3, h:2) as shown in the below picture

enter image description here

How can I do that?

If I try to use the function NativeArray<>.Copy it copies from index to length. So if I specify index 7 and 6 length it would copy from 7 to 12, which wouldn't work for me.

like image 799
UnknownUser Avatar asked Dec 06 '25 13:12

UnknownUser


1 Answers

NativeArray.Copy as you found out can take a start index where the copy should begin and an end index where it should end. Your scenario is more complicated. You may treat the Native array as a 2D array and use a nested for loop to copy elements from it over to the destination. The Copy method does the same, it is not a magic thing.

You may use following formula to convert 2D x, y indexes to 1D index

i = y * width + x
like image 195
Eduard Hasanaj Avatar answered Dec 08 '25 06:12

Eduard Hasanaj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!