- UID
- 30376
- 帖子
- 527
- 精华
- 1
- 积分
- 6520
- 来自
- 湖南湘潭
|
class IFsystem{
constructor(canvas,funs,num,scale,color,seed,x,y,z){
this.canvas=canvas;
this.scale=scale;
this.funs=funs;
this.getseed=seed;
this.getcolor=color;
this.ox=x;
this.oy=y;
this.num=num;
this.point={x:0,y:0,z:z};
this.tp=0;
this.density=[];
this.init();
}
init(){
if(this.funs[0].length<7)
for(let i=0;i<this.funs.length;i++)this.funs[6]=1;
for(let i=0;i<this.funs.length;i++)this.tp+=this.funs[6];
this.density=[];
for(let x=0;x<this.canvas.width;x++){
this.density[x]=[];
for(let y=0;y<this.canvas.height;y++){
this.density[x][y]=[];
for(let i=0;i<4;i++)this.density[x][y]=0;
}
}
}
iterate(d){
let k=-1,p=random()*this.tp,funs=this.funs;
while(p>0)p-=funs[++k][6];
let x=funs[k][0]*d.x+funs[k][1]*d.y+funs[k][4],
y=funs[k][2]*d.x+funs[k][3]*d.y+funs[k][5],
z=this.getseed({x:x,y:y,z:d.z});
return{x,y,z}
}
getDensity(){
let w=this.canvas.width,h=this.canvas.height,dens=0;
for(let i=0;i<this.num;i++){
this.point=this.iterate(this.point);
let x=floor(this.ox+this.point.x*this.scale),
y=floor(h-this.oy-this.point.y*this.scale);
if(x>=0&&x<w&&y>=0&&y<h){
let col=this.getcolor(this.point.z);
for(let i=0;i<4;i++)this.density[x][y]+=col;
}
}
for(let x=0;x<w;x++)for(let y=0;y<h;y++)
dens=max(this.density[x][y][3],dens);
return log(dens)*log(dens);
}
drawIfs(){
let w=this.canvas.width,h=this.canvas.height,
ctx=this.canvas.getContext('2d'),
image=ctx.createImageData(w,h),
data=image.data,
maxDensity=this.getDensity();
for(let x=0;x<w;x++)for(let y=0;y<h;y++)
if(this.density[x][y][3]>0){
let idx=(x+y*w)*4;
for(let k=0;k<4;k++){
let light=log(this.density[x][y][k]);
light=(light*light)/maxDensity;
data[idx+k]=floor(255*light);
}
}
ctx.putImageData(image,0,0);
}
}
其中的 getSeed,getColor在窗体中实时编辑和运行。
这是除窗体外的所有代码
|
|