View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide

processing examples+

Home   How To   Code Pool   Public Library   Theory   Events
ported straight from processing example folder. see http://processing.org/. all credits for these drawings to the authors of those examples.

note: these are the UserView:animate versions (available in recent osx version of supercollider only)
see http://swiki.hfbk-hamburg.de:8888/MusicTechnology/875 if you use swingosc (linux/win).

Processing-Book-001 / Illustrations

//updated 100228 to use UserView:animate
//updated 171229 changed from Pen.setSmoothing to Pen.smoothing_

(
	var w= Window("processing - page_022", Rect(100, 100, 713, 938), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var ydiv, s, x;
	u.background= Color.white;
	ydiv= u.bounds.height/54;
	u.drawFunc= {
		Pen.width= 0.25;
		Pen.fillColor= Color.black;
		0.forBy(u.bounds.height, ydiv, {|y|
			0.forBy(u.bounds.width, ydiv, {|x|
				Pen.addOval(Rect(x, y, 1, 1));
			});
		});
		Pen.fill;
		s= ydiv*10;
		x= ydiv*28;
		Pen.fillOval(Rect.aboutPoint(Point(x, ydiv*12), s/2, s/2));
		Pen.fillRect(Rect.aboutPoint(Point(x, ydiv*26), ydiv*12/2, ydiv*12/2));
		Pen.fillColor= Color.white;
		Pen.fillOval(Rect.aboutPoint(Point(x, ydiv*26), s/2, s/2));
		Pen.fillColor= Color.black;
		Pen.fillOval(Rect.aboutPoint(Point(x, ydiv*40), s*1.07/2, s*1.07/2));
		Pen.fillColor= Color.white;
		Pen.fillRect(Rect.aboutPoint(Point(x, ydiv*40), s*0.7/2, s*0.7/2));
	};
	w.front;
)
(
	var w= Window("processing - page_042", Rect(100, 100, 442, 550), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	u.background= Color.white;
	u.drawFunc= {
		var x1= 0, x2= 0, x3= 0, x5= 0, x4= 0;
		Pen.smoothing_(false);
		Pen.width= 0.25;
		51.do{|i|
			Pen.line(Point(u.bounds.width-x2, u.bounds.height*0.0), Point(u.bounds.width-x2, u.bounds.height*0.2));
			x2= x2+4.5;
			Pen.line(Point(u.bounds.width-x1, u.bounds.height*0.2), Point(u.bounds.width-x1, u.bounds.height*0.4));
			x1= x1+9;
			Pen.line(Point(u.bounds.width-x3, u.bounds.height*0.4), Point(u.bounds.width-x3, u.bounds.height*0.6));
			x3= x3+6.25;
			Pen.line(Point(u.bounds.width-x5, u.bounds.height*0.6), Point(u.bounds.width-x5, u.bounds.height*0.8));
			x5= x5+3.125;
			Pen.line(Point(u.bounds.width-x4, u.bounds.height*0.8), Point(u.bounds.width-x4, u.bounds.height));
			x4= x4+5.375;
		};
		Pen.stroke;
	};
	w.front;
)
(
	var w= Window("processing - page_060", Rect(100, 100, 180, 666), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	u.background= Color.white;
	u.drawFunc= {
		Pen.smoothing_(false);
		Pen.width= 0.25;
		0.forBy(u.bounds.height, 5, {|y|
			0.forBy(u.bounds.width, 5, {|x|
				if(x%20==0, {
					Pen.line(Point(x, y), Point(x-3, y-3));
				}, {
					Pen.line(Point(x, y), Point(x-3, y+3));
				});
			});
		});
		Pen.stroke;
	};
	w.front;
)
(
	var w= Window("processing - page_078", Rect(100, 100, 360, 550), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	u.background= Color.white;
	u.drawFunc= {
		var ax= 0, ay= 0;
		Pen.smoothing_(false);
		Pen.strokeColor= Color.black;
		(2..35).do{|e|
			0.forBy(u.bounds.width, 2, {|x|
				var n= x.linlin(0, u.bounds.width, 0, 1);
				var y= 1-n.pow(e);
				y= y*u.bounds.height;
				if(x>0, {
					Pen.line(Point(ax, ay), Point(x, y));
				});
				ax= x;
				ay= y;
			});
		};
		Pen.stroke;
	};
	w.front;
)
(
	var w= Window("processing - page_100", Rect(100, 100, 360, 666), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var s= Font("Helvetica", 34);	//Ziggurat-HTF-Black
	u.background= Color.white;
	u.drawFunc= {
		var a= 37;
		Pen.fillColor= Color.grey(204/255);
		20.forBy(u.bounds.height+38, 38, {|i|
			Pen.font= s.size_(34);
			Pen.stringAtPoint(a.asAscii.asString, Point(50, i-34));
			Pen.font= s.size_(10);
			Pen.stringAtPoint(a.asString, Point(10, i-10-9));
			a= a+1;
		});
		20.forBy(u.bounds.height+38, 38, {|i|
			Pen.font= s.size_(34);
			Pen.stringAtPoint(a.asAscii.asString, Point(170, i-34));
			Pen.font= s.size_(10);
			Pen.stringAtPoint(a.asString, Point(130, i-10-9));
			a= a+1;
		});
		20.forBy(u.bounds.height+38, 38, {|i|
			Pen.font= s.size_(34);
			Pen.stringAtPoint(a.asAscii.asString, Point(290, i-34));
			Pen.font= s.size_(10);
			Pen.stringAtPoint(a.asString, Point(250, i-10-9));
			a= a+1;
		});
	};
	w.front;
)
(
	var w= Window("processing - page_110", Rect(100, 100, 360, 550), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var s= Font("Helvetica", 460);	//Ziggurat-HTF-Black
	u.background= Color.white;
	u.drawFunc= {
		Pen.fillColor= Color.grey(0, 50/255);
		Pen.font= s;
		6.do{|i|
			Pen.stringAtPoint(i.asString, Point(u.bounds.width/2-120, u.bounds.height*0.98-(i*40)-460));
		};
	};
	w.front;
)
(
	var w= Window("processing - page_136", Rect(100, 100, 360, 550), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	u.background= Color.white;
	u.drawFunc= {
		Pen.smoothing_(true);
		Pen.fillColor= Color.grey(0, 10/255);
		Pen.translate(33, 66);
		45.do{|i|
			Pen.scale(1.02, 1.02);
			Pen.fillOval(Rect.aboutPoint(Point(25, 90), 200/2, 200/2));
		};
	};
	w.front;
)
(
	var w= Window("processing - page_144", Rect(100, 100, 440, 666), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	u.background= Color.white;
	u.drawFunc= {
		var scaleVal, angleInc, angle;
		Pen.smoothing_(false);
		Pen.strokeColor= Color.black;
		Pen.width= 0.25;
		Pen.fillColor= Color.black;
		scaleVal= 16;
		angleInc= pi/24;
		angle= 0;
		20.forBy(u.bounds.width-20-5, 5, {|offset|
			0.forBy(u.bounds.height, 2, {|y|
				var x= offset+(sin(angle)*scaleVal);
				Pen.line(Point(x, y), Point(x+0.125, y+0.125));
				angle= angle+angleInc;
			});
			angle= angle+pi;
		});
		Pen.stroke;
	};
	w.front;
)
(
	var w= Window("processing - page_222", Rect(100, 100, 460, 225), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var s= Font("Helvetica", 50);	//Ziggurat-HTF-Black
	u.background= Color.white;
	u.drawFunc= {
		var c1, c2, c3, c4;
		Pen.fillColor= Color.grey(204/255);
		Pen.font= s;
		c1= #[$1, $2, $3, $4, $5, $6, $7, $8, $9, $0, $-, $=];
		c2= #[$Q, $W, $E, $R, $T, $Y, $U, $I, $O, $P, $[, $]];
		c3= #[$A, $S, $D, $F, $G, $H, $J, $K, $L, $;];
		c4= #[$Z, $X, $C, $V, $B, $N, $M, $,, $., $/];
		Pen.strokeColor= Color.grey(204/255);
		Pen.width= 0.5;
		Pen.smoothing_(false);
		c1.do{|c, i|
			Pen.stringAtPoint(c.asString, Point(20+(i*78)-12, 50-50));
			if(i%2!=0, {
				Pen.line(Point(20+(i*39), 40), Point(20+(i*39), 20));
			});
		};
		c2.do{|c, i|
			Pen.stringAtPoint(c.asString, Point(46+(i*78)-12, 105-50));
			if(i%2!=0, {
				Pen.line(Point(46+(i*39), 95), Point(46+(i*39), 75));
			});
		};
		c3.do{|c, i|
			Pen.stringAtPoint(c.asString, Point(65+(i*78)-12, 160-50));
			if(i%2!=0, {
				Pen.line(Point(65+(i*39), 150), Point(65+(i*39), 130));
			});
		};
		c4.do{|c, i|
			Pen.stringAtPoint(c.asString, Point(90+(i*78)-12, 215-50));
			if(i%2!=0, {
				Pen.line(Point(90+(i*39), 205), Point(90+(i*39), 185));
			});
		};
		Pen.stroke;
	};
	w.front;
)
(
	var w= Window("processing - page_228", Rect(100, 100, 360, 666), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var moveX= 0, moveY= 0, dragX= 0, dragY= 0, button= false;
	w.acceptsMouseOver= true;
	u.mouseOverAction= {|v, x, y|
		moveX= x;
		moveY= y;
		u.refresh;
	};
	u.mouseMoveAction= {|v, x, y|
		dragX= x;
		dragY= y;
		u.refresh;
	};
	u.background= Color.white;
	u.drawFunc= {
		Pen.strokeColor= Color.black;
		Pen.strokeOval(Rect.aboutPoint(Point(dragX, dragY), 200/2, 200/2));
		Pen.fillColor= Color.grey(153/255);
		Pen.fillOval(Rect.aboutPoint(Point(moveX, moveY), 200/2, 200/2));
	};
	w.front;
)
(
	var w= Window("processing - page_254", Rect(100, 100, 442, 550), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var gx, gy, mx, my, drawCursor;
	w.acceptsMouseOver= true;
	gx= u.bounds.width;
	gy= u.bounds.height;
	mx= gx/2;
	my= gy/2;
	u.mouseOverAction= {|v, x, y|
		mx= x;
		my= y;
		u.refresh;
	};
	u.background= Color.white;
	u.drawFunc= {
		var rot;
		Pen.strokeColor= Color.grey(0.2);
		16.forBy(gx+2, 32, {|i|
			11.forBy(gy, 30, {|j|
				rot= pi.neg/4+atan2(j-my, i-mx);
				drawCursor.value(i, j, 0.5, rot);
			});
		});
	};
	drawCursor= {|x, y, myscale, myrot|
		Pen.use{
			Pen.translate(x, y);
			Pen.rotate(myrot, 0, 0);
			Pen.scale(myscale, myscale);
			Pen.moveTo(Point(7, 21));
			Pen.lineTo(Point(4, 13));
			Pen.lineTo(Point(1, 16));
			Pen.lineTo(Point(0, 16));
			Pen.lineTo(Point(0, 0));
			Pen.lineTo(Point(1, 0));
			Pen.lineTo(Point(12, 11));
			Pen.lineTo(Point(12, 12));
			Pen.lineTo(Point(7, 12));
			Pen.lineTo(Point(10, 20));
			Pen.lineTo(Point(9, 21));
			Pen.lineTo(Point(7, 21));
			Pen.stroke;
		};
	};
	w.front;
)
(
	var w= Window("processing - page_278", Rect(100, 100, 750, 800), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var spot= {
		var radius= 15*2;
		(
		\radius: radius,
		\x: radius.rrand(u.bounds.width-15),
		\y: radius.rrand(u.bounds.height-15),
		\speedX: 0.4.rrand(1),
		\speedY: 0.4.rrand(1),
		\directionX: 1,
		\directionY: -1,
		\init: {|envir|
			var r= 1.0.rand;
			if(r<0.25, {
				envir.directionX= 1;
				envir.directionY= 1;
			}, {
				if(r<0.5, {
					envir.directionX= -1;
					envir.directionY= 1;
				}, {
					if(r<0.75, {
						envir.directionX= 1;
						envir.directionY= -1;
					}, {
						envir.directionX= -1;
						envir.directionY= -1;
					});
				});
			});
		},
		\updateDisplay: {|envir|
			Pen.addOval(Rect(envir.x, envir.y, envir.radius, envir.radius));
			envir.x= envir.x+(envir.speedX*envir.directionX);
			if(envir.x>(u.bounds.width-envir.radius) or:{envir.x<=0}, {
				envir.directionX= envir.directionX.neg;
			});
			envir.y= envir.y+(envir.speedY*envir.directionY);
			if(envir.y>(u.bounds.height-envir.radius) or:{envir.y<=0}, {
				envir.directionY= envir.directionY.neg;
			});
		}
	)};
	var sps= spot.dup(36).do{|x| x.init};
	thisThread.randSeed= 0;
	u.clearOnRefresh= false;
	u.animate= true;
	u.drawFunc= {
		Pen.fillColor= Color.grey(1, 12/255);
		Pen.fillRect(u.bounds);
		Pen.fillColor= Color.black;
		sps.do{|sps|
			sps.updateDisplay;
		};
		Pen.fill;
	};
	w.front;
)
(
	var w= Window("processing - page_290", Rect(100, 100, 513, 462), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var inc= 0, tail;
	u.background= Color.white;
	u.animate= true;
	u.drawFunc= {
		var angle;
		Pen.smoothing_(true);
		Pen.strokeColor= Color.grey(0, 143/255);
		thisThread.randSeed= 0;
		inc= inc+0.01;
		angle= sin(inc)/10+(sin(inc*1.2)/20);
		-20.forBy(u.bounds.width+20, 20, {|i|
			tail.value(i, 100, 5.rrand(12), angle*1.0.rrand(1.3));
			tail.value(i, 200, 5.rrand(12), angle.neg*1.0.rrand(1.3));
			tail.value(i, 300, 5.rrand(12), angle*1.0.rrand(1.3));
			tail.value(i, 400, 5.rrand(12), angle.neg*1.0.rrand(1.3));
		});
	};
	tail= {|x, y, units, angle|
		Pen.use{
			Pen.translate(x, y);
			units.forBy(0, -1, {|i|
				Pen.width= i/2;
				Pen.line(Point(0, 0), Point(0, -8));
				Pen.translate(0, -8);
				Pen.rotate(angle);
				Pen.stroke;
			});
		};
	};
	w.front;
)
(
	var w= Window("processing - page_300", Rect(100, 100, 180, 666), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var inc= 0, x= 0.dup(u.bounds.height), y= 0.dup(u.bounds.height), mx= 0, my= 0;
	var mouseX= 0, mouseY= 0;
	w.acceptsMouseOver= true;
	u.mouseOverAction= {|v, x, y|
		mouseX= x;
		mouseY= y;
	};
	u.background= Color.white;
	u.animate= true;
	u.drawFunc= {
		Pen.width= 0.25;
		y= y.rotate(1);
		my= my+(mouseX-my*0.1);
		y.put(0, my);
		Pen.use{
			Pen.moveTo(Point(y[1], 0));
			2.forBy(y.size-1, 1, {|i|
				Pen.lineTo(Point(y[i], i));
			});
			Pen.stroke;
		};
		x= x.rotate(1);
		x.put(0, mx);
		mx= mx+(mouseY-mx*0.1);
		Pen.use{
			Pen.moveTo(Point(x[1]*u.bounds.width/u.bounds.height, 0));
			2.forBy(x.size-1, 1, {|i|
				Pen.lineTo(Point(x[i]*u.bounds.width/u.bounds.height, i));
			});
			Pen.stroke;
		};
		
	};
	w.front;
)
(
	var w= Window("processing - page_326", Rect(100, 100, 842, 550), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var f, s, angle= 0;
	s= "012345678901234567890123456789";
	f= Font("Helvetica", 24);
	u.background= Color.white;
	u.animate= true;
	u.drawFunc= {
		var cw= 0;
		Pen.fillColor= Color.grey(204/255);
		Pen.translate(10, 0);
		angle= angle+0.1;
		s.size.do{|i|
			var c= sin(angle+(i/pi));
			Pen.font= f.size_(c+1*18+6);
			Pen.stringAtPoint(s[i].asString, Point(cw, 60));
			cw= cw+(f.size*0.667);
		};
		cw= 0;
		s.size.do{|i|
			var c= sin(angle+(pi/4/2)+(i/pi));
			Pen.font= f.size_(c+1*18+6);
			Pen.stringAtPoint(s[i].asString, Point(cw, 120));
			cw= cw+(f.size*0.667);
		};
		cw= 0;
		s.size.do{|i|
			var c= sin(angle+(pi/4)+(i/pi));
			Pen.font= f.size_(c+1*18+6);
			Pen.stringAtPoint(s[i].asString, Point(cw, 180));
			cw= cw+(f.size*0.667);
		};
		cw= 0;
		s.size.do{|i|
			var c= sin(angle+(pi/4+(pi/4/2))+(i/pi));
			Pen.font= f.size_(c+1*18+6);
			Pen.stringAtPoint(s[i].asString, Point(cw, 240));
			cw= cw+(f.size*0.667);
		};
		cw= 0;
		s.size.do{|i|
			var c= sin(angle+(pi/2)+(i/pi));
			Pen.font= f.size_(c+1*18+6);
			Pen.stringAtPoint(s[i].asString, Point(cw, 300));
			cw= cw+(f.size*0.667);
		};
		cw= 0;
		s.size.do{|i|
			var c= sin(angle+(pi/2+(pi/4/2))+(i/pi));
			Pen.font= f.size_(c+1*18+6);
			Pen.stringAtPoint(s[i].asString, Point(cw, 360));
			cw= cw+(f.size*0.667);
		};
		cw= 0;
		s.size.do{|i|
			var c= sin(angle+(pi/2+(pi/4))+(i/pi));
			Pen.font= f.size_(c+1*18+6);
			Pen.stringAtPoint(s[i].asString, Point(cw, 420));
			cw= cw+(f.size*0.667);
		};
	};
	w.front;
)
(
	var w= Window("processing - page_366", Rect(100, 100, 200*11, 200*10), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var angle, speed, circlePhase;
	u.background= Color.white;
	u.drawFunc= {
		angle= 0;
		speed= 0.2;
		Pen.fillColor= Color.grey(0, 60/255);
		10.do{|y|
			11.do{|i|
				Pen.use{
					Pen.translate(i*200, y*200);
					circlePhase.value(0);
					circlePhase.value(pi/4);
					circlePhase.value(pi/2);
					angle= angle+speed;
				};
			};
		};
	};
	circlePhase= {|phase|
		var diameter= 65+(sin(angle+phase)*130)/2;
		Pen.fillOval(Rect.aboutPoint(Point(100, 100), diameter, diameter));
	};
	w.front;
)
(
	var w= Window("processing - page_394", Rect(100, 100, 513, 666), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var numRings= 50, currentRing= 0;
	var ring= {(
		\x: 0,
		\y: 0,
		\diameter: 1,
		\on: false,
		\start: {|envir, xpos, ypos|
			envir.x= xpos;
			envir.y= ypos;
			envir.on= true;
			envir.diameter= 1;
		},
		\agrow: {|envir|
			if(envir.on, {
				envir.diameter= envir.diameter+0.5;
				if(envir.diameter>(u.bounds.height*2), {
					envir.on= false;
				});
			});
		},
		\display: {|envir|
			if(envir.on, {
				Pen.addOval(Rect.aboutPoint(Point(envir.x, envir.y), envir.diameter, envir.diameter));
			});
		}
	)};
	var rings= ring.dup(numRings);
	u.background= Color.white;
	u.animate= true;
	u.mouseDownAction= {|v, x, y|
		rings[currentRing].start(x, y);
		currentRing= currentRing+1;
		if(currentRing>=numRings, {
			currentRing= 0;
		});
	};
	u.drawFunc= {
		Pen.strokeColor= Color.grey(102/255, 153/255);
		rings.do{|ring|
			ring.agrow;
			ring.display;
		};
		Pen.stroke;
	};
	w.front;
)
(
	var w= Window("processing - page_412", Rect(100, 100, 400, 666), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var s= 0, mouseX= 0, mouseY= 0, pmouseX= 0, pmouseY= 0;
	//u.background= Color.grey(0.8);
	w.acceptsMouseOver= true;
	u.clearOnRefresh= false;
	u.mouseOverAction= {|v, x, y|
		mouseX= x;
		mouseY= y;
		s= ([x, y]-[pmouseX, pmouseY]).sum{|z| z*z}.sqrt.abs;
		u.refresh;
		pmouseX= x;
		pmouseY= y;
	};
	u.drawFunc= {
		Pen.fillColor= Color.grey(0, 102/255);
		Pen.fillOval(Rect.aboutPoint(Point(mouseX, mouseY), s/2, s/2));
		Pen.strokeColor= Color.white;
		Pen.line(Point(mouseX, mouseY), Point(mouseX+1, mouseY+1));
		Pen.stroke;
	};
	w.front;
)
(
	var w= Window("processing - page_420", Rect(100, 100, 513, 666), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var mouseX= 0, mouseY= 0, clear= true;
	u.clearOnRefresh= false;
	u.mouseMoveAction= {|v, x, y|
		clear= false;
		mouseX= x;
		mouseY= y;
		u.refresh;
		"hi".postln;
	};
	u.mouseDownAction= {
		clear= true;
		u.refresh;
	};
	u.drawFunc= {
		Pen.smoothing_(false);
		if(clear, {
			Pen.fillColor= Color.white;
			Pen.fillRect(u.bounds);
		});
		Pen.strokeColor= Color.black;
		Pen.line(Point(mouseX, mouseY), Point(mouseX, mouseY+0.5));
		Pen.stroke;
	};
	w.front;
)
(
	var w= Window("processing - page_476", Rect(100, 100, 513, 666), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var arrowParticle= {(
		\x: 0,
		\y: 0,
		\vx: 0,
		\vy: 0,
		\radius: 1,
		\gravity: 0.1,
		\angle: 0,
		\shaftLength: 20,
		\setup: {|envir, xpos, ypos, velx, vely, r|
			envir.x= xpos;
			envir.y= ypos;
			envir.vx= velx;
			envir.vy= vely;
			envir.radius= r;
		},
		\aupdate: {|envir|
			envir.vy= envir.vy+envir.gravity;
			envir.y= envir.y+envir.vy;
			envir.x= envir.x+envir.vx;
			envir.angle= atan2(envir.vy, envir.vx);
		},
		\display: {|envir|
			Pen.use{
				Pen.translate(envir.x, envir.y);
				Pen.rotate(envir.angle);
				Pen.scale(envir.shaftLength, envir.shaftLength);
				Pen.width= 1/envir.shaftLength;
				Pen.line(Point(0, 0), Point(0.25, 0));
				Pen.line(Point(0.25, 0), Point(0.174, -0.075));
				Pen.line(Point(0.25, 0), Point(0.174, 0.075));
				Pen.stroke;
			};
		}
	)};
	var num= 900;
	var radius= 1.2;
	var p= {
		var velX= -1.0.rrand(10);
		var velY= -20.0.rrand(-6);
		arrowParticle.value.setup(u.bounds.width/4, u.bounds.height, velX, velY, 0.4);
	}.dup(num);
	u.background= Color.white;
	u.animate= true;
	u.drawFunc= {
		p.do{|a|
			a.aupdate;
			a.display;
		};
	};
	w.front;
)
(
	var w= Window("processing - page_xx", Rect(100, 100, 250, 925), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var x, y, r= 350, n= 8, rs= 12;
	var drawCircle;
	u.background= Color.white;
	thisThread.randSeed= rs;
	y= u.bounds.height*0.33;
	x= u.bounds.width/2;
	u.drawFunc= {
		drawCircle.value(x, y, r, n);
	};
	drawCircle= {|x, y, radius, num|
		var value= 126*num/6;
		var branches;
		Pen.fillColor= Color.grey(value/255, 153/255);
		Pen.fillOval(Rect.aboutPoint(Point(x, y), radius, radius));
		if(num>1, {
			num= num-1;
			branches= 2.rrand(6);
			branches.do{|i|
				var a= 2pi.rand;
				var newx= x+(cos(a)*6*num);
				var newy= y+(sin(a)*6*num);
				drawCircle.value(newx, newy+26, radius/2, num);
			};
		});
	};
	w.front;
)





Processing-Book-001 / Synthesis


(
	var w= Window("processing - _01_waves", Rect(100, 100, 400, 400), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var shapeTriangleStrip, points;
	u.background= Color.white;
	u.drawFunc= {
		var angle= 0, magnitude= 24;
		points= [];
		0.forBy(u.bounds.width, 8, {|x|
			var y, y2;
			y= 50+(sin(angle)*magnitude);
			angle= angle+(pi/48);
			y2= 70+(sin(angle+(pi/6))*magnitude);
			points= points.add(Point(x, y));
			points= points.add(Point(x, y2));
		});
		shapeTriangleStrip.value(points);
		Pen.stroke;
	};
	shapeTriangleStrip= {|points|
		var max= points.size-1;
		points.do{|p, i|
			if(i<max, {Pen.line(p, points[i+1])});
			if(i>1, {Pen.line(p, points[i-2])});
		};
	};
	w.front;
)
(
	var w= Window("processing - _02_waves", Rect(100, 100, 400, 400), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var shapeTriangleStrip, points;
	u.background= Color.white;
	u.drawFunc= {
		var angle= 0, magnitude= 18;
		magnitude.neg.forBy(u.bounds.height+magnitude, 24, {|i|
			points= [];
			0.forBy(u.bounds.width, 8, {|x|
				var y, y2;
				y= i+(sin(angle)*magnitude);
				angle= angle+(pi/24);
				y2= i+10+(sin(angle+(pi/12))*magnitude);
				points= points.add(Point(x, y));
				points= points.add(Point(x, y2));
			});
			shapeTriangleStrip.value(points);
			Pen.stroke;
		});
		
	};
	shapeTriangleStrip= {|points|
		var max= points.size-1;
		points.do{|p, i|
			if(i<max, {Pen.line(p, points[i+1])});
			if(i>1, {Pen.line(p, points[i-2])});
		};
	};
	w.front;
)
//almost.  some problems with fill in this example
(
	var w= Window("processing - _03_waves", Rect(100, 100, 1200, 280), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var shapeTriangleStripFill, points;
	u.background= Color.white;
	u.drawFunc= {
		var angle= 0, angle2= 0, magnitude= 8;
		magnitude.neg.forBy(u.bounds.height+magnitude, 18, {|i|
			angle2= angle;
			
			points= [];
			Pen.fillColor= Color.black;
			0.forBy(u.bounds.width, 8, {|x|
				var y, y2;
				y= i+(sin(angle)*magnitude);
				angle= angle+(pi/24);
				y2= i+4+(sin(angle+(pi/12))*magnitude);
				points= points.add(Point(x, y));
				points= points.add(Point(x, y2));
			});
			shapeTriangleStripFill.value(points);
			Pen.fill;
			
			points= [];
			Pen.fillColor= Color.grey(204/255);
			0.forBy(u.bounds.width, 8, {|x|
				var y, y2;
				y= i+4+(sin(angle2+(pi/12))*magnitude);
				angle2= angle2+(pi/24);
				y2= i+8+(sin(angle2+(pi/12))*magnitude);
				points= points.add(Point(x, y));
				points= points.add(Point(x, y2));
			});
			shapeTriangleStripFill.value(points);
			Pen.fill;
		});
	};
	shapeTriangleStripFill= {|points|
		var max= points.size-1;
		points.do{|p, i|
			Pen.moveTo(p);
			if(i<max, {Pen.lineTo(points[i+1])});
			if(i>1, {Pen.lineTo(points[i-2])});
		};
	};
	w.front;
)
//almost.  some problems with fill in this example
(
	var w= Window("processing - _04_waves", Rect(100, 100, 1200, 280), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var shapeTriangleStripFill, points;
	u.background= Color.white;
	u.drawFunc= {
		var angle= 0, magnitude= 16;
		magnitude.neg.forBy(u.bounds.width+magnitude, 16, {|i|
			Pen.fillColor= Color.grey(i/u.bounds.width);
			points= [];
			0.forBy(u.bounds.height, 6, {|y|
				var x, x2;
				x= i+(sin(angle)*magnitude);
				angle= angle+(pi/22);
				x2= i+8+(sin(angle+(pi/22))*magnitude);
				points= points.add(Point(x, y));
				points= points.add(Point(x2, y));
			});
			shapeTriangleStripFill.value(points);
			Pen.fill;
		});
	};
	shapeTriangleStripFill= {|points|
		var max= points.size-1;
		points.do{|p, i|
			Pen.moveTo(p);
			if(i<max, {Pen.lineTo(points[i+1])});
			if(i>1, {Pen.lineTo(points[i-2])});
		};
	};
	w.front;
)
//almost.  some problems with fill in this example
(
	var w= Window("processing - _05_waves", Rect(100, 100, 1200, 280), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var shapeTriangleStripFill, points;
	u.background= Color.white;
	u.drawFunc= {
		var angle= pi, angle2= pi, magnitude= 3;
		magnitude.neg.forBy(u.bounds.height+magnitude, 12, {|i|
			angle2= angle;
			
			Pen.fillColor= Color.black;
			points= [];
			0.forBy(u.bounds.width, 8, {|x|
				var y, y2;
				y= i+(sin(angle)*magnitude);
				angle= angle+(pi/24);
				y2= i+4+(sin(angle+(pi/12))*magnitude);
				points= points.add(Point(x, y));
				points= points.add(Point(x, y2));
			});
			shapeTriangleStripFill.value(points);
			Pen.fill;
		});
	};
	shapeTriangleStripFill= {|points|
		var max= points.size-1;
		points.do{|p, i|
			Pen.moveTo(p);
			if(i<max, {Pen.lineTo(points[i+1])});
			if(i>1, {Pen.lineTo(points[i-2])});
		};
	};
	w.front;
)
(
	var w= Window("processing - _01_grids", Rect(100, 100, 600, 600), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var sqNum= 12, sqSize= u.bounds.width.div(sqNum), halfSize= sqSize.div(2);
	u.background= Color.white;
	u.drawFunc= {
		Pen.smoothing_(false);
		halfSize.forBy(u.bounds.width, sqSize, {|y|
			halfSize.forBy(u.bounds.height, sqSize, {|x|
				Pen.strokeRect(Rect(x-halfSize+2, y-halfSize+2, sqSize-4, sqSize-4));
			});
		});
	};
	w.front;
)
(
	var w= Window("processing - _02_grids", Rect(100, 100, 600, 600), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var numSquares= 12, gap= 4, sqSize= (u.bounds.width-(gap*2)).div(numSquares),
		halfSize= sqSize.div(2), offset= halfSize+gap, ydiv= sqSize.div(12);
	u.background= Color.white;
	u.drawFunc= {
		Pen.smoothing_(false);
		numSquares.do{|y|
			numSquares.do{|x|
				var xp= offset+(x*sqSize);
				var yp= offset+(y*sqSize);
				0.forBy(sqSize-(gap*2), ydiv, {|i|
					var yy= i*halfSize+gap+i;
					Pen.line(Point(xp-halfSize+gap, yp-halfSize+gap+i), Point(xp+halfSize-gap, yp-halfSize+gap+i));
				});
			};
		};
		Pen.stroke;
	};
	w.front;
)
(
	var w= Window("processing - _03_grids", Rect(100, 100, 600, 600), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var numSquares= 6, gap= 8, sqSize= (u.bounds.width-(gap*2)).div(numSquares);
	u.background= Color.black;
	u.drawFunc= {
		Pen.smoothing_(false);
		Pen.strokeColor= Color.grey(1, 204/255);
		numSquares.do{|y|
			numSquares.do{|x|
				var sqArea= 10.rrand(100);
				var halfArea= sqArea.div(2);
				var ydiv= sqArea.div(12);
				var offset= sqSize/2+gap;
				var xp= offset+(x*sqSize);
				var yp= offset+(y*sqSize);
				0.forBy(sqArea-(gap*2), ydiv, {|i|
					var yy= i*halfArea+gap+i;
					Pen.line(Point(xp-halfArea+gap, yp-halfArea+gap+i), Point(xp+halfArea-gap, yp-halfArea+gap+i));
				});
			};
		};
		Pen.stroke;
	};
	w.front;
)
(
	var w= Window("processing - _04_grids", Rect(100, 100, 600, 600), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var numSquares= 6, gap= 8, xUnit= u.bounds.width-(gap*2)/numSquares, yUnit= u.bounds.height-(gap*2)/numSquares;
	u.background= Color.black;
	u.drawFunc= {
		Pen.smoothing_(false);
		Pen.strokeColor= Color.grey(1, 204/255);
		numSquares.do{|y|
			numSquares.do{|x|
				var squareWidth= 10.rrand(300);
				var squareHeight= squareWidth.round;
				var ydiv= squareHeight.div(12);
				var xoff= xUnit/2+gap;
				var yoff= yUnit/2+gap;
				var xp= xoff+(x*xUnit);
				var yp= yoff+(y*yUnit);
				0.forBy(squareHeight-(gap*2), ydiv, {|i|
					var yy= i*squareHeight/2+gap+i;
					Pen.line(Point(xp-(squareWidth/2)+gap, yp-(squareHeight/2)+gap+i), Point(xp+(squareWidth/2)-gap, yp-(squareHeight/2)+gap+i));
				});
			};
		};
		Pen.stroke;
	};
	w.front;
)
(
	var w= Window("processing - _05_grids", Rect(100, 100, 600, 600), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var numSquares= 12, gap= 4, xUnit= u.bounds.width-(gap*2)/numSquares, yUnit= u.bounds.height-(gap*2)/numSquares, squareWidth= xUnit, squareHeight= yUnit;
	u.background= Color.black;
	u.drawFunc= {
		Pen.smoothing_(false);
		Pen.strokeColor= Color.grey(1, 204/255);
		numSquares.do{|y|
			numSquares.do{|x|
				var ydiv= squareHeight/12;
				var xoff= xUnit/2+gap;
				var yoff= yUnit/2+gap;
				var xp= xoff+(x*xUnit);
				var yp= yoff+(y*yUnit);
				0.forBy(squareHeight-(gap*2), ydiv, {|i|
					var yy= i*squareHeight/2+gap+i;
					Pen.line(Point(xp-(squareWidth/2)+gap, yp-(squareHeight/2)+gap+i), Point(xp+(squareWidth/2)-gap, yp-(squareHeight/2)+gap+i));
				});
				ydiv= 1.0.rrand(squareHeight/4);
				0.forBy(squareHeight-(gap*2), ydiv, {|i|
					var yy= i*squareHeight/2+gap+i;
					Pen.line(Point(xp-(squareWidth/2)+gap, yp-(squareHeight/2)+gap+i), Point(xp+(squareWidth/2)-gap, yp-(squareHeight/2)+gap+i));
				});
			};
		};
		Pen.stroke;
	};
	w.front;
)
(
	var w= Window("processing - _04_mandelbrot", Rect(100, 100, 600, 600), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var co;	// color
	var accuracy= 200; // Lower numbers are less accurate (lower resolution)
	var zoom= 200;	// Try these: 200 250 350 500 700 950 1250 1600 2000 2450 2950 3500
	var dx= u.bounds.width/2;
	var dy= u.bounds.height/2;
	var xscale= u.bounds.width/zoom;
	var yscale= u.bounds.height/zoom;
	var xOff= dx/u.bounds.width*xscale/2;
	var yOff= -1+(dy/u.bounds.height*yscale/2);
	u.background= Color.black;
	u.drawFunc= {
		Pen.smoothing_(false);
		u.bounds.width.do{|i|
			u.bounds.height.do{|j|
				var k= 0, zz1, zz2;
				var re= i/zoom-xOff-(dx/zoom);
				var im= j/zoom-yOff-(dy/zoom);
				// Complex number z, c
				var z1= 0.0;
				var z2= 0.0;
				co= 0;
				
				while({k<accuracy}, {
					zz1= z1.squared-z2.squared+re;
					zz2= 2*z1*z2+im;
					z1= zz1;
					z2= zz2;
					// Check if the modulus of complex num is within limit
					if((z1.squared+z2.squared).sqrt>2, {
						co= 1-(k/accuracy);
						k= accuracy;
					}, {
						k= k+1;
					});
				});
				
				Pen.strokeColor= Color(co, co, co);
				Pen.strokeRect(Rect.aboutPoint(Point(i, j), 1, 1));
			};
		};
	};
	w.front;
)
(
	var w= Window("processing - _05_tennis", Rect(100, 100, 640, 480), false);
	var u= UserView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
	var ballX= 0, ballY= u.bounds.height/2, ballDir= 1, ballSize= 10, ballAngle= 0;
	var paddleWidth= 20, paddleHeight= 40, wallGap= 50, netSegment= u.bounds.height/32;
	var mouseX= 0, mouseY= 0, pmouseX= 0, pmouseY= 0;
	w.acceptsMouseOver= true;
	u.mouseOverAction= {|v, x, y|
		pmouseX= (x-mouseX).abs;
		pmouseY= (y-mouseY).abs;
		mouseX= x;
		mouseY= y;
	};
	u.background= Color.black;
	u.animate= true;
	u.drawFunc= {
		var paddleY, py;
		Pen.strokeColor= Color.white;
		0.forBy(u.bounds.height, netSegment, {|i|
			Pen.line(Point(u.bounds.width/2, i), Point(u.bounds.width/2, i+(netSegment/3)));
		});
		Pen.stroke;
		ballX= ballX+(ballDir*2);
		ballY= ballY+ballAngle;
		if(ballX>(u.bounds.width+(ballSize*2)), {
			ballX= ballSize.neg;
			ballY= 0.rrand(u.bounds.height-ballSize);
			ballAngle= 0;
			(""++ballX+":"+ballY+":"+ballAngle).postln;
		});
		if(ballX<(ballSize.neg*2), {
			ballX= u.bounds.width;
			ballY= 0.rrand(u.bounds.height-ballSize);
			ballAngle= 0;
			(""++ballX+":"+ballY+":"+ballAngle).postln;
		});
		paddleY= mouseY.clip(0, u.bounds.height-paddleHeight);
		py= u.bounds.width-wallGap-ballSize;
		if(ballX>=py and:{ballY+ballSize>=paddleY and:{ballY<=(paddleY+paddleHeight)}}, {
			ballDir= ballDir* -1;
			if(mouseY!=pmouseY, {
				ballAngle= (mouseY-pmouseY)/2;
				ballAngle= ballAngle.clip(-5, 5);
			});
		});
		if(ballX<0, {
			ballDir= ballDir* -1;
		});
		if(ballY>(u.bounds.height-ballSize) or:{ballY<0}, {
			ballAngle= ballAngle* -1;
		});
		Pen.fillColor= Color.white;
		Pen.fillRect(Rect(ballX, ballY, ballSize, ballSize));
		Pen.fillColor= Color.grey(153/255);
		Pen.fillRect(Rect(u.bounds.width-wallGap, paddleY, paddleWidth, paddleHeight));
		Pen.fillRect(Rect(wallGap, u.bounds.height-paddleY-paddleHeight, paddleWidth, paddleHeight));
	};
	w.front;
)



Link to this Page