English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
[Introduction]
I have been reading 'The Deepest Simplicity' recently, and there is a chapter that introduces several methods of using noise to generate fractal patterns, which is very interesting, so I tried to simulate it with a computer and the effect is quite good (noise method is easier to implement in programming than traditional iterative method, and later I found that there are quite a few of these algorithms, you can find more by searching chaosgame).
[Noise generation method of Sierpinski triangle]
In these noise games, the generation rule of Sierpinski (谢尔宾斯基) triangle is the simplest:
1.Select three points on the plane, mark them as1,2,3, as the vertex of the large triangle.
2.Choose one of them as the 'current point' (for example, choose1number).
3.Generate1~3The random number, at the vertex expressed by the number and the midpoint of the 'current point', draw a new point, and take the new point as the 'current point'.
4.Repeat the steps3, so as to approach the pattern.
*.Note that it is better not to use a random number generation method with time as the seed.
[Simulated Program]
package com.geiv.chaos; import java.awt.event.KeyEvent; import com.thrblock.util.RandomSet; import geivcore.DefaultFactor; import geivcore.KeyFactor; import geivcore.KeyListener; import geivcore.R; import geivcore.UESI; import geivcore.enginedata.obj.Obj; public class Sierpinski extends DefaultFactor implements KeyListener{ UESI UES; Obj[] basePoint; Obj crtPoint; public Sierpinski(UESI UES,int times){ this.UES = UES; basePoint = new Obj[3]; //Create three reference points for (int i = 0;i < 3;i++){ basePoint[i] = UES.creatObj(UESI.BGIndex); basePoint[i].addGLPoint("70DBDB",0,0); basePoint[i].show(); } basePoint[0].setCentralX(400); //Set the position of three points basePoint[0].setCentralY(60); basePoint[1].setCentralX(60); basePoint[1].setCentralY(550); basePoint[2].setCentralX(740); basePoint[2].setCentralY(550); crtPoint = basePoint[0]; //Set point 0 as the current point this.setKeyListener(this); UES.pushKeyBoardIO(this); for (int i = 0;i < times;i++){ generateNew(); } } @Override public void doKeyBord(KeyFactor whom, int keyCode, Boolean ispressed) { //Mount callback if(ispressed){ if(keyCode == KeyEvent.VK_SPACE){ //Space corresponds to create a new point generateNew(); } else if(keyCode == KeyEvent.VK_A){ //A corresponds to create100 new points for (int i = 0;i < 100;i++){ generateNew(); } } else if(keyCode == KeyEvent.VK_B){ //B corresponds to create1000 new points for (int i = 0;i < 1000;i++){ generateNew(); } } } } public void generateNew(){ Obj flagPoint = basePoint[RandomSet.getRandomNum(0, 2); //Randomly select one of the reference points float nx = (flagPoint.getCentralX() + crtPoint.getCentralX())/2f; //calculate the midpoint float ny = (flagPoint.getCentralY() + crtPoint.getCentralY())/2f; Obj newPoint = UES.creatObj(UESI.BGIndex); //create a new point newPoint.addGLPoint("70DBDB",0,0); newPoint.setColor(RandomSet.getRandomColdColor()); newPoint.setCentralX(nx); //set coordinates newPoint.setCentralY(ny); newPoint.show(); crtPoint = newPoint; //set as the current point } public static void main(String[] args) { UESI ues = new R(); new Sierpinski(ues, 0); //the construction parameters can be set to the initial number of points. } }
[Simulation Results]
When the B key is pressed
[Noise Generation Method of Barnsley fern]
. Compared with the simple regularity of Sierpinski triangle, Barnsley fern (fractal fern) gives people a more complex impression. Due to its complexity, the chaos discipline often takes it out to prove the conclusion that "simple rules can also produce complex objects".
. Its generation rule is not very complex:
1. First, give the "current point" (0, 0), we use ox, oy to represent the abscissa and ordinate.
2. To calculate the next point (nx, ny), it is necessary to choose one of the following four iteration formulas according to a certain random rule:
1)with %1with a probability of choosing this iteration formula:
nx = 0;
ny = 0.16f*oy;
2)with %85with a probability of choosing this iteration formula:
nx = 0.85*ox+0.04*oy;
ny =-0.04*ox+0.85*oy+1.6;
3)with %7with a probability of choosing this iteration formula:
nx = 0.2*ox-0.26*oy;
ny = 0.23*ox+0.22*oy+1.6;
4)with %7with a probability of choosing this iteration formula:
nx =-0.15*ox+0.28*oy;
ny = 0.26*ox+0.24*oy+0.44;
3. Draw (nx, ny) and set it as the current point, repeat2, and you can approach the result infinitely.
↑The above formula is from Wiki:http://en.wikipedia.org/wiki/Barnsley_fern. When programming, I found a problem, Wiki did not specify the relationship between the absolute value of this coordinate and the screen size, nor did it specify the direction of the x, y axes. Drawing in my defined coordinate system always failed. Later, I searched for the formula according to the formula and found this surface:http://people.sc.fsu.edu/~jburkardt/cpp_src/fern_opengl/fern.cpp. This is a C++The OPENGL program under it uses the same formula as Wiki, that is, this set of formulas is based on the Opengl coordinate system, and after corresponding transformation, it was finally successful in drawing.
[Simulated Program]
package com.geiv.chaos; import geivcore.DefaultFactor; import geivcore.KeyFactor; import geivcore.KeyListener; import geivcore.R; import geivcore.UESI; import geivcore.enginedata.obj.Obj; import java.awt.Color; import java.awt.event.KeyEvent; import com.thrblock.util.RandomSet; public class Barnsleyfern extends DefaultFactor implements KeyListener{ UESI UES; Obj crtPoint; public Barnsleyfern(UESI UES,int times){ this.UES = UES; crtPoint = UES.creatObj(UESI.BGIndex); crtPoint.addGLPoint("70DBDB",0,0); crtPoint.show(); crtPoint.setCentralX(0); crtPoint.setCentralY(0); UES.setViewOffsetX(90); this.setKeyListener(this); UES.pushKeyBoardIO(this); for (int i = 0;i < times;i++){ generateNew(); } } @Override public void doKeyBord(KeyFactor whom, int keyCode, Boolean ispressed) { //Keyboard IO is the same as the example above if(ispressed){ if(keyCode == KeyEvent.VK_SPACE){ generateNew(); } else if(keyCode == KeyEvent.VK_A){ for (int i = 0;i < 100;i++){ generateNew(); } } else if(keyCode == KeyEvent.VK_B){ for (int i = 0;i < 1000;i++){ generateNew(); } } } } public void generateNew(){ float nx,ny; float ox = crtPoint.getCentralX()/150f,oy = (600 - crtPoint.getCentralY())/60f; //Here is OPENGL coordinate transformation, the corresponding inversion is used when setting the new point position. double code = 100.0 * RandomSet.getRandomFloatIn_1(); //Random floating point number 0~100 if(code >= 0&&code <= 1){ nx = 0; ny = 0.00f * ox + 0.16f * oy; } 1&& code <= 86){ nx = 0.85f*ox + 0.04f*oy; ny = -0.04f*ox + 0.85f*oy + 1.6f; } 86&& code <= 93){ nx = 0.2f*ox - 0.26f*oy; ny = 0.23f*ox + 0.22f*oy + 1.6f; } nx = -0.15f*ox + 0.28f*oy; ny = 0.26f*ox + 0.24f*oy + 0.44f; } Obj newPoint = UES.creatObj(UESI.BGIndex); newPoint.addGLPoint("70DBDB",0,0); newPoint.setColor(Color.GREEN); newPoint.setCentralX(nx*150f); //Cancel the transformation of the previous coordinates newPoint.setCentralY(600 - ny*60f); newPoint.show(); crtPoint = newPoint; //Set the new point as the current point. } public static void main(String[] args) { UESI ues = new R(); new Barnsleyfern(ues,0); } }
[Simulation Results]
Summary
That is all the content of this article about the Java Chaos Game noise game example code. I hope it will be helpful to everyone. Those who are interested can continue to read other related topics on this site, and welcome to leave comments if there are any shortcomings. Thank you for your support to this site!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.)