Comment detail

格子点の列挙 (Nested Flatten)
Squeak Smalltalk で。

1. 円形のブラシを描く機能(#dotOfSize:)があるので、これで描いた円形のドット数を数え(primCountBits)て、同心円内に列挙すべき格子点をすべて含む最少の矩形格子を探し、その x (もしくは y)の最大値(max)を求めます。

2. -max から max まで整数で、重複を許す二項の順列組み合わせを生成(asDigitsToPower: 2 do: [...])し、それぞれの項目を x、y に持つ、ポイントオブジェクト(x@y)としてコレクション(points)に収めます。

3. pointsを、各要素の原点からの距離(r)、距離が同じなら x 軸となす角度(theta)の小さい順に並べ変えます。

4. 前から n 個をとり(first: n)、各ポイントの x、y を出力。終了後、ファイルを開いて表示(edit)し、最後の出力を結果として返して(^last)います。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
| n count max out points last |
n := 1000.
count := [:m | (Form dotOfSize: (m + 1 * 2)) primCountBits - (m + 1 * 4) + 1].
max := (1 to: Float infinity) detect: [:r | (count value: r) >= n].
out := FileStream forceNewFileNamed: 'doukaku65.out.txt'.
points := OrderedCollection new: max * max.
(max negated to: max) asDigitsToPower: 2 do: [:xy | points add: xy first @ xy second].
points := points asArray sort: [:a :b | a r < b r or: [a r = b r and: [a theta < b theta]]].
points := points first: n. 
points do: [:pt | out nextPutAll: (last := '{1}, {2}' format: {pt x. pt y}); cr].
out edit.
^last   "=> '-8, 16' "

Index

Feed

Other

Link

Pathtraq

loading...