本文主要是介绍使用暴力的方法(循环)实现科赫曲线,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
用暴力的方法画出科赫曲线(循环方法),注释代码如下:
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]);}}
}
这篇关于使用暴力的方法(循环)实现科赫曲线的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!