@@ -44,12 +44,15 @@ def circle_outline(options = {})
44
44
45
45
angle = 0
46
46
while angle < 360
47
- new_pixel = point_at_distance_angle ( point : { x : x , y : y } , distance : radius , angle : angle )
47
+ new_pixel = point_at_distance_angle ( point : { x : x , y : y } ,
48
+ distance : radius ,
49
+ angle : angle )
48
50
49
51
new_pixel [ :x ] = new_pixel [ :x ] . floor
50
52
new_pixel [ :y ] = new_pixel [ :y ] . floor
51
53
52
- possible_pixel = { x : new_pixel [ :x ] , y : new_pixel [ :y ] , w : 1 , h : 1 , r : r , g : g , b : b , a : a } . solid!
54
+ possible_pixel = { x : new_pixel [ :x ] , y : new_pixel [ :y ] , w : 1 , h : 1 ,
55
+ r : r , g : g , b : b , a : a } . solid!
53
56
54
57
pixels << possible_pixel unless pixels . include? ( possible_pixel )
55
58
angle += 1
@@ -82,7 +85,8 @@ def circle_raster(options = {})
82
85
pixels [ x_min ] ||= [ ]
83
86
y_min = y - radius
84
87
while y_min <= y_max
85
- possible_pixel = { x : x_min , y : y_min , w : 1 , h : 1 , r : r , g : g , b : b , a : a } . solid!
88
+ possible_pixel = { x : x_min , y : y_min , w : 1 , h : 1 ,
89
+ r : r , g : g , b : b , a : a } . solid!
86
90
pixels [ x_min ] [ y_min ] = possible_pixel if args . geometry . point_inside_circle? ( { x : x_min , y : y_min } , { x : x , y : y } , radius )
87
91
y_min += 1
88
92
end
@@ -108,19 +112,19 @@ def point_inside_triangle?(point:, triangle:)
108
112
triangle = triangle . reverse
109
113
110
114
leg0 = [ triangle [ 0 ] , triangle [ 1 ] ]
111
- leg0_slope = args . geometry . line_slope ( leg0 . flatten )
115
+ leg0_slope = args . geometry . line_slope ( leg0 . flatten , replace_infinity : 1080 )
112
116
leg0_intercept = triangle [ 0 ] [ 1 ] - ( leg0_slope * triangle [ 0 ] [ 0 ] )
113
117
114
118
return false unless point_y <= leg0_slope * point_x + leg0_intercept
115
119
116
120
leg1 = [ triangle [ 0 ] , triangle [ 2 ] ]
117
- leg1_slope = args . geometry . line_slope ( leg1 . flatten )
121
+ leg1_slope = args . geometry . line_slope ( leg1 . flatten , replace_infinity : 1080 )
118
122
leg1_intercept = triangle [ 0 ] [ 1 ] - ( leg1_slope * triangle [ 0 ] [ 0 ] )
119
123
120
124
return false unless point_y <= leg1_slope * point_x + leg1_intercept
121
125
122
126
leg2 = [ triangle [ 1 ] , triangle [ 2 ] ]
123
- leg2_slope = args . geometry . line_slope ( leg2 . flatten )
127
+ leg2_slope = args . geometry . line_slope ( leg2 . flatten , replace_infinity : 1080 )
124
128
leg2_intercept = triangle [ 2 ] [ 1 ] - ( leg2_slope * triangle [ 2 ] [ 0 ] )
125
129
126
130
return false unless point_y >= leg2_slope * point_x + leg2_intercept
@@ -166,61 +170,101 @@ def triangle_raster(options = {})
166
170
y2 = options [ :y2 ]
167
171
x3 = options [ :x3 ]
168
172
y3 = options [ :y3 ]
169
- r = options [ :r ]
170
- g = options [ :g ]
171
- b = options [ :b ]
172
- a = options [ :a ]
173
+
174
+ r = options [ :r ] . nil? ? 0 : options [ :r ]
175
+ g = options [ :g ] . nil? ? 0 : options [ :g ]
176
+ b = options [ :b ] . nil? ? 0 : options [ :b ]
177
+ a = options [ :a ] . nil? ? 255 : options [ :a ]
178
+
179
+ path = options [ :path ] . nil? ? 'pixel' : options [ :path ]
180
+
181
+ image_width = options [ :image_width ] . nil? ? [ x , x2 , x3 ] . max - [ x , x2 , x3 ] . min : options [ :image_width ]
173
182
174
183
args = $gtk. args
175
184
triangle = [ [ x , y ] , [ x2 , y2 ] , [ x3 , y3 ] ]
176
185
triangle = triangle . sort_by { |point | point [ 1 ] }
177
186
triangle = triangle . reverse
178
187
179
- line_slope = args . geometry . line_slope [ triangle [ 0 ] [ 0 ] , triangle [ 0 ] [ 1 ] , triangle [ 2 ] [ 0 ] , triangle [ 2 ] [ 1 ] ]
188
+ line_slope = args . geometry . line_slope ( [ triangle [ 0 ] [ 0 ] , triangle [ 0 ] [ 1 ] , triangle [ 2 ] [ 0 ] , triangle [ 2 ] [ 1 ] ] , replace_infinity : 1080 )
180
189
x_intercept = triangle [ 0 ] [ 1 ] - ( line_slope * triangle [ 0 ] [ 0 ] )
181
190
182
191
vertex4 = [ ( triangle [ 1 ] [ 1 ] - x_intercept ) / line_slope , triangle [ 1 ] [ 1 ] ]
183
192
184
193
leg0 = [ triangle [ 0 ] , triangle [ 1 ] ]
185
- leg0_slope = args . geometry . line_slope ( leg0 . flatten )
194
+ leg0_slope = args . geometry . line_slope ( leg0 . flatten , replace_infinity : 1080 )
186
195
leg0_intercept = triangle [ 0 ] [ 1 ] - ( leg0_slope * triangle [ 0 ] [ 0 ] )
187
196
188
197
leg1 = [ triangle [ 0 ] , vertex4 ]
189
- leg1_slope = args . geometry . line_slope ( leg1 . flatten )
198
+ leg1_slope = args . geometry . line_slope ( leg1 . flatten , replace_infinity : 1080 )
190
199
leg1_intercept = triangle [ 0 ] [ 1 ] - ( leg1_slope * triangle [ 0 ] [ 0 ] )
191
200
192
- leg2 = [ triangle [ 2 ] , triangle [ 1 ] ]
193
- leg2_slope = args . geometry . line_slope ( leg2 . flatten )
201
+ leg2 = [ triangle [ 1 ] , triangle [ 2 ] ]
202
+ leg2_slope = args . geometry . line_slope ( leg2 . flatten , replace_infinity : 1080 )
194
203
leg2_intercept = triangle [ 2 ] [ 1 ] - ( leg2_slope * triangle [ 2 ] [ 0 ] )
195
204
196
- leg3 = [ triangle [ 2 ] , vertex4 ]
197
- leg3_slope = args . geometry . line_slope ( leg3 . flatten )
205
+ leg3 = [ vertex4 , triangle [ 2 ] ]
206
+ leg3_slope = args . geometry . line_slope ( leg3 . flatten , replace_infinity : 1080 )
198
207
leg3_intercept = triangle [ 2 ] [ 1 ] - ( leg3_slope * triangle [ 2 ] [ 0 ] )
199
208
200
209
raster_lines = [ ]
201
210
211
+ x_offset = [ x , x2 , x3 ] . min
212
+ y_offset = [ y , y2 , y3 ] . min
213
+
202
214
y_iter = triangle [ 0 ] [ 1 ]
203
215
while y_iter >= vertex4 [ 1 ]
216
+ start_x = ( y_iter - leg0_intercept ) / leg0_slope
217
+ end_x = ( y_iter - leg1_intercept ) / leg1_slope
218
+
219
+ sort_list = [ start_x , end_x ] . sort
220
+ small_x = sort_list [ 0 ]
221
+ big_x = sort_list [ 1 ]
222
+
223
+ start_x = small_x
224
+ end_x = big_x
225
+
226
+ draw_width = end_x - x_offset > image_width ? image_width - start_x + x_offset : end_x - start_x
227
+
228
+ grab = draw_width . abs
204
229
raster_lines << {
205
- x : ( y_iter - leg0_intercept ) / leg0_slope ,
230
+ x : start_x ,
206
231
y : y_iter ,
207
- x2 : ( y_iter - leg1_intercept ) / leg1_slope ,
208
- y2 : y_iter ,
209
- r : r , g : g , b : b , a : a
210
- } . line!
232
+ w : draw_width ,
233
+ h : 1 ,
234
+ r : r , g : g , b : b , a : a ,
235
+ path : path ,
236
+ source_x : start_x - x_offset , source_y : y_iter - y_offset ,
237
+ source_w : grab , source_h : 1
238
+ } . sprite!
211
239
y_iter -= 1
212
240
end
213
241
214
- y_iter = triangle [ 2 ] [ 1 ]
215
- while y_iter <= vertex4 [ 1 ]
242
+ y_iter = vertex4 [ 1 ]
243
+ while y_iter >= triangle [ 2 ] [ 1 ]
244
+ start_x = ( y_iter - leg2_intercept ) / leg2_slope
245
+ end_x = ( y_iter - leg3_intercept ) / leg3_slope
246
+
247
+ sort_list = [ start_x , end_x ] . sort
248
+ small_x = sort_list [ 0 ]
249
+ big_x = sort_list [ 1 ]
250
+
251
+ start_x = small_x
252
+ end_x = big_x
253
+
254
+ draw_width = end_x - x_offset > image_width ? image_width - start_x + x_offset : end_x - start_x
255
+
256
+ grab = draw_width . abs
216
257
raster_lines << {
217
- x : ( y_iter - leg2_intercept ) / leg2_slope ,
258
+ x : start_x ,
218
259
y : y_iter ,
219
- x2 : ( y_iter - leg3_intercept ) / leg3_slope ,
220
- y2 : y_iter ,
221
- r : r , g : g , b : b , a : a
222
- } . line!
223
- y_iter += 1
260
+ w : draw_width ,
261
+ h : 1 ,
262
+ r : r , g : g , b : b , a : a ,
263
+ path : path ,
264
+ source_x : start_x - x_offset , source_y : y_iter - y_offset ,
265
+ source_w : grab , source_h : 1
266
+ } . sprite
267
+ y_iter -= 1
224
268
end
225
269
raster_lines
226
270
end
0 commit comments