JAVA语言实现CyrusBeck算法,须有图形显示,显示各阶段结果

时间: 2024-02-29 15:52:35 浏览: 25
以下是Java实现的Cyrus-Beck算法,带有图形显示: ```java import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JPanel; public class CyrusBeckAlgorithmWithGraphics extends JPanel { // 定义线段的两个端点 private Point p1, p2; // 定义矩形剪裁窗口的四个顶点 private Point[] clipWindow = new Point[4]; // 定义当前鼠标拖动的点 private Point currentPoint; public CyrusBeckAlgorithmWithGraphics() { // 初始化线段和剪裁窗口 p1 = new Point(50, 50); p2 = new Point(200, 200); clipWindow[0] = new Point(150, 100); clipWindow[1] = new Point(250, 100); clipWindow[2] = new Point(250, 200); clipWindow[3] = new Point(150, 200); // 添加鼠标拖动事件监听器 addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { currentPoint = e.getPoint(); } }); addMouseMotionListener(new MouseAdapter() { public void mouseDragged(MouseEvent e) { p1.translate(e.getPoint().x - currentPoint.x, e.getPoint().y - currentPoint.y); p2.translate(e.getPoint().x - currentPoint.x, e.getPoint().y - currentPoint.y); currentPoint = e.getPoint(); repaint(); } }); } // 计算法向量 private Point computeNormal(Point p1, Point p2) { int dx = p2.x - p1.x; int dy = p2.y - p1.y; return new Point(dy, -dx); } // 计算点积 private double dotProduct(Point a, Point b) { return a.x * b.x + a.y * b.y; } // 计算线段起点到P点的向量 private Point computeVector(Point p) { return new Point(p.x - p1.x, p.y - p1.y); } // 计算交点 private Point computeIntersection(Point p, Point d) { Point e = new Point(p1.x - p.x, p1.y - p.y); double t = dotProduct(computeNormal(p1, p2), e) / dotProduct(computeNormal(p1, p2), d); return new Point((int) (p.x + t * d.x), (int) (p.y + t * d.y)); } // 判断点是否在剪裁窗口内 private boolean isInside(Point p) { Point d = new Point(p2.x - p1.x, p2.y - p1.y); Point n = computeNormal(p1, p2); Point w = computeVector(p); double nd = dotProduct(n, d); if (nd == 0) { return false; } double t = -dotProduct(n, w) / nd; return t >= 0 && t <= 1; } // 计算Cyrus-Beck剪裁后的线段 public Point[] cyrusBeckClip(Point p, Point q) { Point d = new Point(q.x - p.x, q.y - p.y); Point[] result = new Point[2]; int count = 0; if (isInside(p)) { result[count++] = p; } if (isInside(q)) { result[count++] = q; } Point pi, qi; pi = computeIntersection(p, d); qi = computeIntersection(q, d); if (isInside(pi)) { result[count++] = pi; } if (isInside(qi)) { result[count++] = qi; } if (count == 2) { return result; } else { return null; } } public void paint(Graphics g) { // 绘制剪裁窗口 g.setColor(Color.RED); for (int i = 0; i < 4; i++) { g.drawLine(clipWindow[i].x, clipWindow[i].y, clipWindow[(i + 1) % 4].x, clipWindow[(i + 1) % 4].y); } // 绘制线段 g.setColor(Color.BLUE); g.drawLine(p1.x, p1.y, p2.x, p2.y); // 剪裁线段 Point[] clippedLine = cyrusBeckClip(p1, p2); if (clippedLine != null) { // 绘制剪裁后的线段 g.setColor(Color.GREEN); g.drawLine(clippedLine[0].x, clippedLine[0].y, clippedLine[1].x, clippedLine[1].y); } } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); frame.setLocationRelativeTo(null); frame.setTitle("Cyrus-Beck Algorithm with Graphics"); CyrusBeckAlgorithmWithGraphics panel = new CyrusBeckAlgorithmWithGraphics(); frame.add(panel); frame.setVisible(true); } } ``` 在这个例子中,我们使用Java Swing框架创建一个窗口,并在窗口中绘制剪裁窗口和线段。我们还添加了鼠标拖动事件监听器,使得用户可以拖动线段。在每次绘制时,我们都会计算剪裁后的线段,并绘制出来。这样,用户就可以看到线段在剪裁窗口内部的变化过程。

相关推荐

最新推荐

recommend-type

银行家算法Java实现带图形界面

图形界面银行家算法,设计有 n 个进程共享 m 个系统资源的系统,进程可动态的申请和释放资源,系统按各进程的申请动态的分配资源。系统能显示各个进程申请和释放资源,以及系统动态分配资源的过程,便于用户观察和...
recommend-type

java语言实现权重随机算法完整实例

主要介绍了java语言实现权重随机算法完整实例,具有一定借鉴价值,需要的朋友可以参考下。
recommend-type

基于java实现的ECC加密算法示例

主要介绍了基于java实现的ECC加密算法,简单说明了ECC算法的概念、原理,并结合实例形式分析了java实现ECC加密算法的定义与使用技巧,需要的朋友可以参考下
recommend-type

Java编程实现轨迹压缩之Douglas-Peucker算法详细代码

主要介绍了Java编程实现轨迹压缩之Douglas-Peucker算法详细代码,具有一定借鉴价值,需要的朋友可以参考。
recommend-type

java利用DFA算法实现敏感词过滤功能

在最近的开发中遇到了敏感词过滤,便去网上查阅了很多敏感词过滤的资料,在这里也和...下面这篇文章主要给大家介绍了关于java利用DFA算法实现敏感词过滤功能的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。