You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多网格高度图斜率计算需求及TypeError问题排查求助

Fixing TypeError When Calculating Slope for Multiple 2D Height Maps

Let's break down the problem and fix it step by step:

1. Why the TypeError Happens

Your find_slope function is built to work with a single 2D height map (one list of lists). But when you pass test_maps—which is a collection of 4 separate height maps—as the first argument, the function treats map_of_heights as the entire group of maps.

For example, when accessing map_of_heights[y][x-1] with y=1 and x=1, you're actually grabbing test_maps[1][0] (the list [10, 1, 2]) and map_of_heights[y][x+1] is test_maps[1][2] (the list [3,4,5]). Trying to subtract two lists directly throws the TypeError you encountered.

2. Fix the Core Slope Calculation Logic

First, we need to correct the vertical gradient calculation in your original function. According to your requirement, the vertical gradient is upper height minus lower height, but your current code uses map_of_heights[y+1][x] - map_of_heights[y-1][x] (lower minus upper). We'll adjust this to match your stated logic.

3. Add Support for Multiple Height Maps

We'll create a wrapper function that loops through each height map in test_maps, calls the corrected find_slope function for each, and collects all results into a single list.

Here's the full corrected code:

import math

# Corrected single height map slope calculator
def find_slope(map_of_heights, x, y):
    ind_row = len(map_of_heights) - 1
    ind_column = len(map_of_heights[0]) - 1
    dx = 0
    dy = 0

    # Main logic for inner points
    if 0 < x < ind_column and 0 < y < ind_row:
        dx = map_of_heights[y][x-1] - map_of_heights[y][x+1]  # Left - Right (matches your requirement)
        dy = map_of_heights[y-1][x] - map_of_heights[y+1][x]  # Upper - Lower (fixed to match your logic)
    # Edge cases (non-corner edges)
    elif x == 0 and 0 < 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 0 < 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 == ind_column and 0 < 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 0 < x < ind_column:
        dx = map_of_heights[y][x-1] - map_of_heights[y][x+1]
        dy = map_of_heights[y-1][x] - 0
    # Corner cases
    elif x == 0 and y == 0:
        dx = 0 - map_of_heights[y][x+1]
        dy = 0 - map_of_heights[y+1][x]
    elif x == ind_column and y == 0:
        dx = map_of_heights[y][x-1] - 0
        dy = 0 - map_of_heights[y+1][x]
    elif x == 0 and y == ind_row:
        dx = 0 - map_of_heights[y][x+1]
        dy = map_of_heights[y-1][x] - 0
    elif x == ind_column and y == ind_row:
        dx = map_of_heights[y][x-1] - 0
        dy = map_of_heights[y-1][x] - 0

    return math.sqrt(dx**2 + dy**2)

# Wrapper function to handle multiple height maps
def find_slopes_for_maps(maps_list, x, y):
    return [find_slope(height_map, x, y) for height_map in maps_list]

# Test the code with your sample data
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]]
]

# Get the expected output
result = find_slopes_for_maps(test_maps, 1, 1)
print(result)
# Output: [3.605551275463989, 3.605551275463989, 3.605551275463989, 8.54400374531753]

Key Changes Made:

  • Fixed vertical gradient: Adjusted the dy calculation to use upper height - lower height to align with your specified logic.
  • Added wrapper function: find_slopes_for_maps iterates over each height map in the input list, computes the slope for the given (x,y) position, and returns all results in a list.
  • Simplified variable initialization: Set dx and dy to 0 upfront to avoid any potential undefined variable issues.

How to Use:

Instead of calling find_slope(test_maps, 1, 1), use find_slopes_for_maps(test_maps, 1, 1) to get the list of slopes for all 4 height maps.

内容的提问来源于stack exchange,提问作者BCKN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:00:59