#๐Ÿ”’ Converting 2D into 1D

127 messages ยท Page 1 of 1 (latest)

vivid ingot
#

Guys so I'm working on a grid tile project, where you have a map of 16*16 tiles. the tiles all can have a different ID, so I store it in a list with 256 places, that are 0 by default. Now, I need help with an algorithm, where I can just give x and y coordinates and it finds the corresponding index in the list.

signal violetBOT
#

@vivid ingot

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

misty temple
#

ig it depends on how you stored it

vivid ingot
#

i'm not sure

#

the list goes from 1 to 256

#

not 0

misty temple
#

ew why

vivid ingot
#

y not

misty temple
#

I mean it doesn't really matter just add 1 but ew

vivid ingot
#

okk

misty temple
#

it breaks indexing conventions. it's just weird

vivid ingot
#

u sure it's going to work?

misty temple
#

no, I haven't seen how you flattened your 16x16 matrix yet

#

but assuming you just laid each index out sequentially it should work

vivid ingot
#

yes

#

huh

#

bottom left sorry

misty temple
vivid ingot
#

and loops up the top right

#

so bottom left is index 1 and top is index 256

misty temple
#

from x=0, y=15 to x=15,y=0?

vivid ingot
#

yeah

#

no

misty temple
#

fck

#

ok just 0,0 to 15,15 then

vivid ingot
#

something like this

misty temple
#

oh

#

okay

vivid ingot
#

but the one is in the bottom left

#

I calculated and x= 3 and y=2 should be at index 19

#

but all the ones I've tested so far don't seem to work, or they work but for nothing else

misty temple
#

can you send your actual code?

vivid ingot
#

I didn't code it yet

#

I like to think about the logic first

misty temple
#

so the bottom left to top right thing is not set in stone?

vivid ingot
#

?

misty temple
#

the order you are indexing them in

vivid ingot
#

yes

#

ow I get it

#

no it's not

#

I just thought it would be smarter to do it like this

misty temple
#

you should do it top left to bottom right then

vivid ingot
#

because it would be a more accurate repr of the list

misty temple
#

that makes it easy to convert and lets you use x*16+y+1

#

there is no such thing as a "more accurate" representation of a flattened list

vivid ingot
#

if you say

#

sooo

#

I just start to iterate from top to bottom then

#

but does it change the algo?

#

because it's technically just the same thing reversed

#

let's say I have a tile in position x=3 and y=2

#

and I'd like to get it's list index

#

(it's 19)

#

that would be

#

3*16+2+1

#

which is 51

#

somehow

misty temple
#

right, because you have the x=0 list as indices 1-16, the x=1 list at 17-32, the x=2 list at 33-48, and x=3,y=0 is 49+2 == 51

vivid ingot
#

so you mean I have to do x-1?

misty temple
#

no

vivid ingot
#

but x=1 and y=1 is the origin right?

misty temple
#

oh.. you're doing 1 based indices for both.. right

#

is that set in stone? it just makes your algorithm harder to use

vivid ingot
#

no I guess

misty temple
#

but yeah in that case you would do x-1

vivid ingot
#

and y too?

#

because I already calculateed

misty temple
#

no you can just remove the +1 part at the end

vivid ingot
#

and that gives 34 or 35

#

yes it's still not 19

#

...

#

I'm smashing my head

misty temple
#

let me just show you a little code to demonstrate what I mean

vivid ingot
#

but jus a second

#

if index 0 doesn't exist

#

like I can't insert stuff at index 0

#

in the list

#

I should then keep the +1 part right ?

#

anyways

#

show me

#

i'm desperate

misty temple
#
>>> x = [[i+j for i in range(3*j,3*(1+j))] for j in range(3)]
>>> x
[[0, 1, 2], [4, 5, 6], [8, 9, 10]]
>>> flat_x = []
>>> for i in range(3):
...     for j in range(3): flat_x.append(x[i][j])
... 
>>> flat_x
[0, 1, 2, 4, 5, 6, 8, 9, 10]
#
>>> def calculate_flat_index(x,y): return x*3+y
... 
>>> x[2][1]
9
>>> flat_x[calculate_flat_index(2,1)]
9
#

this is using 0 based indexing obviously

vivid ingot
#

so you are doing a 3*3 grid right

misty temple
#

in the example yeah

vivid ingot
#

with 0 0 as origin

#

so it's technically a 4*4?

misty temple
#

no it's still a 3x3

vivid ingot
#

my logic is burned

#

so wait it goes

#

0 1 2

misty temple
#

yeah

vivid ingot
#

3 4 5

#

6 7 8

#

okaaaaay

#

how would that translate to 16 * 16?

#

will it just go up to 15

misty temple
#

yes

#

0, 1, 2, ..., 14, 15

vivid ingot
#

so the index of x = 3 and y =2 is technically

misty temple
#

not using 0 based indexing

vivid ingot
#

right?

#

35?

misty temple
#

it'd be 3*16 == 48 + 2 == 50

vivid ingot
#

alr I'm guessing

vivid ingot
#

and using one based indexing means

#

it's just +1

#

right?

#

hello?

#

did he go

#

noo

#

come back

misty temple
#

ngl I'm just not that interested in calculating the 1-based indexing version but fine whatever

#

is your original matrix 1 based indexed as well?

#

i.e. x=1,y=1 is the first index?

#

(x-1)*grid_size + y

#

is the algorithm

#

@vivid ingot

vivid ingot
#

thanks for your help anyways

signal violetBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.