Code Flower Bed
形も色も微妙に違う花をコードで表現。
/**
* Copyright kenji_special ( http://wonderfl.net/user/kenji_special )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/th39
*/
package {
import flash.display.Sprite;
[SWF(backgroundColor="0", width="465", height="465")]
public class FlashTest extends Sprite {
public function FlashTest() {
var canvas:Sprite = new Sprite();
canvas.graphics.beginFill(0x000000);
canvas.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
this.addChild(canvas);
var flTest:flowerBase = new flowerBase(stage.stageWidth, stage.stageHeight );
//trace(flTest.width);
//trace(flTest.height);
flTest.x = (465- flTest.width)/2 ;
flTest.y = (465 -flTest.height)/2 ;
canvas.addChild(flTest);
}
}
}
import flash.display.Sprite;
internal class flowerBase extends Sprite
{
public function flowerBase(w:Number, h:Number)
{
var setX:Number;
var setY:Number;
var baseRad:Number = 20;
var noiseVal:Number = 0;
var varRad:Number ;
var totalRad:Number;
var haba:Number =40;
var choseiY:int = 0;
var firstX:Number;
var firstY:Number;
var nextX:Number;
var nextY:Number;
//graphics.lineStyle(1, 0x000000, );
var rMax:Number = Math.sqrt(w*w/4 + h * h/4);
//graphics.beginFill(0xffffff);
for(var lin:int = 0; lin < 6; lin++){
setY = baseRad * ( 2*lin + 1) + lin*haba;
for(var col:int = 0; col < 6; col++){
setX = baseRad * ( 2*col + 1) + haba * col;
varRad = 5 * customNoise(noiseVal);
totalRad = baseRad + varRad;
firstX = totalRad * Math.cos(0) + setX;
firstY = totalRad * Math.sin(0) + setY;
graphics.moveTo(firstX, firstY);
for(var i:int = 1; i < 360; i++){
noiseVal += 0.1;
varRad = 5 * customNoise(noiseVal) * 0.5* (Math.random() + 1);
totalRad = baseRad + varRad;
nextX = totalRad * Math.cos(i/180 * Math.PI) + setX;
nextY = totalRad * Math.sin(i/180 * Math.PI) + setY;
var kaku:Number = Math.atan2(nextY- h/2 +choseiY , nextX - w/2 ) /Math.PI * 180;
var kyori:Number = Math.sqrt((nextY - h/2 + choseiY)*(nextY - h/2 + choseiY) + (nextX - w/2 ) * (nextX - w/2));
var colString :String = HSVtoRGB(kaku, kyori/rMax + 0.3, 1);
this.graphics.lineStyle(1, int(colString));
graphics.lineTo(nextX, nextY);
}
graphics.lineTo(firstX, firstY);
for(var cenRad:int = 0; cenRad < 6; cenRad += 1){
var gosa:Number = 1;
graphics.moveTo(setX + cenRad*Math.cos(0), setY + cenRad * Math.sin(0));
for(var ii:int = 0; ii < 360; ii+= 6){
var nextCenX:Number = setX + cenRad * Math.cos(ii/180 * Math.PI) + gosa * Math.random();
var nextCenY:Number = setY + cenRad * Math.sin(ii/180 *Math.PI) + gosa * Math.random();
var kakuCen:Number = Math.atan2(nextCenY - h/2 + choseiY, nextCenX - w/2) /Math.PI * 180;
var kyoriCen:Number = Math.sqrt((nextCenY - h/2 + choseiY)*(nextCenY - h/2 + choseiY) + (nextCenX - w/2) * (nextCenX - w/2));
var colStringCen :String = HSVtoRGB(kakuCen, kyoriCen/rMax + 0.3, 1);
this.graphics.lineStyle(1, int(colStringCen), 0.3);
graphics.lineTo(nextCenX , nextCenY);
}
}
}
}
}
public function HSVtoRGB( h:Number, s:Number, v:Number ):String
{
var r:Number=0, g:Number=0, b:Number=0;
var i:Number, x:Number, y:Number, z:Number;
if(s<0) s=0; if(s>1) s=1; if(v<0) v=0; if(v>1) v=1;
h = h % 360; if (h < 0) h += 360; h /= 60;
i = h >> 0;
x = v * (1 - s); y = v * (1 - s * (h - i)); z = v * (1 - s * (1 - h + i));
switch(i){
case 0 : r=v; g=z; b=x; break;
case 1 : r=y; g=v; b=x; break;
case 2 : r=x; g=v; b=z; break;
case 3 : r=x; g=y; b=v; break;
case 4 : r=z; g=x; b=v; break;
case 5 : r=v; g=x; b=y; break;
}
var colString:String = "0x";
r *= 255;
g *= 255;
b *= 255;
if(r >= 16){
colString += r.toString("16");
}else{
colString += "0" + r.toString("16");
}
if(g >= 16){
colString += g.toString("16");
}else{
colString += "0" + g.toString("16");
}
if(b >= 16){
colString += b.toString("16");
}else{
colString += "0" + b.toString("16");
}
return colString;
}
public function customNoise(vari:Number):Number{
var radVari:Number = Math.pow(Math.cos(vari), 3);
return radVari;
}
}