AS3版 Graphing 2D Equation
元ネタ:http://processing.org/learning/basics/graphing2dequation.html
/*
元ネタ:http://processing.org/learning/basics/graphing2dequation.html
*/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Practice76 extends Sprite{
private var bmp:BitmapData;
private const W:Number = 16.0;
private const H:Number = 16.0;
private const DX:Number = W / 480.0;
private const DY:Number = H / 480.0;
private var isColor:Boolean = false;
public function Practice76() {
bmp = new BitmapData(480, 480, false, 0xffffff);
addChild(new Bitmap(bmp));
addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent):void { isColor = !isColor; } );
}
private function update(e:Event):void {
var n:Number = (mouseX * 10.0) / bmp.width;
var x:Number = -W / 2;
for (var i:int = 0; i < bmp.width; i++) {
var y:Number = -H / 2;
for (var j:int = 0; j < bmp.height; j++) {
var r:Number = Math.sqrt(x * x + y * y);
var t:Number = Math.atan2(y, x);
var v:Number = Math.sin(n * Math.cos(r) + 5.0 * t);
bmp.setPixel(i, j, getColor(v));
y += DY;
}
x += DX;
}
}
private function getColor(v:Number):uint {
if (isColor) {
return getColor2(360 * v, 0.5);
}else {
var c:int = ((v + 1.0) * 255.0 / 2.0);
return (c << 16) + (c << 8) + c;
}
}
private function getColor2(i:int, saturation:Number):uint {
var h:Number = i / 60;
var ii:Number = Math.floor(h);
var ff:Number = h - ii;
var p1:Number = (1.0 - saturation);
var p2:Number = (1.0 - saturation * ff);
var p3:Number = (1.0 - saturation * (1.0 - ff));
var rv:Number, gv:Number, bv:Number;
switch(ii) {
case 0:
rv = 1.0, gv = p3, bv = p1;
break;
case 1:
rv = p2, gv = 1.0, bv = p1;
break;
case 2:
rv = p1, gv = 1.0, bv = p3;
break;
case 3:
rv = p1, gv = p2, bv = 1.0;
break;
case 4:
rv = p3, gv = p1, bv = 1.0;
break;
default:
rv = 1.0, gv = p1, bv = p2;
}
var color:uint = (Math.max(0, Math.min(255, rv * 255)) << 16) + (Math.max(0, Math.min(255, gv * 255)) << 8) + Math.max(0, Math.min(255, bv * 255));
return color;
}
}
}