Point2D.java

package onlyfun.caterpillar;

 

public class Point2D{

  private int x;

  private int y;

 

  public Point2D{

  }

 

  public Point2D(int x,int y){

    this.x=x;

    this.y=y;

  }

 

  public int getX(){return x;}

  public int getY(){return y;}

}

package work; import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; public class CyrusBeckAlgorithmApplet extends Applet { private static final long serialVersionUID = 1L; private Point2D.Double[] clipWindow; private Point2D.Double[][] lines; private double[][] vectors; private double[] p1, p2, D; @Override public void init() { clipWindow = new Point2D.Double[3]; clipWindow[0] = new Point2D.Double(200, 275); clipWindow[1] = new Point2D.Double(250.0 / 3, 100); clipWindow[2] = new Point2D.Double(950.0 / 3, 100); lines = new Point2D.Double[2][2]; lines[0][0] = new Point2D.Double(0, 120); lines[0][1] = new Point2D.Double(400, 120); lines[1][0] = new Point2D.Double(0, 180); lines[1][1] = new Point2D.Double(400, 180); vectors = new double[2][2]; D = new double[2]; } @Override public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; // draw clip window g2d.setColor(Color.BLACK); g2d.draw(new Line2D.Double(clipWindow[0], clipWindow[1])); g2d.draw(new Line2D.Double(clipWindow[1], clipWindow[2])); g2d.draw(new Line2D.Double(clipWindow[2], clipWindow[0])); // draw lines for (int i = 0; i < lines.length; i++) { Point2D.Double p1 = lines[i][0]; Point2D.Double p2 = lines[i][1]; cyrusBeckClip(g2d, p1, p2); } } private void cyrusBeckClip(Graphics2D g2d, Point2D.Double p1, Point2D.Double p2) { double tE = 0, tL = 1; double dx = p2.x - p1.x; double dy = p2.y - p1.y; for (int i = 0; i < clipWindow.length; i++) { Point2D.Double P1 = clipWindow[i]; Point2D.Double P2 = clipWindow[(i + 1) % clipWindow.length]; double nx = -(P2.y - P1.y); double ny = P2.x - P1.x; double D = -nx * P1.x - ny * P1.y; double numerator = nx * p1.x + ny * p1.y + D; double denominator = -(nx * dx + ny * dy); if (denominator == 0) { if (numerator < 0) { return; } } else { double t = numerator / denominator; if (denominator < 0) { tE = Math.max(tE, t); } else { tL = Math.min(tL, t); } } } if (tE <= tL) { double x1 = p1.x + tE * dx; double y1 = p1.y + tE * dy; double x2 = p1.x + tL * dx; double y2 = p1.y + tL * dy; g2d.setColor(Color.BLUE); g2d.draw(new Line2D.Double(p1, new Point2D.Double(x1, y1))); g2d.setColor(Color.RED); g2d.draw(new Line2D.Double(new Point2D.Double(x1, y1), new Point2D.Double(x2, y2))); g2d.setColor(Color.BLUE); g2d.draw(new Line2D.Double(new Point2D.Double(x2, y2), p2)); } } } 将此代码改为 Java 应用程序运行
05-16
import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.util.*; public class OrthogonalPathFinder { private static final float GRID_SIZE = 0.5f; // 网格步长,可根据需要调整 private static final float EPSILON = 1e-5f; // 浮点数比较容差 static class Node implements Comparable<Node> { Point2D.Float point; Node parent; double gScore; // 从起点到当前节点的实际代价 double fScore; // 总估计代价 (gScore + 启发式代价) Node(Point2D.Float point, Node parent, double gScore, double fScore) { this.point = point; this.parent = parent; this.gScore = gScore; this.fScore = fScore; } @Override public int compareTo(Node other) { return Double.compare(this.fScore, other.fScore); } } public static List<Point2D.Float> findPath(List<Rectangle2D.Float> obstacles, Point2D.Float start, Point2D.Float end) { // 检查起点终点是否在障碍物内 if (isPointInObstacle(start, obstacles) || isPointInObstacle(end, obstacles)) { return Collections.emptyList(); } // 计算搜索边界 float[] bounds = calculateBounds(obstacles, start, end); float minX = bounds[0], minY = bounds[1], maxX = bounds[2], maxY = bounds[3]; // A* 算法初始化 PriorityQueue<Node> openSet = new PriorityQueue<>(); Map<Point2D.Float, Double> gScoreMap = new HashMap<>(); Map<Point2D.Float, Node> nodeMap = new HashMap<>(); Node startNode = new Node(start, null, 0, heuristic(start, end)); openSet.add(startNode); gScoreMap.put(start, 0.0); while (!openSet.isEmpty()) { Node current = openSet.poll(); Point2D.Float currentPoint = current.point; // 到达终点,回溯路径 if (isEqual(currentPoint, end)) { return reconstructPath(current); } // 生成四个方向的邻居点 Point2D.Float[] neighbors = { new Point2D.Float(currentPoint.x + GRID_SIZE, currentPoint.y), // 右 new Point2D.Float(currentPoint.x - GRID_SIZE, currentPoint.y), // 左 new Point2D.Float(currentPoint.x, currentPoint.y + GRID_SIZE), // 上 new Point2D.Float(currentPoint.x, currentPoint.y - GRID_SIZE) // 下 }; for (Point2D.Float neighbor : neighbors) { // 检查邻居点是否在边界内 if (neighbor.x < minX || neighbor.x > maxX || neighbor.y < minY || neighbor.y > maxY) { continue; } // 检查线段是否与障碍物相交 if (isLineIntersectingObstacle(currentPoint, neighbor, obstacles)) { continue; } // 计算新的代价 double tentativeGScore = current.gScore + GRID_SIZE; double oldGScore = gScoreMap.getOrDefault(neighbor, Double.POSITIVE_INFINITY); // 如果新路径更好,更新节点 if (tentativeGScore < oldGScore) { double fScore = tentativeGScore + heuristic(neighbor, end); Node neighborNode = new Node(neighbor, current, tentativeGScore, fScore); gScoreMap.put(neighbor, tentativeGScore); openSet.add(neighborNode); } } } return Collections.emptyList(); // 无路径 } // 回溯重建路径 private static List<Point2D.Float> reconstructPath(Node endNode) { LinkedList<Point2D.Float> path = new LinkedList<>(); Node current = endNode; while (current != null) { path.addFirst(current.point); current = current.parent; } return path; } // 计算曼哈顿距离作为启发式函数 private static double heuristic(Point2D.Float a, Point2D.Float b) { return Math.abs(a.x - b.x) + Math.abs(a.y - b.y); } // 检查点是否在障碍物内 private static boolean isPointInObstacle(Point2D.Float point, List<Rectangle2D.Float> obstacles) { for (Rectangle2D.Float rect : obstacles) { if (rect.contains(point.x, point.y)) { return true; } } return false; } // 检查线段是否与障碍物相交 private static boolean isLineIntersectingObstacle(Point2D.Float a, Point2D.Float b, List<Rectangle2D.Float> obstacles) { if (Math.abs(a.y - b.y) < EPSILON) { // 水平线段 float y = a.y; float x1 = Math.min(a.x, b.x); float x2 = Math.max(a.x, b.x); for (Rectangle2D.Float rect : obstacles) { if (y >= rect.getMinY() && y <= rect.getMaxY()) { if (x1 <= rect.getMaxX() && x2 >= rect.getMinX()) { return true; } } } } else if (Math.abs(a.x - b.x) < EPSILON) { // 垂直线段 float x = a.x; float y1 = Math.min(a.y, b.y); float y2 = Math.max(a.y, b.y); for (Rectangle2D.Float rect : obstacles) { if (x >= rect.getMinX() && x <= rect.getMaxX()) { if (y1 <= rect.getMaxY() && y2 >= rect.getMinY()) { return true; } } } } return false; } // 计算搜索边界 private static float[] calculateBounds(List<Rectangle2D.Float> obstacles, Point2D.Float start, Point2D.Float end) { float minX = Math.min(start.x, end.x); float minY = Math.min(start.y, end.y); float maxX = Math.max(start.x, end.x); float maxY = Math.max(start.y, end.y); for (Rectangle2D.Float rect : obstacles) { minX = Math.min(minX, rect.x); minY = Math.min(minY, rect.y); maxX = Math.max(maxX, rect.x + rect.width); maxY = Math.max(maxY, rect.y + rect.height); } // 扩展边界 minX -= 1.0f; minY -= 1.0f; maxX += 1.0f; maxY += 1.0f; return new float[]{minX, minY, maxX, maxY}; } // 浮点数比较(容差) private static boolean isEqual(Point2D.Float a, Point2D.Float b) { return Math.abs(a.x - b.x) < EPSILON && Math.abs(a.y - b.y) < EPSILON; } }
05-30
Java程序设计》课程实验指导书程序代码(答案)(实验四:java继承与多态),个人原创,仅供参考与交流。 希望多多交流,共同进步! 实验四 java继承与多态 一、实验目的: 掌握继承、多态的概念与实现方法; 掌握包接口的定义使用方法; 了解JAVA语言实现多继承的途径; 二、实验内容: 1.分别编写两个类Point2DPoint3D来表示二维空间三维空间的点,使之满足下列要求: (1) Point2D有两个整型成员变量x, y (分别为二维空间的X,Y方向坐标),Point2D构造方法要实现对其成员变量x, y的初始化。 (2)Point2D有一个void型成员方法offset(int a, int b),它可以实现Point2D的平移。 (3)Point3D是Point2D的直接子类,它有有三个整型成员变量x,y,z (分别为三维空间的X,Y,Z方向坐标),Point3D有两个构造方法Point3D(int x,int y,int z)Point3D(Point2D p,int z),两者均可实现对Point3D的成员变量x, y,z的初始化。 (4)Point3D有一个void型成员方法offset(int a, int b,int c),该方法可以实现Point3D的平移。 (5)在Point3D中的主函数main()中实例化两个Point2D的对象p2d1,p2d2,打印出它们之间的距离,再实例化两个Point2D的对象p3d1,p3d2,打印出他们之间的距离。 2.定义抽象类Shape,抽象方法为showArea(),求出面积并显示,定义矩形类Rectangle,正方形类Square,圆类 Circle,根据各自的属性,用showArea方法求出各自的面积,在main方法中构造3个对象,调用showArea方法。 定义接口DiagArea,其中包含方法double getDiagonal()求对角线长, double getArea()求面积,定义一个矩形类,实现此接口,并自行扩充成员变量方法,定义一个正方形类继承矩形类(如矩形有长w宽h,正方形有边x,并有相应的构造函数,有一个方法中一次直接显示边长、面积对角线长),在另一类中的主方法里使用测试该类。 三、实验要求: 1. 能实现类的继承关系; 2. 用多种方法创建各个类的对象; 3. 程序应包括各个被调用方法的执行结果的显示。 4. 写出实验报告。要求记录编译执行Java程序当中的系统错误信息提示,并给出解决办法。(附运行界面、源代码)。 四、实验步骤: 1.(第1题)定义Point2D,及定义它的属性方法; 定义子类Point3D,及定义它的属性方法;在Point3D中的主函数main()中实例化两个Point2D的对象,并通过这两个对象调用它们的属性方法,输出方法执行结果。 2.(第2题)定义抽象类Shape,抽象方法为showArea(),再定义矩形类Rectangle,正方形类Square,圆类 Circle,各自的属性。定义主类、主方法,在main方法中构造3个对象,调用showArea方法;定义接口DiagArea,其中包含方法double getDiagonal(),在主main方法中输出方法执行结果。 五、自做作实验 1.定义一个描述人的基本类,该类包括人的性别出生日期两个数据成员,以及设置获取这些属性值的方法成员;再定义一个大学生类,使大学生类具有人的所有属性外,还具有姓名、学号,大学入学成绩,籍贯属性以及设置获取这些属性值的方法成员;编写完整的程序,完成一个具有班级学生信息管理功能的程序。 2创建一个接口Shape,其中有抽象方法area,类Circle 、Rectangle实现area方法计算其面积并返回。又有Star实现Shape的area方法,其返回值是0,Star类另有一返回值boolean型方法isStar;在main方法里创建一个Vector,根据随机数的不同向其中加入Shape的不同子类对象(如是1,生成Circle对象;如是2,生成Rectangle对象;如是3,生成Star对象)。然后将Vector中元素依次取出,判断其是否为Star类。如是返回其是个Star。否则返回其面积。 3..学校中有老师学生两类人,而在职研究生既是老师又是学生,对学生的管理对教师的管理在他们身上都有体现。 1)设计两个信息管理接口StudentManageInterfaceTeacherManageInterface。其中,StudentInterface接口包括setFee()方法getFee()方法,分别用于设置获取学生的学费;TeacherInterface接口包括setPay()方法getPay()方法,分别用于设置获取教师的工资 2) 定义一个研究生类Graduate,实现StudentInterface接口TeacherInterface接口,它定义的成员变量有name(姓名)、sex(性别)、age(年龄)、fee(每学期学费)、pay(月工资)。 3) 创建一个姓名为“zhangsan”的研究生,统计他的年收入学费,如果收入减去学费不足2000元,则输出“provide a loan”(需要贷款)信息。 提示: 1)定义两个接口,分别在其中申明两个方法。 2)定义主类Graduate,实现这两个接口。 3)定义主类的成员变量,构造方法。 4)给出四个接口方法的实现。 5)给出一个计算是否需要贷款的方法,在里面统计年收入学费,并输出是否需要贷款的信息。 6)写main方法。在其中创建一个姓名为“zhangsan”的研究生,调用计算是否需要贷款的方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值