Explain about width and height

Hi,

I see the code:

def populate_attractive_field(height, width, goal_xy):
“”"
Creates an attractive field
returns: 1D flat grid map
“”"
field = [0] * height * width
for row in range(height):
for col in range(width):
force_value = random_force()
# Assign to potential field
field[row + width * col] = force_value
return field

Normally, how to define row, column in a map?
I mean, How to know which side is row, and which side is column?

Hi,

As I see from the code (hard to read, can you use the preformatted text when copy-pasting code?):

field[row + width * col] = force_value

Lets say the coordinates are (row, col) and width = 10, height = 20
(0,0) → field[0]
(1,0) → field[1]

(9, 0) → field[9]

(19,0) → field[19]
(0,1) → field[10]
(1,1) → field[11]
(0,2) → field[20]

So you will use the same place for different coordinates.

Seems to me that the expression has a bug. Maybe it is better:

field[row * width + col]

Where row is in range [0…width-1] and col is range [0…]

Péter

By the way, personally I do not like “coding” matrices like this. I think it is error prone and confusing.
Python can deal with matrixes like this:

Péter

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.