55# features use of Vec2D class and grid method
66load_library :control_panel
77
8- attr_reader :panel , :hide , : image_mask, :spots
8+ attr_reader :image_mask , :positions
99ACCURACY = 4
1010
1111# Firefly holder
12- Flyer = Struct . new ( :pos , :to_pos , :rotation , :positions )
12+ Flyer = Struct . new ( :pos , :to_pos , :rotation , :tail )
1313
1414def settings
1515 size 1024 , 480 , P2D
@@ -22,18 +22,16 @@ def setup
2222 control . title 'Firefly Controller'
2323 control . look_feel 'Nimbus'
2424 control . slider ( :speed , 0 ..20 , 5 )
25- control . slider ( :tail_length , 0 ..400 , 30 )
25+ control . slider ( :tail_length , 0 ..70 , 30 )
2626 control . slider ( :rotation_max , 0 ..30 , 7 )
2727 control . slider ( :target_radius , 5 ...100 , 20 )
28- control . slider ( :spot_distance , 5 .. 200 , 80 )
28+ control . slider ( :spot_distance , 10 .. 100 , 80 )
2929 control . button :reset
30- @panel = control
3130 end
32- @hide = false
3331 @spotlight = create_spotlight
3432 @background = load_image ( data_path ( 'background.png' ) )
3533 @image_mask = load_image ( data_path ( 'mask.png' ) )
36- load_spots ( image_mask , ACCURACY )
34+ load_positions ( image_mask , ACCURACY )
3735 reset
3836end
3937
@@ -42,10 +40,6 @@ def reset
4240end
4341
4442def draw
45- unless hide
46- @hide = true
47- panel . set_visible ( hide )
48- end
4943 image @background , 0 , 0
5044 draw_lights
5145 draw_flyers
@@ -85,8 +79,8 @@ def draw_flyers
8579 flyer . rotation = to_rotation if flyer . rotation < to_rotation
8680 end
8781 # add tail position
88- flyer . positions << flyer . pos . dup
89- flyer . positions . shift while flyer . positions . size > @tail_length
82+ flyer . tail << flyer . pos . copy
83+ flyer . tail . shift while flyer . tail . length > @tail_length
9084 # set flyer position
9185 flyer . pos . x = flyer . pos . x + @speed * cos ( flyer . rotation )
9286 flyer . pos . y = flyer . pos . y + @speed * sin ( flyer . rotation )
@@ -103,42 +97,38 @@ def draw_flyers
10397end
10498
10599def create_flyer
106- spot = rand_spot
100+ spot = positions . sample
107101 to_spot = find_spot_near spot , @spot_distance
108102 rotation = rand * TWO_PI
109103 Flyer . new ( spot , to_spot , rotation , [ ] )
110104end
111105
112106def draw_tail ( flyer )
113- positions = flyer . positions
114- return unless positions && !positions . empty?
115- alpha_add = ( 255 / positions . size ) . to_i
116- positions . each_index do |i |
117- stroke ( 255 , i * alpha_add )
118- if i < positions . size - 2
119- line ( positions [ i ] . x , positions [ i ] . y , positions [ i + 1 ] . x , positions [ i + 1 ] . y )
120- end
107+ tail = flyer . tail
108+ return unless tail && !tail . empty?
109+ alpha_add = 100.0 / tail . length
110+ begin_shape ( LINES )
111+ tail . each_with_index do |vec , i |
112+ stroke ( color ( 255 , i * alpha_add ) )
113+ vertex ( vec . x , vec . y )
121114 end
115+ end_shape
122116end
123117
124- def load_spots ( spot_image , accuracy = ACCURACY )
125- @spots = [ ]
118+ def load_positions ( spot_image , accuracy = ACCURACY )
119+ @positions = [ ]
126120 spot_image . load_pixels
127- corner_color = spot_image . get 0 , 0
128- grid ( spot_image . width , spot_image . height , accuracy , accuracy ) do |x , y |
129- color = spot_image . get ( x , y )
130- spots << Vec2D . new ( x , y ) if color != corner_color
121+ grid ( width , height , accuracy , accuracy ) do |x , y |
122+ positions << Vec2D . new ( x , y ) if color ( 0 ) == spot_image . pixels [ y * width + x ]
131123 end
132124end
133125
134- def rand_spot
135- spots . sample
136- end
137-
138126def find_spot_near ( pos , distance )
139- spot = Vec2D . new ( Float ::INFINITY , Float ::INFINITY )
140- spot = rand_spot until spot . dist ( pos ) < distance
141- spot
127+ Vec2D . new . tap do |spot |
128+ spot . x = rand ( 0 ..width ) # target width
129+ spot . y = rand ( 140.0 ..310 ) # target text area
130+ spot = positions . sample until spot . dist ( pos ) < distance
131+ end
142132end
143133
144134def find_nearest_rotation ( from , to )
@@ -155,7 +145,7 @@ def create_spotlight
155145 spotlight = buffer ( size , size , P2D ) do |buffer |
156146 buffer . no_stroke
157147 buffer . fill 255 , 60
158- # spotlight.fill 255, 40
148+ # spotlight.fill 255, 40
159149 buffer . ellipse half_size , half_size , half_size , half_size
160150 buffer . filter BLUR , 4
161151 end
0 commit comments