@@ -12,6 +12,21 @@ Class {
12
12
#category : #' GM-TE-UI'
13
13
}
14
14
15
+ {
16
+ #category : #forms ,
17
+ #' squeak_changestamp' : ' JS 7/11/2024 15:40'
18
+ }
19
+ GMTEBrush >> calculateOffsetsForRadius: aRadius [
20
+
21
+ |offsets |
22
+ offsets := OrderedCollection new .
23
+ (0 - radius to: radius) do: [:dx |
24
+ (0 - radius to: radius) do: [:dy |
25
+ ((dx squared + dy squared) <= aRadius squared) ifTrue: [offsets add: dx @ dy]]].
26
+
27
+ ^ offsets
28
+ ]
29
+
15
30
{
16
31
#category : #accessing ,
17
32
#' squeak_changestamp' : ' Valentin Teutschbein 7/6/2024 12:48'
@@ -129,15 +144,6 @@ GMTEBrush >> initialize [
129
144
self resetOutputSet
130
145
]
131
146
132
- {
133
- #category : #forms ,
134
- #' squeak_changestamp' : ' JS 7/11/2024 14:04'
135
- }
136
- GMTEBrush >> isPointInRadius: aPoint [
137
- self flag: ' to long? - refactor with lineBrush in mind' .
138
- ^ (( self currentMatrixIndex x - aPoint x) squared + ( self currentMatrixIndex y - aPoint y) squared <= (self radius - 1 ) squared)
139
- ]
140
-
141
147
{
142
148
#category : #accessing ,
143
149
#' squeak_changestamp' : ' Valentin Teutschbein 7/6/2024 14:51'
@@ -156,51 +162,32 @@ GMTEBrush >> layer: anObject [
156
162
157
163
{
158
164
#category : #forms ,
159
- #' squeak_changestamp' : ' JS 7/11/2024 14:09 '
165
+ #' squeak_changestamp' : ' JS 7/11/2024 16:16 '
160
166
}
161
167
GMTEBrush >> lineBrush [
162
- | radius collection start end deltaX deltaY stepX stepY error error2 x y offsets |
163
- self flag: ' refactor - do instead of collect or select' .
164
- radius := self radius - 1 .
165
- self resetOutputSet.
166
- self flag: ' todo: method extraction' .
167
- " Helper method to generate radius offsets"
168
- offsets := OrderedCollection new .
169
- (0 - radius to: radius) do: [:dx |
170
- (0 - radius to: radius) do: [:dy |
171
- ((dx * dx) + (dy * dy) <= (radius * radius)) ifTrue: [offsets add: dx @ dy]]].
172
168
169
+ | offsets |
173
170
self resetOutputSet.
174
171
(self currentMatrixIndex isNil or : [self firstMatrixIndex isNil]) ifTrue: [^ nil ].
172
+
173
+ offsets := self calculateOffsetsForRadius: self offsetCorrectedRadius.
174
+
175
+ (self rasterizeLineBetweenAStart: self firstMatrixIndex andAnEnd: self currentMatrixIndex) do: [:point |
176
+ offsets do: [:offset |
177
+ self outputSet add: (point + offset)]].
175
178
176
- start := self firstMatrixIndex.
177
- end := self currentMatrixIndex.
178
- deltaX := (end x - start x) abs.
179
- deltaY := (end y - start y) abs.
180
- stepX := (start x < end x) ifTrue: [1 ] ifFalse: [- 1 ].
181
- stepY := (start y < end y) ifTrue: [1 ] ifFalse: [- 1 ].
182
- error := deltaX - deltaY.
183
- x := start x.
184
- y := start y.
179
+ ^ (self outputSet)
185
180
186
- collection := OrderedCollection new .
181
+ ]
187
182
188
- [
189
- | point |
190
- point := x @ y.
191
- offsets do: [:offset | collection add: (point + offset)].
192
- (x = end x and : [y = end y]) ifTrue: [
193
- self outputSet: collection asSet.
194
- ^ self outputSet].
195
- error2 := 2 * error.
196
- (error2 > (0 - deltaY)) ifTrue: [
197
- error := error - deltaY.
198
- x := x + stepX].
199
- (error2 < deltaX) ifTrue: [
200
- error := error + deltaX.
201
- y := y + stepY]
202
- ] repeat
183
+ {
184
+ #category : #accessing ,
185
+ #' squeak_changestamp' : ' JS 7/11/2024 16:16'
186
+ }
187
+ GMTEBrush >> offsetCorrectedRadius [
188
+ " In order to display a more intuitive radius for the user, we need to offset it by 1 for the calculations"
203
189
190
+ ^ self radius - 1
204
191
]
205
192
206
193
{
@@ -237,56 +224,73 @@ GMTEBrush >> radius: anObject [
237
224
238
225
{
239
226
#category : #forms ,
240
- #' squeak_changestamp' : ' JS 7/11/2024 14:06 '
227
+ #' squeak_changestamp' : ' JS 7/11/2024 16:14 '
241
228
}
242
229
GMTEBrush >> radiusBrush [
243
230
244
- | collection xMin xMax yMin yMax |
231
+ | offsets |
245
232
self currentMatrixIndex ifNil: [^ nil ].
246
233
247
- collection := OrderedCollection new .
248
- self flag: ' radius offset is bad' .
249
- xMin := self currentMatrixIndex x - (self radius - 1 ).
250
- xMax := self currentMatrixIndex x + (self radius - 1 ).
251
- yMin := self currentMatrixIndex y - (self radius - 1 ).
252
- yMax := self currentMatrixIndex y + (self radius - 1 ).
253
-
254
- self flag: ' select instead of do?' .
255
- (xMin to: xMax) do: [:x |
256
- (yMin to: yMax) do: [:y |
257
- (self isPointInRadius: x @ y) ifTrue: [collection add: x @ y]]].
234
+ offsets := self calculateOffsetsForRadius: self offsetCorrectedRadius.
258
235
259
- collection do: [:i |
260
- self outputSet add: i ].
236
+ offsets do: [:i |
237
+ self outputSet add: ( self currentMatrixIndex + i) ].
261
238
262
239
^ self outputSet
263
240
264
241
]
265
242
266
243
{
267
244
#category : #forms ,
268
- #' squeak_changestamp' : ' JS 7/11/2024 13:49'
245
+ #' squeak_changestamp' : ' JS 7/11/2024 16:16'
246
+ }
247
+ GMTEBrush >> rasterizeLineBetweenAStart: aStartPoint andAnEnd: anEndPoint [
248
+ " implementation of Bresenhams Line Algorithm"
249
+
250
+ | collection deltaX deltaY stepX stepY error error2 x y |
251
+
252
+ self flag: ' todo: method extraction? - Ich glaube geht schlecht' .
253
+
254
+ deltaX := (anEndPoint x - aStartPoint x) abs.
255
+ deltaY := (anEndPoint y - aStartPoint y) abs.
256
+ stepX := (aStartPoint x < anEndPoint x) ifTrue: [1 ] ifFalse: [- 1 ].
257
+ stepY := (aStartPoint y < anEndPoint y) ifTrue: [1 ] ifFalse: [- 1 ].
258
+ error := deltaX - deltaY.
259
+ x := aStartPoint x.
260
+ y := aStartPoint y.
261
+
262
+ collection := OrderedCollection new .
263
+
264
+ [
265
+ | point |
266
+ point := x @ y.
267
+ collection add: point.
268
+ (x = anEndPoint x and : [y = anEndPoint y]) ifTrue: [
269
+ ^ collection].
270
+ error2 := 2 * error.
271
+ (error2 > (0 - deltaY)) ifTrue: [
272
+ error := error - deltaY.
273
+ x := x + stepX].
274
+ (error2 < deltaX) ifTrue: [
275
+ error := error + deltaX.
276
+ y := y + stepY]
277
+ ] repeat
278
+
279
+ ]
280
+
281
+ {
282
+ #category : #forms ,
283
+ #' squeak_changestamp' : ' JS 7/11/2024 16:29'
269
284
}
270
285
GMTEBrush >> rectangleBrush [
271
286
272
- | collection startRow endRow startCol endCol |
273
287
self resetOutputSet.
274
288
(self currentMatrixIndex isNil or : [self firstMatrixIndex isNil]) ifTrue: [^ nil ].
275
- collection := OrderedCollection new .
276
289
277
- " Determine the starting and ending rows and columns"
278
- self flag: ' method extraction' .
279
- startRow := (self currentMatrixIndex x min: self firstMatrixIndex x).
280
- endRow := (self currentMatrixIndex x max: self firstMatrixIndex x).
281
- startCol := (self currentMatrixIndex y min: self firstMatrixIndex y).
282
- endCol := (self currentMatrixIndex y max: self firstMatrixIndex y).
283
-
284
- " Fill the collection with all indices within the rectangle"
285
- startRow to: endRow do: [:row |
286
- startCol to: endCol do: [:col |
287
- collection add: (row @ col)]].
288
-
289
- self outputSet: collection asSet.
290
+ self flag: ' besser machen?' .
291
+ (self currentMatrixIndex x min: self firstMatrixIndex x) to: (self currentMatrixIndex x max: self firstMatrixIndex x) do: [:row |
292
+ (self currentMatrixIndex y min: self firstMatrixIndex y) to: (self currentMatrixIndex y max: self firstMatrixIndex y) do: [:col |
293
+ self outputSet add: (row @ col)]].
290
294
291
295
^ self outputSet
292
296
0 commit comments