私はC++で書いたプログラムをPythonに移植しようとしています。具体的には、高さマップを生成するためのプログラムで、3Dモデルにエクスポートすることができ、理想的には見たときに現実的な地形のように見えます。Pythonの2次元配列が異常に動作しています
HeightMapクラスは、float値の2次元配列をラップします。今のところ、整数として扱っています。なぜなら、それはより良い印刷を行うからです。他の機能を実装する機会はありませんでした。私が持っている不具合は、単に "self.rows [y] [x] = value"というコードでx、yの値をvalueに設定するset(self、x、y、value) self.rowsの各メンバーを訪問し、その配列のx番目のメンバーをvalueに設定して、列全体を変更するように見えます。
ここに私のコードです。私は間違いが2アレイの初期化のどこかにあることを約90%確信しています。
def filledArray(length, value) :
result = []
for i in range(1, length) :
result.append(value)
return result
def resizeArray(array, newLength, nullValue) :
if newLength == len(array) :
return array
result = []
for i in range(0, newLength) :
if i < len(array) :
result.append(array[i])
else :
result.append(nullValue)
return result
class HeightMap:
"""A class that wraps a 2D array for generating height maps"""
def __init__(self) :
self.width = 0
self.height = 0
self.rows = []
def __init__(self, initWidth, initHeight) :
self.clear(initWidth, initHeight);
def clear(self, initWidth, initHeight) :
self.width = initWidth
self.height = initHeight
self.rows = filledArray(initHeight, filledArray(initWidth, 0))
def setHeight(self, newHeight) :
if self.height == newHeight :
return
self.rows = resizeArray(self.rows, newHeight, filledArray(self.width, 0))
def setWidth(self, newWidth) :
if self.width == newWidth :
return
for i in range(0, len(self.rows)) :
self.rows[i] = resizeArray(self.rows[i], newWidth, 0);
self.width = newWidth
def get(self, x, y) :
return self.rows[y][x]
def set(self, x, y, value) :
self.rows[y][x] = value
def add(self, x, y, value) :
self.rows[y][x] += value
def multiply(self, x, y, value) :
self.rows[y][x] *= value
これは狂っていますが、pythonは高水準な言語なので、 'numpy'配列を使うだけです。必要に応じて、アレイインタフェースの周りにラッパーを書きます。それははるかに速く、はるかに少ないエラーが発生しやすくなります。 – wim