大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。
1 public void draw1(int x1, int y1, int x2, int y2,int depth) {//科赫曲线 keleyi.com 2 g.drawLine(x1, y1, x2, y2); 3 if (depth<=1) 4 return; 5 else {//得到三等分点 6 double x11 = (x1 * 2 + x2) / 3; 7 double y11 = (y1 * 2 + y2) / 3; 8 9 double x22 = (x1 + x2 * 2) / 3; 10 double y22 = (y1 + y2 * 2) / 3; 11 12 double x33 = (x11 + x22) / 2 - (y11 - y22) * Math.sqrt(3) / 2; 13 double y33 = (y11 + y22) / 2 - (x22 - x11) * Math.sqrt(3) / 2; 14 15 g.setColor(j.getBackground()); 16 g.drawLine((int) x1, (int) y1, (int) x2, (int) y2); 17 g.setColor(Color.black); 18 draw1((int) x1, (int) y1, (int) x11, (int) y11,depth-1); 19 draw1((int) x11, (int) y11, (int) x33, (int) y33,depth-1); 20 draw1((int) x22, (int) y22, (int) x2, (int) y2,depth-1); 21 draw1((int) x33, (int) y33, (int) x22, (int) y22,depth-1); 22 } 23 }
正方形:
1 public void draw2(int x1, int y1, int m,int depth) {//正方形 keleyi.com 2 g.fillRect(x1, y1, m, m); 3 m = m / 3; 4 if (depth<=1) 5 return; 6 else{ 7 double x11 = x1 - 2 * m; 8 double y11 = y1 - 2 * m; 9 10 double x22 = x1 + m; 11 double y22 = y1 - 2 * m; 12 13 double x33 = x1 + 4 * m; 14 double y33 = y1 - 2 * m; 15 16 double x44 = x1 - 2 * m; 17 double y44 = y1 + m; 18 19 double x55 = x1 + 4 * m; 20 double y55 = y1 + m; 21 22 double x66 = x1 - 2 * m; 23 double y66 = y1 + 4 * m; 24 25 double x77 = x1 + m; 26 double y77 = y1 + 4 * m; 27 28 double x88 = x1 + 4 * m; 29 double y88 = y1 + 4 * m; 30 31 draw2((int) x11, (int) y11, (int) m,depth-1); 32 33 draw2((int) x22, (int) y22, (int) m,depth-1); 34 35 draw2((int) x33, (int) y33, (int) m,depth-1); 36 37 draw2((int) x44, (int) y44, (int) m,depth-1); 38 39 draw2((int) x55, (int) y55, (int) m,depth-1); 40 41 draw2((int) x66, (int) y66, (int) m,depth-1); 42 43 draw2((int) x77, (int) y77, (int) m,depth-1); 44 45 draw2((int) x88, (int) y88, (int) m,depth-1); 46 } 47 48 }
谢冰斯基三角形:
1 public void draw3(int x1,int y1,int x2,int y2,int x3,int y3,int depth){//三角形 keleyi.com 2 3 double s = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); 4 g.drawLine(x1,y1,x2,y2); 5 g.drawLine(x2,y2,x3,y3); 6 g.drawLine(x1,y1,x3,y3); 7 // if(s<3) 8 // return; 9 if (depth<=1) 10 return; 11 else 12 { 13 /* 14 * 上面的三角形 15 */ 16 double x11=(x1*3+x2)/4; 17 double y11=y1-(s/4)*Math.sqrt(3); 18 19 double x12=(x1+x2*3)/4; 20 double y12=y11; 21 22 double x13=(x1+x2)/2; 23 double y13=y1; 24 25 /* 26 * 左边的三角形 27 */ 28 double x21=x1-s/4; 29 double y21=(y1+y3)/2; 30 31 double x22=x1+s/4; 32 double y22=y21; 33 34 double x23=x1; 35 double y23=y3; 36 37 /* 38 * 右边的三角形 39 */ 40 double x31=x2+s/4; 41 double y31=(y1+y3)/2; 42 43 double x32=x2-s/4; 44 double y32=y21; 45 46 double x33=x2; 47 double y33=y3; 48 49 50 draw3((int)x11,(int)y11,(int)x12,(int)y12, (int)x13, (int)y13, depth-1); 51 draw3((int)x21,(int)y21,(int)x22,(int)y22, (int)x23, (int)y23, depth-1); 52 draw3((int)x31,(int)y31,(int)x32,(int)y32, (int)x33, (int)y33, depth-1); 53 } 54 }
科赫曲线是一种外形像雪花的几何曲线,所以又称为雪花曲线,它是分形曲线中的一种,具体画法如下:
1、任意画一个正三角形,并把每一边三等分;
2、取三等分后的一边中间一段为边向外作正三角形,并把这“中间一段”擦掉;
3、重复上述两步,画出更小的三角形。
4、一直重复,直到无穷,所画出的曲线叫做科赫曲线。
小结:分形是个很好玩的东西,根据自己的奇妙想象可以画出很多很好看的图形,不仅仅是已经存在的,你可以创造出属于你自己的图形!
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/120795.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...