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

2024-06-21 18:58

本文主要是介绍使用暴力的方法(循环)实现科赫曲线,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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]);}}
}

 

这篇关于使用暴力的方法(循环)实现科赫曲线的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1082058

相关文章

PyTorch核心方法之state_dict()、parameters()参数打印与应用案例

《PyTorch核心方法之state_dict()、parameters()参数打印与应用案例》PyTorch是一个流行的开源深度学习框架,提供了灵活且高效的方式来训练和部署神经网络,这篇文章主要介绍... 目录前言模型案例A. state_dict()方法验证B. parameters()C. 模型结构冻

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Python字符串处理方法超全攻略

《Python字符串处理方法超全攻略》字符串可以看作多个字符的按照先后顺序组合,相当于就是序列结构,意味着可以对它进行遍历、切片,:本文主要介绍Python字符串处理方法的相关资料,文中通过代码介... 目录一、基础知识:字符串的“不可变”特性与创建方式二、常用操作:80%场景的“万能工具箱”三、格式化方法

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoo WebFlux+MongoDB实现非阻塞API过程

《SpringBooWebFlux+MongoDB实现非阻塞API过程》本文介绍了如何使用SpringBootWebFlux和MongoDB实现非阻塞API,通过响应式编程提高系统的吞吐量和响应性能... 目录一、引言二、响应式编程基础2.1 响应式编程概念2.2 响应式编程的优势2.3 响应式编程相关技术

C#实现将XML数据自动化地写入Excel文件

《C#实现将XML数据自动化地写入Excel文件》在现代企业级应用中,数据处理与报表生成是核心环节,本文将深入探讨如何利用C#和一款优秀的库,将XML数据自动化地写入Excel文件,有需要的小伙伴可以... 目录理解XML数据结构与Excel的对应关系引入高效工具:使用Spire.XLS for .NETC

自定义注解SpringBoot防重复提交AOP方法详解

《自定义注解SpringBoot防重复提交AOP方法详解》该文章描述了一个防止重复提交的流程,通过HttpServletRequest对象获取请求信息,生成唯一标识,使用Redis分布式锁判断请求是否... 目录防重复提交流程引入依赖properties配置自定义注解切面Redis工具类controller