java - My Barnsley Fern is Too Skinny -


i'm new java programming, , i've decided pick topic of fractals school essay. however, first step of writing essay requires me recreate barnsley fern using java code. when tried this, barnsley fern looked skinny compared other ones i've seen on web. checked wikipedia make sure numbers , probabilities of affine transformations correct, same, skinny fern produced. can me? dove realm of bufferedimage, hearing that better way of rendering pixels, i'm still unsure of how works. graphics style of painting unreliable or something? code:

import java.awt.graphics; import java.awt.color; import java.awt.canvas;  public class fern extends canvas{      private static final long serialversionuid = 1l;     public static final int iteration = 100000;      public  fern() {         setbackground(color.white);     }      public void paint(graphics window)     {         double x = .5;         double y = 0;         window.setcolor(color.green);         window.drawrect(200,600,1,1);         color color;         for(int = 0; i<iteration; i++){             double chance = (math.random());             if(chance<.85){                 x=.85*x+.04*y; //x=.85*x+.04*y;                 y=-.04*x+.85*y+1.6; //y=-.04*x+.85*y+1.6;                 color = new color(color.green.getrgb());             }             else if(chance<.86){                 x=0; //x=0;                 y=.16*y; //y=.16*y;                 color = new color(color.blue.getrgb());             }             else if(chance<.93){                 x=.2*x-.26*y; //x=.2*x-.26*y;                 y=.23*x+.22*y+1.6; //y=.23*x+.22*y+1.6;                 color = new color(color.red.getrgb());             }             else{                 x=-.15*x+.28*y; //x=-.15*x+.28*y;                 y=.26*x+.24*y+.44; //y=.26*x+.24*y+.44;                 color = new color(color.orange.getrgb());             }             window.setcolor(color);             window.fillrect((int)(66*x)+200,(int)(66*-y)+675,1,1);         }     } } 

and runner class provided apluscompsci.com few tweaks.

//© a+ computer science  -  www.apluscompsci.com  import javax.swing.jframe;  public class graphicsrunner extends jframe {     private static final long serialversionuid = 1l;     private static final int width = 500;     private static final int height= 800;      public graphicsrunner()     {         super("barnsley fern");         setsize(width,height);            getcontentpane().add(new fern());         setdefaultcloseoperation( jframe.exit_on_close );         setvisible(true);     }      public static void main( string args[] )     {         graphicsrunner run = new graphicsrunner();     } } 

sorry not provide picture of misshapen fern. thank in advance!

the following lines contain common error :

  x=.85*x+.04*y; //x=.85*x+.04*y;   y=-.04*x+.85*y+1.6; //y=-.04*x+.85*y+1.6; 

because change value x on first line, second line (y) gives wrong result.

you should write :

  int x2=.85*x+.04*y; //x=.85*x+.04*y;   y=-.04*x+.85*y+1.6; //y=-.04*x+.85*y+1.6;   x = x2; 

using temporary variable allows have correct result both x , y.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -