说一说分形。
大概分形是新手阶段最能简单直接的体现数学之美、编程之美的一个话题了,据说,它描述了大自然。
百度一下分形图片,各种不可思议而又美轮美奂的图就会闪现出来,数目庞大,种类繁多,结构复杂。
看起来确实很复杂。但是实际却很简单。一个数学公式的简单循环递归,就可以勾勒出一幅美丽的画卷。
分形的著名实例:用“镂空”办法制成的康托三分集虚线、谢尔宾斯基三角形垫子(Waclaw Sierpinski,1882-1969,波兰数学家)及门格尔奶酪或称门格尔海绵(Menger,1902-1985,为著名经济学家门格尔之子),它们的非整数维数是渐增的,分别为0.63、1.58、2.72,而它们长度、面积、体积令人吃惊的皆为0;另一个用“凸起”办法制作的科赫雪花曲线(H.von Koch,1870-1924,瑞典数学家),其维数是1.26,它的长度则是无限的,可它围住的面积却有限!(来自百度百科~)
上边的姑且不谈,我们从基础开始,这也是我对自己的总结,和温习。(今天都是全码,可以直接运行,大家执行着玩)
1、先做一个手镯。
import java.awt.Graphics;
import javax.swing.JFrame;
public class shape extends JFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub
shape a=new shape();
a.draw();
}
public void draw(){
this.setTitle("手镯");
this.setSize(1200, 700);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void paint(Graphics g){
//调用父类的重绘方法
super.paint(g);
double x=0,y=0,x0=0;
double a=1.40,b=1.56,c=1.40,d=-6.56;
for(int i = 0;i<70000;i++){
x0=x;
x=d*Math.sin(a*x)-Math.sin(b*y);
y=c*Math.cos(a*x0)+Math.cos(b*y);
int ix=(int)(x*30);
int iy=(int)(y*30);
g.drawLine(ix+300,iy+300,ix+300,iy+300);
}
}
}
用x,y记录坐标,画点,一群点按照数学函数x=d*Math.sin(a*x)-Math.sin(b*y);y=c*Math.cos(a*x0)+Math.cos(b*y);练成线(循环),然后就画了出来。
2、现在是一条变色的线
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class shape3 extends JFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub
shape3 a=new shape3();
a.draw();
}
public void draw(){
this.setTitle("简单画板");
this.setSize(1200, 700);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void paint(Graphics g){
//调用父类的重绘方法
super.paint(g);
for(int i=0;i<255;i++){
Color color=new Color(i%255,i%255,255);
g.setColor(color);
g.drawLine((int)i*3+200,(int)300,(int)i*3+300,(int)300);
}
}
}
3、稍微升级一下
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class shape2 extends JFrame {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
shape2 a=new shape2();
a.draw();
}
public void draw(){
this.setTitle("简单画板");
this.setSize(1200, 700);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void paint(Graphics g){
//调用父类的重绘方法
super.paint(g);
for(int i=0;i<=700;i++){
for(int j=0;j<=200;j++){
Color color=new Color(j%255,j%255,255);
g.setColor(color);
g.drawLine((int)i+200,(int)j+300,(int)i+300,(int)j+300);
//长方形渐变
}
}
}
}
4、一个变色的通道,不过没有设置停止和线程,所以要等很久才能关掉,或者强关
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class shape1 extends JFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub
shape1 a=new shape1();
a.draw();
}
public void draw(){
this.setTitle("简单画板");
this.setSize(1200, 700);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void paint(Graphics g){
//调用父类的重绘方法
super.paint(g);
double x=0,y=0,x0=0;
double a=-2,b=-2,c=-1.2,d=2;
for(int aa = 0;aa<55;aa++){
for(int bb = 20;bb<255;bb++){
for(int cc = 1;cc<55;cc++){
x0=x;
x=Math.sin(a*y)-Math.cos(b*x);
y=Math.sin(c*x0)-Math.cos(d*y);
int ix=(int)(x*100);
int iy=(int)(y*100);
Color color=new Color(aa,bb,cc);
g.setColor(color);
g.drawLine(ix+300,iy+300,ix+300,iy+300);
}
}
}
for(int aa = 178;aa<255;aa++){
for(int bb = 40;bb<90;bb++){
for(int cc = 89;cc<255;cc++){
x0=x;
x=Math.sin(a*y)-Math.cos(b*x);
y=Math.sin(c*x0)-Math.cos(d*y);
int ix=(int)(x*100);
int iy=(int)(y*100);
Color color=new Color(aa,bb,cc);
g.setColor(color);
g.drawLine(ix+300,iy+300,ix+300,iy+300);
}
}
}
}
}
5、孔雀开屏
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class beatuful extends JFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub
beatuful a=new beatuful();
a.draw();
}
public void draw(){
this.setTitle("简单画板");
this.setSize(1200, 700);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void paint(Graphics g){
//调用父类的重绘方法
super.paint(g);
double x=0,y=0;
double e=2.718,r;
for(int i=0;i<=200000;i++){
r=(Math.pow(e , Math.sin(i))+2*Math.cos(i)+Math.pow(Math.sin(49*i) , 4));
x=r*Math.cos(i);
y=r*Math.sin(i);
Color color=new Color(i%255,i%255,255);
g.setColor(color);
g.drawLine((int)(x*100)+300,(int)(y*100)+300,(int)(x*100)+300,(int)(y*100)+300);
}
}
}
}