4

我有一个点列表,必须进行侵蚀/膨胀操作。我需要一种二维数组,但在 VisualWorks 中找不到方法(我知道 Squeak 中有一个 Array2d 类,但我必须使用 VW)。

4

5 回答 5

4

许多 Smalltalk 实现将具有某种 Matrix 类,有时会进行优化,该类将具有诸如 #rowAt:columnAt: (或为简洁起见 #at:at:) 之类的方法。

在 GNU Smalltalk 中,它位于包 DhbNumericalMethods 中。不过现在还没有优化。

于 2010-06-23T00:33:23.960 回答
3

简单地使用一种通用方式:数组数组:

(Array new: xSize)
    at: 1 put: ((Array new: ySize) at: 1 put: aValue; at: 2 put: aValue; ...);
    at: 2 put: ((Array new: ySize) at: 1 put: aValue; at: 2 put: aValue; ...);
    ...
于 2010-06-22T18:31:10.853 回答
2

如果您希望操作高效,请研究 VisualWorks Image 类,协议“图像处理”和“位处理”。基于那里的图元构建您自己的腐蚀/膨胀操作。

于 2010-06-22T23:48:11.190 回答
2

这是在 Squeak 中处理二维数组的另一种方法(我使用的是 4.2 版)。

test := Matrix new: 3.     "this defines a 3 x 3 array"
test at: 1 at: 1 put: 5.
test at: 1 at: 2 put: 6.
test at: 1 at: 3 put: 7.

等等等等。AFAIK 你只能用这种方式做二维数组,它们必须是方阵。这对于我儿子和我正在开发的数独游戏 ymmv 的项目非常有效。干杯!

于 2013-01-30T18:29:20.817 回答
0
Array subclass: Array2D

instanceVariableNames: 'myRows myColumns'

classVariableNames: ''
poolDictionaries: ''

category: 'Basic Data Structures'

"I am a two-dimensional array of arbitrary objects.

[Privately, I am really a linear (one-dimensional) array.
I locate my elements internally by index arithmetic on their
(two-dimensional) coordinates.]"


Instance creation (class)
-------------------------

new: nRows by: nColumns
   "Create a new instance of me with nRows rows and nColumns
    columns."
   
   ^(super new: (nRows * nColumns))
       withRows: nRows withColumns: nColumns
   
   "exampleArray := Array2D new: 10 by: 5"


Initialization
--------------

withRows: nRows withColumns: nColumns
   "Set my number of rows and columns to nRows and nColumns,
   respectively.
"
   
   myRows    := nRows.
   myColumns := nColumns


Properties
----------

rows
   "My number of rows."
   
   ^myRows


columns
   "My number of columns."
   
   ^myColumns


Element access
--------------

atRow: whichRow atColumn: whichColumn
   "My element at row whichRow and column whichColumn."
   
   ^super at: (self indexAtRow: whichRow
                      atColumn: whichColumn)
   
   "exampleValue := exampleArray atRow: 6 atColumn: 4"


atRow: whichRow atColumn: whichColumn put: newValue
   "Store value newValue as my element at row whichRow and
    column whichColumn."
   
   super at: (self indexAtRow: whichRow
                     atColumn: whichColumn)
        put: newValue
   
   "exampleArray atRow: 6 atColumn: 4 put: exampleValue"


Private
-------

indexAtRow: whichRow atColumn: whichColumn
   "The internal index at which I store my element at row
    whichRow and column whichColumn.
"
   
   ^((whichRow - 1) * myColumns) + whichColumn
于 2021-05-08T22:15:31.610 回答