Мне нужно рассчитать наклон каждой сетки, логика такова: горизонтальный градиент: левая высота сетки минус правая высота сетки вертикальный градиент: верхняя высота сетки минус нижняя высота сетки
Возврат: квадратный корень из суммы квадратов как горизонтального, так и вертикального градиента.
Однако я не могу рассчитать список из 3 списков и вернуть список с плавающей точкой.
#calculate slope main program
def find_slope(map_of_heights, x, y):
ind_row = len(map_of_heights)-1
ind_column = len(map_of_heights[0])-1
if y in range(1, ind_column) and x in range(1, ind_row):
#main logic
dx = map_of_heights[y][x-1] - map_of_heights[y][x+1]
dy = map_of_heights[y+1][x] - map_of_heights[y-1][x]
#different cases when the point is at the edge
elif x == 0 and y != 0 and y!= ind_row:
dx = 0 - map_of_heights[y][x+1]
dy = map_of_heights[y+1][x] - map_of_heights[y-1][x]
elif y == 0 and x != 0 and x != ind_column:
dx = map_of_heights[y][x-1] - map_of_heights[y][x+1]
dy = map_of_heights[y+1][x] - 0
elif x == ind_column and y != 0 and y!= ind_row:
dx = map_of_heights[y][x-1] - 0
dy = map_of_heights[y+1][x] - map_of_heights[y-1][x]
elif y == ind_row and x != 0 and x != ind_column:
dx = map_of_heights[y][x-1] - map_of_heights[y][x+1]
dy = 0 - map_of_heights[y-1][x]
elif x == 0 and y == 0:
dx = 0 - map_of_heights[y][x+1]
dy = map_of_heights[y+1][x] - map_of_heights[0][x]
elif x == ind_column and y == 0:
dx = map_of_heights[y][x-1] - 0
dy = map_of_heights[y+1][x] - 0
elif x == 0 and y == ind_row:
dx = 0 - map_of_heights[y][x+1]
dy = 0 - map_of_heights[y-1][x]
elif x == ind_column and y == ind_row:
dx = map_of_heights[y][x-1] - 0
dy = 0 - map_of_heights[y-1][x]
return math.sqrt(dx**2 + dy**2)
#the test height maps
test_maps = [
[[0, 1, 2],
[2, 10, 4],
[3, 4, 5]],
[[10, 1, 2],
[2, 3, 4],
[3, 4, 5]],
[[0, 1, 2],
[2, 3, 4],
[3, 4, 10]],
[[0, 1, 10],
[2, 3, 10],
[3, 4, 10]]]
Например, в приведенных выше тестовых картах, когда x = 1 иy = 1, для первой сетки:
[[0, 1, 2],
[2, 10, 4],
[3, 4, 5]]]
значение, которое я выбираю, равно 10, и поэтому значение слева равно 2, справа - 4, нижнему - 4, верхнему - 1.Затем примените формулу sqrt ((слева - справа) ** 2 + (снизу - вверх) ** 2), 3.605551275463989 в качестве результата.
TypeError: unsupported operand type(s) for -: 'list' and 'list'
#test case
find_slope(test_maps, 1, 1)
#expected output
[3.605551275463989, 3.605551275463989, 3.605551275463989, 8.54400374531753]