importturtleimportrandompen=turtle.Turtle()pen.speed(20)pen.color('green')pen.penup()x=0y=0forninrange(100000):pen.goto(85*x,57*y-275)# 57 is to scale the fern and -275 is to start the drawing from the bottom.pen.pendown()pen.dot()pen.penup()r=random.random()# to get probabilityr=r*100xn=xyn=yifr<1:# elif ladder based on the probabilityx=0y=0.16*ynelifr<86:x=0.85*xn+0.04*yny=-0.04*xn+0.85*yn+1.6elifr<93:x=0.20*xn-0.26*yny=0.23*xn+0.22*yn+1.6else:x=-0.15*xn+0.28*yny=0.26*xn+0.24*yn+0.44
R
# Barnsley's Fern# create function of the probability and the current pointfractal_fern2<-function(x,p){if (p<=0.01){m<-matrix(c(0,0,0,.16),2,2)f<-c(0,0)}elseif (p<=0.86){m<-matrix(c(.85,-.04,.04,.85),2,2)f<-c(0,1.6)}elseif (p<=0.93){m<-matrix(c(.2,.23,-.26,.22),2,2)f<-c(0,1.6)}else{m<-matrix(c(-.15,.26,.28,.24),2,2)f<-c(0,.44)}m%*%x+f}# how many reps determines how detailed the fern will bereps<-10000# create a vector with probability values, and a matrix to store coordinatesp<-runif(reps)# initialise a point at the origincoords<-c(0,0)# compute Fractal Coordinatesm<-Reduce(fractal_fern2,p,accumulate=T,init=coords)m<-t(do.call(cbind,m))# Create plotplot(m,type="p",cex=0.1,col="darkgreen",xlim=c(-3,3),ylim=c(0,10),xlab=NA,ylab=NA,axes=FALSE)
Processing
/* Barnsley Fern for Processing 3.4*/// declaring variables x and yfloatx,y;// creating canvasvoidsetup(){size(600,600);background(255);}/* setting stroke, mapping canvas and then plotting the points */voiddrawPoint(){stroke(34,139,34);strokeWeight(1);floatpx=map(x,-2.1820,2.6558,0,width);floatpy=map(y,0,9.9983,height,0);point(px,py);}/* algorithm for calculating value of (n+1)th term of x and y based on the transformation matrices */voidnextPoint(){floatnextX,nextY;floatr=random(1);if(r<0.01){nextX=0;nextY=0.16*y;}elseif(r<0.86){nextX=0.85*x+0.04*y;nextY=-0.04*x+0.85*y+1.6;}elseif(r<0.93){nextX=0.20*x-0.26*y;nextY=0.23*x+0.22*y+1.6;}else{nextX=-0.15*x+0.28*y;nextY=0.26*x+0.24*y+0.44;}x=nextX;y=nextY;}/* iterate the plotting and calculation functions over a loop */voiddraw(){for(inti=0;i<100;i++){drawPoint();nextPoint();}}
P5.JS
letx=0;lety=0;functionsetup(){createCanvas(600,600);background(0);}//range −2.1820 < x < 2.6558 and 0 ≤ y < 9.9983.functiondrawPoint(){stroke(255);strokeWeight(1);letpx=map(x,-2.1820,2.6558,0,width);letpy=map(y,0,9.9983,height,0);point(px,py);}functionnextPoint(){letnextX;letnextY;letr=random(1);if(r<0.01){//1nextX=0;nextY=0.16*y;}elseif(r<0.86){//2nextX=0.85*x+0.04*y;nextY=-0.04*x+0.85*y+1.60;}elseif(r<0.93){//3nextX=0.20*x+-0.26*y;nextY=0.23*x+0.22*y+1.60;}else{//4nextX=-0.15*x+0.28*y;nextY=0.26*x+0.24*y+0.44;}x=nextX;y=nextY;}functiondraw(){for(leti=0;i<1000;i++){drawPoint();nextPoint();}}
JavaScript (HTML5)
<canvasid="canvas"height="700"width="700"></canvas><script>letcanvas;letcanvasContext;letmove;letfps=250;letpointColor="green";letbackgroundColor="black";letpointRadius=1;letx=0;lety=0;window.onload=function(){canvas=document.getElementById("canvas");canvasContext=canvas.getContext('2d');canvasContext.fillStyle=backgroundColor;canvasContext.fillRect(0,0,canvas.width,canvas.height);move=setInterval(doIt,1000/fps);};letdoIt=function(){for(leti=0;i<20;i++)moveAll()};functionmoveAll(){letnextX,nextY;letr=Math.random();if(r<0.01){nextX=0;nextY=0.16*y;}elseif(r<0.86){nextX=0.85*x+0.04*y;nextY=-0.04*x+0.85*y+1.6;}elseif(r<0.93){nextX=0.20*x-0.26*y;nextY=0.23*x+0.22*y+1.6;}else{nextX=-0.15*x+0.28*y;nextY=0.26*x+0.24*y+0.44;}// Scaling and positioningletplotX=canvas.width*(x+3)/6;letplotY=canvas.height-canvas.height*((y+2)/14);drawFilledCircle(plotX,plotY,pointRadius,pointColor);x=nextX;y=nextY;}constdrawFilledCircle=(centerX,centerY,radius,color)=>{canvasContext.beginPath();canvasContext.fillStyle=color;canvasContext.arc(centerX,centerY,radius,0,2*Math.PI,true);canvasContext.fill();};</script>
SubBarnsley()DimiEndAsLongDimiAsLongDimxAsDoubleDimyAsDoubleDimnextXAsDoubleDimnextYAsDoubleDimsShapeArray()AsShapeDimdSizeAsDoubleDimsColorAsStringdSize=0.01' Size of the dotssColor="0,0,100"' RGB color of dots, value range 0 to 255iEnd=5000' Number of iterationsReDimsShapeArray(iEnd)' In Corel, each object drawn requires a variable name of its ownRandomize' Initialize the Rnd functionFori=0ToiEnd' Iterate ...SelectCaseRndCaseIs<0.01' f1 = Draw stemnextX=0nextY=0.16*yCase0.01To0.08' f3nextX=0.2*x-0.26*ynextY=0.23*x+0.22*y+1.6Case0.08To0.15' f4nextX=-0.15*x+0.28*ynextY=0.26*x+0.24*y+0.44CaseElse' f2nextX=0.85*x+0.04*ynextY=-0.04*x+0.85*y+1.6EndSelectx=nextXy=nextYSetsShapeArray(i)=ActiveLayer.CreateEllipse2(x+2.5,y+0.5,dSize)sShapeArray(i).Style.StringAssign"{""fill"":{""primaryColor"":""RGB255,USER,"&sColor&",100,00000000-0000-0000-0000-000000000000"",""secondaryColor"":""RGB255,USER,255,255,255,100,00000000-0000-0000-0000-000000000000"",""type"":""1"",""fillName"":null},""outline"":{""width"":""0"",""color"":""RGB255,USER,0,0,0,100,00000000-0000-0000-0000-000000000000""},""transparency"":{}}"DoEventsNextEndSub
Amola
addpackage("Forms.dll")set("x", 0)set("y", 0)set("width", 600)set("height", 600)methodsetup()createCanvas(width,height)rect(0,0,600,600,color(0,0,0))endmethoddrawPoint()set("curX", div(mult(width, add(x, 3)), 6))set("curY", sub(height, mult(height, div(add(y, 2), 14))))set("size", 1)//log(curX)//log(curY)rect(round(curX-size/2),round(curY-size/2),round(curX+size/2),round(curY+size/2),color(34,139,34))endmethodnextPoint()set("nextX", 0)set("nextY", 0)set("random", random(0, 100))if(random<1)set("nextX", 0)set("nextY", 0.16 * y)endelseif(random<86)set("nextX", 0.85 * x + 0.04 * y)set("nextY", -0.04 * x + 0.85 * y + 1.6)endelseif(random<93)set("nextX", 0.2 * x - 0.26 * y)set("nextY", 0.23 * x + 0.22 * y + 1.6)endelseset("nextX", -0.15 * x + 0.28 * y)set("nextY", 0.26 * x + 0.24 * y + 0.44)endendendset("x", nextX)set("y", nextY)endsetup()while(true)drawPoint()nextPoint()end