//Java version of Julia Set from Math Adventures with Python//Declare 2-element array, assign to variable cfloat[] c = new float[2];//set the range of x-valuesint xmin=-2;int xmax=2;//range of y-valuesint ymin = -2;int ymax = 2;//calculate the rangefloat rangex = xmax - xmin;float rangey = ymax - ymin;//Declare xscl and yxscl// We'll assign them values in setupfloat xscl = rangex/600;float yscl = rangey/600;void setup(){size(600,600);colorMode(HSB);noStroke();}void draw(){//Declare the components of cc[0] = 0.25*mouseX*xscl; //realc[1] = 0.25*mouseY*yscl; //imaginary//go over all x's and y's on the gridfor (int x=0;x<width;x++){for (int y=0;y<height;y++){float[] z ={xmin+x*xscl,ymin+y*yscl};//put z into the julia functionint col=julia(z,100);//if julia returns 100//that location never diverged//not in julia set --> blackif (col == 100){fill(0); //black}else{//calculate color from "col"int c1 = (255-2*col);fill(c1,255,255);}rect(x,y,1,1); //draw the tiny rectangle}}}int julia(float[] z,int num){//runs the process num times//and returns the diverge countint count=0;//define z1 as zfloat[] z1 = z;//iterate num timeswhile (count <= num){//check for divergenceif (magnitude(z1) > 2.0){//return the step it diverged onreturn count;}//iterate zz1=cAdd(cMult(z1,z1),c);count+=1;}//if z hasn't diverged by the endreturn num;}float[] cAdd(float[] a,float[] b){float[] output = {a[0]+b[0],a[1]+b[1]};return output;}float[] cMult(float[] u,float[] v){//Returns the product of two complex numbersfloat[] output = {u[0]*v[0]-u[1]*v[1],u[1]*v[0]+u[0]*v[1]};return output;}float magnitude(float[] z){float output = z[0]*z[0] + z[1]*z[1];if (output == 0){return 0;} else {return sqrt(output);}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。