使用暴力的方法(循环)实现科赫曲线

用暴力的方法画出科赫曲线(循环方法),注释代码如下:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
/**
 * 用暴力方法实现科赫曲线(循环实现)
 * @author LONG
 *
 */
public class Kehe extends JFrame {
	private Dimension di = null;	//创建Dimension类型的变量来保存屏幕尺寸
	private Graphics2D gr = null;	//创建Graphics类型变量来保存画布对象
	private static final long serialVersionUID = 1L;
	private int[] old_x = new int[2];	//创建初始化x数组用来动态保存原来的坐标
	private int[] old_y = new int[2];	//创建初始化y数组用来动态保存原来的坐标
	private int[] new_x = new int[2];	//创建数组来保存现在需要画的x坐标
	private int[] new_y = new int[2];	//创建数组来保存现在需要画的y坐标
	private JPanel jp_draw = null;		//声明面板
	public static void main(String[] args){
		Kehe ke = new Kehe();
		ke.showFrame();
	}
	public void showFrame(){
		this.setTitle("科赫曲线");
		Toolkit tl = Toolkit.getDefaultToolkit();
		di = tl.getScreenSize();
		
		//初始化数组
		new_x[0] = 0; new_y[0] = di.height*3/4;		
		new_x[1] = di.width; new_y[1] = di.height*3/4;
		
		this.setSize(di.width,di.height);
		this.setDefaultCloseOperation(3);
		jp_draw = new JPanel();
		jp_draw.setPreferredSize(new Dimension(di.width,di.height));
		jp_draw.setBackground(Color.WHITE);
		this.setResizable(false);
		this.add(jp_draw);
		this.setVisible(true);
		gr = (Graphics2D)jp_draw.getGraphics();
		
		jp_draw.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){
				Start();
			}
		});
	}
	/**
	 * (基于我的暴力画法分析)
	 * 在画之前可以分析一下,科赫曲线,以最简单的思维来看,就是在一条条直线上画正三角形,
	 * 然后再重复前面的过程再画,只是画的时候,直线的位置可能有所不同,因为有水平的直线
	 * 还有倾斜的直线,但是我们们可以看出,这些直线的有规律可循的,总之就是以PI/6的大小
	 * 在变化倾斜度,所以只要我们分清楚是哪一种,就可以计算出坐标,存储在数组中,最后把
	 * 这些点连接起来就可以了。
	 * 
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 */
	
	public void doSomething(int x1,int y1,int x2,int y2){

		old_x = new_x;		//将旧的数组指向新的数组
		old_y = new_y;		//将旧的数组指向新的数组
		
		int length = old_x.length;			//得到上一个数组的长度,为计算下一次数组的长度做铺垫
		new_x = new int[3 * (length - 1) + length];		//扩充新的数组
		new_y = new int[3 * (length - 1) + length];		//扩充新的数组
		for(int q = 0; q < old_x.length - 1; q++){	//遍历整个旧的数组得到新的坐标
				x1 = old_x[q]; y1 = old_y[q];	
				x2 = old_x[q+1]; y2 = old_y[q+1];
				if(Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)) > 3){
					//使用collection-based for循环遍历旧的数组,将旧的数组的值放到新数组中
					int g = 0;
					for(int n : old_x){		
						new_x[g] = n;
						g += 4;
					}
					g = 0;
					for(int n : old_y){
						new_y[g] = n;
						g += 4;
					}
					for(int i = 0; i < old_x.length - 1; i++){		//循环的次数是旧的数组中保存的边数,然后计算下一次的新增坐标
						if(old_y[i] == old_y[i + 1]){	//判断说明这条线是水平的,水平的时候再判断一下大小
							//接着生成这条边上的新的三个点,并且付给新的数组
							if(old_x[i] < old_x[i + 1]){
								int x11 = old_x[i] + (old_x[i + 1] - old_x[i])/3;
								int y11 = old_y[i];
								int x33 = old_x[i] + 2 * (old_x[i + 1] - old_x[i])/3;
								int y33 = old_y[i];
								int x22 = (x11 + x33)/2;
								int y22 = y11 - (int)((x33 - x11)*Math.sqrt(3)/2);
								new_x[4 * i + 1] = x11;
								new_y[4 * i + 1] = y11;
								new_x[4 * i + 2] = x22;
								new_y[4 * i + 2] = y22;
								new_x[4 * i + 3] = x33;
								new_y[4 * i + 3] = y33;
							}else{
								int x11 = old_x[i + 1] + 2 * (old_x[i] - old_x[i + 1])/3;
								int y11 = old_y[i];
								int x33 = old_x[i + 1] + (old_x[i] - old_x[i + 1])/3;
								int y33 = old_y[i];
								int x22 = (x11 + x33)/2;
								int y22 = y11 + (int)((x11 - x33)*Math.sqrt(3)/2);
								new_x[4 * i + 1] = x11;
								new_y[4 * i + 1] = y11;
								new_x[4 * i + 2] = x22;
								new_y[4 * i + 2] = y22;
								new_x[4 * i + 3] = x33;
								new_y[4 * i + 3] = y33;
							}
							
						}else if(old_x[i] < old_x[i + 1] && old_y[i] > old_y[i + 1]){
							int x11 = old_x[i] + (old_x[i + 1] - old_x[i])/3;
							int y11 = old_y[i + 1] + 2 * (old_y[i] - old_y[i + 1])/3;
							int x33 = old_x[i] + 2 * (old_x[i + 1] - old_x[i])/3;
							int y33 = old_y[i + 1] +(old_y[i] - old_y[i + 1])/3;
							int c_x = (x11 + x33)/2;
							int c_y = (y11 + y33)/2;
							int h = (int)(Math.sqrt(Math.pow(x33 - x11, 2) + Math.pow(y33 - y11, 2))*Math.sqrt(3)/2);
							int dx = (int)(Math.cos(Math.PI/6) * h);
							int dy = (int)(Math.sin(Math.PI/6) * h);
							int x22 = c_x - dx;
							int y22 = c_y - dy;
							new_x[4 * i + 1] = x11;
							new_y[4 * i + 1] = y11;
							new_x[4 * i + 2] = x22;
							new_y[4 * i + 2] = y22;
							new_x[4 * i + 3] = x33;
							new_y[4 * i + 3] = y33;
						}else if(old_x[i] < old_x[i + 1] && old_y[i] < old_y[i + 1]){
							int x11 = old_x[i] + (old_x[i + 1] - old_x[i])/3;
							int y11 = old_y[i] + (old_y[i + 1] - old_y[i])/3;
							int x33 = old_x[i] + 2 * (old_x[i + 1] - old_x[i])/3;
							int y33 = old_y[i] + 2 * (old_y[i + 1] - old_y[i])/3;
							int c_x = (x11 + x33)/2;
							int c_y = (y11 + y33)/2;
							int h = (int)(Math.sqrt(Math.pow(x33 - x11, 2) + Math.pow(y33 - y11, 2))*Math.sqrt(3)/2);
							int dx = (int)(Math.cos(Math.PI/6) * h);
							int dy = (int)(Math.sin(Math.PI/6) * h);
							int x22 = c_x + dx;
							int y22 = c_y - dy;
							new_x[4 * i + 1] = x11;
							new_y[4 * i + 1] = y11;
							new_x[4 * i + 2] = x22;
							new_y[4 * i + 2] = y22;
							new_x[4 * i + 3] = x33;
							new_y[4 * i + 3] = y33;
						}else if(old_x[i] > old_x[i + 1] && old_y[i] > old_y[i + 1]){
							int x11 = old_x[i + 1] + 2 * (old_x[i] - old_x[i + 1])/3;
							int y11 = old_y[i + 1] + 2 * (old_y[i] - old_y[i + 1])/3;
							int x33 = old_x[i + 1] + (old_x[i] - old_x[i + 1])/3;
							int y33 = old_y[i + 1] + (old_y[i] - old_y[i + 1])/3;
							int c_x = (x11 + x33)/2;
							int c_y = (y11 + y33)/2;
							int h = (int)(Math.sqrt(Math.pow(x33 - x11, 2) + Math.pow(y33 - y11, 2))*Math.sqrt(3)/2);
							int dx = (int)(Math.cos(Math.PI/6) * h);
							int dy = (int)(Math.sin(Math.PI/6) * h);
							int x22 = c_x - dx;
							int y22 = c_y + dy;
							new_x[4 * i + 1] = x11;
							new_y[4 * i + 1] = y11;
							new_x[4 * i + 2] = x22;
							new_y[4 * i + 2] = y22;
							new_x[4 * i + 3] = x33;
							new_y[4 * i + 3] = y33;
						}else if(old_x[i] > old_x[i + 1] && old_y[i + 1] > old_y[i]){
							int x11 = old_x[i + 1] + 2 * (old_x[i] - old_x[i + 1])/3;
							int y11 = old_y[i] + (old_y[i + 1] - old_y[i])/3;
							int x33 = old_x[i + 1] + (old_x[i] - old_x[i + 1])/3;
							int y33 = old_y[i] + 2 * (old_y[i + 1] - old_y[i])/3;
							int c_x = (x11 + x33)/2;
							int c_y = (y11 + y33)/2;
							int h = (int)(Math.sqrt(Math.pow(x33 - x11, 2) + Math.pow(y33 - y11, 2))*Math.sqrt(3)/2);
							int dx = (int)(Math.cos(Math.PI/6) * h);
							int dy = (int)(Math.sin(Math.PI/6) * h);
							int x22 = c_x + dx;
							int y22 = c_y + dy;
							new_x[4 * i + 1] = x11;
							new_y[4 * i + 1] = y11;
							new_x[4 * i + 2] = x22;
							new_y[4 * i + 2] = y22;
							new_x[4 * i + 3] = x33;
							new_y[4 * i + 3] = y33;
						}
					}
					
				}else{//选择判断语句结束
					break;
				}
			}
		
		
		}
		
		
	//}
	public void Start(){	//用来调用doSomething函数进行求点画图
				//动态的调用的doSomething()函数得到足够大的坐标集
		
		
			for(int i = 0; i < 5; i++){
				doSomething(new_x[0], new_y[0], new_x[1], new_y[1]);
			}
			for(int j = 0; j < new_x.length - 1; j++){		//将坐标集里面的点连接起来
				gr.drawLine(new_x[j], new_y[j], new_x[j + 1], new_y[j + 1]);
			
			}
		
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值