编写完整,包括输入类和Point类1.编写一个线段类MyLine 要求如下: (1)主要属性有两个端点:e1、e2,类型为Point。 (2)编写构造方法,如MyLine(Point p1,Point p2)。 (3)编写成员方法。 例如: a.检查线段是否位于第一象限check()。 b.求线段的长度length()。() c.判断两条直线是否相交(另一线段作为参数)。(参考API中Line2D类) d.求一点到该线段(或延长线)的距离。(参考API中Line2D类)
时间: 2024-02-06 14:12:40 浏览: 109
java线段类
5星 · 资源好评率100%
输入类Input:
```java
import java.util.Scanner;
public class Input {
private static Scanner scanner = new Scanner(System.in);
public static int getInt(String prompt) {
System.out.print(prompt);
return scanner.nextInt();
}
public static double getDouble(String prompt) {
System.out.print(prompt);
return scanner.nextDouble();
}
}
```
Point类:
```java
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double distanceTo(Point other) {
double dx = other.x - x;
double dy = other.y - y;
return Math.sqrt(dx * dx + dy * dy);
}
}
```
MyLine类:
```java
import java.awt.geom.Line2D;
public class MyLine {
private Point e1;
private Point e2;
public MyLine(Point p1, Point p2) {
this.e1 = p1;
this.e2 = p2;
}
public boolean check() {
return e1.getX() >= 0 && e2.getX() >= 0 && e1.getY() >= 0 && e2.getY() >= 0;
}
public double length() {
return e1.distanceTo(e2);
}
public boolean intersects(MyLine other) {
return Line2D.linesIntersect(e1.getX(), e1.getY(), e2.getX(), e2.getY(), other.e1.getX(), other.e1.getY(), other.e2.getX(), other.e2.getY());
}
public double distanceTo(Point p) {
return Line2D.ptLineDist(e1.getX(), e1.getY(), e2.getX(), e2.getY(), p.getX(), p.getY());
}
}
```
阅读全文