1+ // P_2_2_4_02.pde
2+ //
3+ // Generative Gestaltung, ISBN: 978-3-87439-759-9
4+ // First Edition, Hermann Schmidt, Mainz, 2009
5+ // Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni
6+ // Copyright 2009 Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni
7+ //
8+ // http://www.generative-gestaltung.de
9+ //
10+ // Licensed under the Apache License, Version 2.0 (the "License");
11+ // you may not use this file except in compliance with the License.
12+ // You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
13+ // Unless required by applicable law or agreed to in writing, software
14+ // distributed under the License is distributed on an "AS IS" BASIS,
15+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+ // See the License for the specific language governing permissions and
17+ // limitations under the License.
18+
19+ /**
20+ * limited diffusion aggregation
21+ *
22+ * KEYS
23+ * 1 : toggle draw original position of circles
24+ * s : save png
25+ */
26+
27+ var maxCount = 5000 ; //max count of the cirlces
28+ var currentCount = 1 ;
29+ var newx = [ maxCount ] ;
30+ var newy = [ maxCount ] ;
31+ var x = [ maxCount ] ;
32+ var y = [ maxCount ] ;
33+ var r = [ maxCount ] ; // radius
34+
35+ var drawGhosts = false ;
36+
37+
38+ function setup ( ) {
39+ createCanvas ( 800 , 800 ) ;
40+ smooth ( ) ;
41+
42+ // first circle
43+ x [ 0 ] = width / 2 ;
44+ y [ 0 ] = height / 2 ;
45+ //r[0] = 10;
46+ r [ 0 ] = 360 ;
47+ }
48+
49+
50+ function draw ( ) {
51+ background ( 255 ) ;
52+
53+ strokeWeight ( 0.5 ) ;
54+ //noFill();
55+
56+ // create a random set of parameters
57+ var newR = random ( 1 , 7 ) ;
58+ var newX = random ( 0 + newR , width - newR ) ;
59+ var newY = random ( 0 + newR , height - newR ) ;
60+
61+ var closestDist = 100000000 ;
62+ var closestIndex = 0 ;
63+ // which circle is the closest?
64+ for ( var i = 0 ; i < currentCount ; i ++ ) {
65+ var newDist = dist ( newX , newY , x [ i ] , y [ i ] ) ;
66+ if ( newDist < closestDist ) {
67+ closestDist = newDist ;
68+ closestIndex = i ;
69+ }
70+ }
71+
72+ // aline it to the closest circle outline
73+ var angle = atan2 ( newY - y [ closestIndex ] , newX - x [ closestIndex ] ) ;
74+
75+ newx [ currentCount ] = newX ;
76+ newy [ currentCount ] = newY ;
77+ x [ currentCount ] = x [ closestIndex ] + cos ( angle ) * ( r [ closestIndex ] + newR ) ;
78+ y [ currentCount ] = y [ closestIndex ] + sin ( angle ) * ( r [ closestIndex ] + newR ) ;
79+ r [ currentCount ] = newR ;
80+ currentCount ++ ;
81+
82+ // draw circles at random position and lines
83+ if ( drawGhosts ) {
84+ for ( var i = 1 ; i < currentCount ; i ++ ) {
85+ fill ( 230 ) ;
86+ ellipse ( newx [ i ] , newy [ i ] , r [ i ] * 2 , r [ i ] * 2 ) ;
87+ line ( newx [ i ] , newy [ i ] , x [ i ] , y [ i ] ) ;
88+ }
89+ }
90+
91+ for ( var i = 0 ; i < currentCount ; i ++ ) {
92+ if ( i == 0 ) noFill ( ) ;
93+ else fill ( 50 ) ;
94+ ellipse ( x [ i ] , y [ i ] , r [ i ] * 2 , r [ i ] * 2 ) ;
95+ }
96+
97+ if ( currentCount >= maxCount ) noLoop ( ) ;
98+
99+ }
100+
101+ function keyReleased ( ) {
102+ if ( key == 's' || key == 'S' ) saveCanvas ( gd . timestamp ( ) , 'png' ) ;
103+
104+ if ( key == '1' ) drawGhosts = ! drawGhosts ;
105+ }
0 commit comments