本文实例为大家分享了Java分形绘制山脉模型的具体代码,供大家参考,具体内容如下
如何绘制一个山脉
构思设计
任意选取三个点,选取一个范围和一个比率,每一个都对这三个点取中点,中点的纵坐标加一个在范围内的随机值,当完成一次递归之后缩小这个范围即range*rate
连线的时候,将三角形的一个点和这个点的两条边生成的中点相连,最后将三条边的中点相连
所以应该有四个递归调用。
代码实现及注解
package Mountion; import java.awt.Graphics; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.JFrame; public class ShowUI { Listlist=new ArrayList<>(); int i=0; public void divide(int x1,int x2,int x3,int y1,int y2,int y3,int range,double rate,int times,Graphics g,Shape p){ if(times==0){ //画线,构成一个三角形 g.drawLine(x1, y1, x2, y2); g.drawLine(x3, y3, x2, y2); g.drawLine(x1, y1, x3, y3); return ; } else{ times--; Random rand=new Random(); //注意数据需要初始化 int x4=0,x5=0,x6=0,y4=0,y5=0,y6=0; int flag1=0,flag2=0,flag3=0; //遍历这个表 for(Shape s : list) { //相等的两种状况 if((s.pX1()==x1&&s.pX2()==x2&&s.pY1()==y1&&s.pY2()==y2)||(s.pX1()==x2&&s.pX2()==x1&&s.pY1()==y2&&s.pY2()==y1)){ if(s.show(g)==1){ //如果这个边已经被使用过了,那么此时说明中点已经生成,只需要读出来中点即可 x4=s.pX3(); y4=s.pY3(); //System.out.println(times+":same:==================size:"+(i++)); } else{ //如果这条边存在但没有使用,那么生成这个中点,进行更新,其实就是将原来的shape删除掉,加入一个新的。 x4=(x1+x2)/2; y4=(y1+y2)/2+rand.nextInt(range*2)-range; //System.out.println(times+":1"); list.remove(s); p =new Shape(x1,x2,x4,y1,y2,y4,1); list.add(p); } flag1=1; break; } } if(flag1==0){ //System.out.println(times+":before:==================size:"+(i++)); //如果不存在,这个很简单,只需要构造一个就好了 x4=(x1+x2)/2; y4=(y1+y2)/2+rand.nextInt(range*2)-range; p =new Shape(x1,x2,x4,y1,y2,y4,1); list.add(p); } //后面两种情况类似 for(Shape s : list) { if((s.pX1()==x1&&s.pX2()==x3&&s.pY1()==y1&&s.pY2()==y3)||(s.pX1()==x3&&s.pX2()==x1&&s.pY1()==y3&&s.pY2()==y1)){ if(s.show(g)==1){ x5=s.pX3(); y5=s.pY3(); } else{ x5=(x1+x3)/2; y5=(y1+y3)/2+rand.nextInt(range*2)-range; list.remove(s); p =new Shape(x1,x3,x5,y1,y3,y5,1); list.add(p); } flag2=1; break; } } if(flag2==0){ x5=(x1+x3)/2; y5=(y1+y3)/2+rand.nextInt(range*2)-range; //System.out.println(times+":before1:==================size:"+(i++)); p =new Shape(x1,x2,x4,y1,y2,y4,1); list.add(p); } for(Shape s : list){ if((s.pX1()==x2&&s.pX2()==x3&&s.pY1()==y2&&s.pY2()==y3)||(s.pX1()==x3&&s.pX2()==x2&&s.pY1()==y3&&s.pY2()==y2)){ if(s.show(g)==1){ x6=s.pX3(); y6=s.pY3(); } else{ x6=(x2+x3)/2; y6=(y2+y3)/2+rand.nextInt(range*2)-range; list.remove(s); p =new Shape(x2,x3,x6,y2,y3,y6,1); list.add(p); } flag3=1; break; } } if(flag3==0){ x6=(x2+x3)/2; y6=(y2+y3)/2+rand.nextInt(range*2)-range; //System.out.println(times+":before2:==================size:"+(i++)); p =new Shape(x1,x2,x4,y1,y2,y4,1); list.add(p); } //逐渐缩小范围 range=(int)(range*rate); //将边添加进行,类似一个初始话,标记位为0 p =new Shape(x1,x4,y1,y4,0); list.add(p); p =new Shape(x1,x5,y1,y5,0); list.add(p); p =new Shape(x4,x5,y4,y5,0); list.add(p); p =new Shape(x2,x4,y2,y4,0); list.add(p); p =new Shape(x2,x6,y2,y6,0); list.add(p); p =new Shape(x4,x6,y4,y6,0); list.add(p); p =new Shape(x3,x5,y3,y5,0); list.add(p); p =new Shape(x3,x6,y3,y6,0); list.add(p); p =new Shape(x5,x6,y5,y6,0); list.add(p); //画四个三角形 divide(x1,x4,x5,y1,y4,y5,range,rate,times,g,p); divide(x2,x4,x6,y2,y4,y6,range,rate,times,g,p); divide(x3,x5,x6,y3,y5,y6,range,rate,times,g,p); divide(x4,x5,x6,y4,y5,y6,range,rate,times,g,p); } } public static void main(String[] args) { // TODO Auto-generated method stub JFrame moun=new JFrame(); moun.setSize(1000, 600); moun.setLocationRelativeTo(null); moun.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); moun.setVisible(true); Graphics g=moun.getGraphics();//获取窗体 int x1=500,x2=100,x3=850; int y1=100,y2=400,y3=400; int range=200; double rate=0.5; int times=7; ShowUI a=new ShowUI(); try { Thread.sleep(566); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Shape p = null; //System.out.println(100); a.divide(x1, x2, x3, y1, y2, y3, range, rate,times,g,p); } }
Shape类
package Mountion; import java.awt.Graphics; public class Shape { private int x1,x2,x3,y1,y2,y3; private int flag; //这个构造函数存储的是一个边的两个点,以及这个边生成的中点 public Shape(int x1,int x2,int x3,int y1,int y2,int y3,int flag){ this.x1=x1; this.x2=x2; this.x3=x3; this.y1=y1; this.y2=y2; this.y3=y3; this.flag=flag; } //这个构造函数存储的一条边的两个点 public Shape(int x1,int x2,int y1,int y2,int flag){ this.x1=x1; this.x2=x2; this.y1=y1; this.y2=y2; this.flag=flag; } //flag标记位,这条边是否被使用过 public int show(Graphics g) { // TODO Auto-generated method stub return flag; } public int pX1(){ return x1; } public int pX2(){ return x2; } public int pX3(){ return x3; } public int pY1(){ return y1; } public int pY2(){ return y2; } public int pY3(){ return y3; } }
效果展示
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。