数字。额外挑战

问题描述 投票:0回答:1

请帮助创建一个类似

public boolean isTheSame(Figure figure){}
的方法 描述:请让三角形、四边形、圆形类扩展图抽象类。

在三角形、四边形、圆形中实现方法:

具有以下参数的构造函数:

  • 三角形 – 三个顶点(点)作为参数。
  • 四边形 – 四个顶点(点)作为参数。
  • 圆 – 圆心点和半径的双倍值。

确保人物不退化。
它们都必须具有非零面积。
四边形也必须是凸的。
如果数字不好,则抛出 IllegalArgumentException。
注:非退化凸四边形被其对角线分成四个非退化三角形。
注意:双重计算并不完全准确,请在适用的情况下使用误差增量。
public Point centroid() 返回图形的质心。
质心是指平面图形的质心,而不是重心。
换句话说,它应该是“区域质心”。

public boolean isTheSame(Figure figure)

两个数字仅被视为相同:
如果他们有相同的类型
如果它们重合(例如具有相同的顶点)

注意:顶点的顺序不必相同。
注意:双重计算并不完全准确,请在适用的情况下使用误差增量。
好奇的请注意:它几乎像 equals() 但事实并非如此。 equals 方法需要与 hashCode() 一起具有一致的行为,并且像本练习中那样建立近似相等是相当复杂的。

点类已经存在。

class Point {
    private final double x;
    private final double y;

    public Point(final double x, final double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (! (o instanceof Point)) return false;
        Point point = (Point) o;
        return Double.compare(point.x, x) == 0 && Double.compare(point.y, y) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }
}`
abstract class Figure {

    public double distance(Point p1, Point p2) {
        return Math.sqrt(StrictMath.pow(p2.getX() - p1.getX(), 2) + StrictMath.pow(p2.getY() - p1.getY(), 2));
    }

    public abstract Point centroid();

    public abstract boolean isTheSame(Figure figure);

    public boolean isDegenerativeTriangle(Point a, Point b, Point c) {
        if (a == null || b == null || c == null) return true;
        if (a.getX() == a.getY() && b.getX() == b.getY() && c.getX() == c.getY()) return true;

        double d1 = distance(a, b);
        double d2 = distance(b, c);
        double d3 = distance(c, a);

        if ((d2 + d3) <= d1) return true;
        if ((d3 + d1) <= d2) return true;
        return (d1 + d2) <= d3;
    }

}
class Segment {

    Point start;
    Point end;

    public Segment(Point start, Point end) {
        if(Objects.isNull(start) || Objects.isNull(end) || start.equals(end))
            throw new RuntimeException();
        this.start = start;
        this.end = end;
    }

    Point intersection(Segment another) {
        double S1 = (end.getY() - start.getY()) / (end.getX() - start.getX());
        double S2 = (another.end.getY() - another.start.getY()) / (another.end.getX() - another.start.getX());
        if (S1 == S2) return null;

        double b1 = (start.getY() * end.getX() - end.getY() * start.getX()) / (end.getX() - start.getX());
        double b2 = (another.start.getY() * another.end.getX() - another.end.getY() * another.start.getX()) /
                (another.end.getX() - another.start.getX());

        double x = (b2 - b1) / (S1 - S2);
        double y = (S1 * b2 - S2 * b1) / (S1 - S2);

        if ((x > start.getX() && x > end.getX()) || (x > another.start.getX() && x > another.end.getX()) ||
                (x < start.getX() && x < end.getX()) || (x < another.start.getX() && x <  another.end.getX()) ||
                (y > start.getY() && y > end.getY()) || (y > another.start.getY() && y > another.end.getY()) ||
                (y < start.getY() && y < end.getY()) || (y < another.start.getY() && y < another.end.getY()))
            return null;

        return new Point(x, y);

    }
}

我尝试使用

equals()
compare()
方法进行比较,但没有任何效果。

class Quadrilateral extends Figure {
    public static final double DELTA = 0.0001;
    Point a, b, c, d;
    Point[] points;


    public Quadrilateral(Point a, Point b, Point c, Point d) {
        points = new Point[]{a, b, c, d};
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;

        if (a == null || b == null || c == null || d == null) throw new IllegalArgumentException();

        if (isDegenerativeTriangle(a, b, c)
                || isDegenerativeTriangle(b, c, d)
                || isDegenerativeTriangle(c, d, a)
                || isDegenerativeTriangle(d, a, b)) throw new IllegalArgumentException();


        if (! isConvex()) throw new IllegalArgumentException();

    }

    @Override
    public Point centroid() {
        Point center1 = centroidTriangle(a, b, c);
        Point center2 = centroidTriangle(a, c, d);
        Point center3 = centroidTriangle(a, d, b);
        Point center4 = centroidTriangle(b, d, c);
        return new Segment(center1, center2)
                .intersection(new Segment(center3, center4));
    }

    private Point centroidTriangle(Point a, Point b, Point c) {
        double x = (a.getX() + b.getX() + c.getX()) / 3;
        double y = (a.getY() + b.getY() + c.getY()) / 3;

        return new Point(x, y);
    }

    @Override
    public boolean isTheSame(Figure figure) {
        if (this == figure) return true;
        if ((figure instanceof Quadrilateral)) return true;
        figure = new Quadrilateral(a, b, c, d);

        Quadrilateral that = (Quadrilateral) figure;
        return Objects.equals(a, that.a) && Objects.equals(b, that.b) && Objects.equals(c, that.c) && Objects.equals(d, that.d);
    }

        public boolean isConvex() {
        boolean isNegative = false;
        boolean isPositive = false;
        int num_points = points.length;
        int B, C;
        for (int A = 0; A < num_points; A++) {
            B = (A + 1) % num_points;
            C = (B + 1) % num_points;

            double product =
                    prodLength(
                            points[A].getX(), points[A].getY(),
                            points[B].getX(), points[B].getY(),
                            points[C].getX(), points[C].getY());
            if (product < 0) {
                isNegative = true;
            } else if (product > 0) {
                isPositive = true;
            }
            if (isNegative && isPositive) return false;
        }
        return true;
    }

        public double prodLength(double aX, double aY, double bX, double bY, double cX, double cY) {
        double baX = aX - bX;
        double baY = aY - bY;
        double bcX = cX - bX;
        double bcY = cY - bY;

        return (baX * bcY - baY * bcX);
    }
}

我需要完成这个方法。我已经尝试了我所知道的一切,但不幸的是没有任何作用。这是我无法完成的一项测试,其余的测试都顺利通过

提前感谢您的帮助!

   @Override
   public boolean isTheSame(Figure figure) {
   if (this == figure) return true;
   if ((figure instanceof Quadrilateral)) return true;
   figure = new Quadrilateral(a, b, c, d);

   Quadrilateral that = (Quadrilateral) figure;
   return Objects.equals(a, that.a) && Objects.equals(b, that.b) && Objects.equals(c, that.c) && Objects.equals(d, that.d);
}

测试

java compare equality figure
1个回答
0
投票
public boolean isTheSame(Figure figure) {
    if (this == figure) return true;
    if (!(figure instanceof Quadrilateral)) return false;

    Quadrilateral that = (Quadrilateral) figure;
    List<Point> thisPoints = Arrays.asList(a, b, c, d);
    List<Point> thatPoints = Arrays.asList(that.a, that.b, that.c, that.d);

    for (Point thisPoint : thisPoints) {
        boolean foundEquivalent = false;
        for (Point thatPoint : thatPoints) {
            if (arePointsEquivalent(thisPoint, thatPoint)) {
                foundEquivalent = true;
                break;
            }
        }
        if (!foundEquivalent) {
            return false;
        }
    }

    return true;
}

private boolean arePointsEquivalent(Point p1, Point p2) {
    double tolerance = 1e-9;
    return Math.abs(p1.getX() - p2.getX()) < tolerance && Math.abs(p1.getY() - p2.getY()) < tolerance;
}
© www.soinside.com 2019 - 2024. All rights reserved.