提问人:Andrii 提问时间:7/14/2023 最后编辑:M. JustinAndrii 更新时间:11/16/2023 访问量:141
数字。额外挑战
Figures. Extra Challenge
问:
请帮助创建一个方法,如描述:请使三角形、四边形、圆形类扩展图形抽象类。public boolean isTheSame(Figure figure){}
在三角形、四边形、圆形中实现方法:
具有以下参数的构造函数:
- 三角形 – 三个顶点(点)作为参数。
- 四边形 – 四个顶点(点)作为参数。
- 圆 ― 中心的点和半径的双倍值。
确保数字不会退化。
它们都必须具有非零面积。
四边形也必须是凸的。
如果数字不好,则抛出 IllegalArgumentException。
注: 非退化凸四边形由其对角线分为四个非退化三角形。
注意:重复计算并不完全准确,在适用的情况下使用误差增量。
public Point centroid() 返回图形的质心。
质心是指平纹的质心,而不是重心。
换句话说,它应该是“面积质心”。
public boolean isTheSame(Figure figure)
仅两个图形被视为相同:
如果它们具有相同的类型
并且它们重合(例如具有相同的顶点)
注意:顶点的顺序不能相同。
注意:重复计算并不完全准确,在适用的情况下使用误差增量。
好奇的注意:它几乎像 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);
}
}
我尝试使用 and 方法进行比较,但没有任何效果。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);
}
测试
答:
-1赞
DerekAyala
8/17/2023
#1
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;
}
评论
0赞
Volodymyr Kulesha
11/16/2023
#2
@Override
public boolean isTheSame(Figure figure) {
if(figure.getClass() != this.getClass()) return false;
Quadrilateral quadrilateral = (Quadrilateral) figure;
Point[] thisVertices = {a, b, c, d};
Point[] otherVertices = {quadrilateral.a, quadrilateral.b, quadrilateral.c, quadrilateral.d};
int isMatched = 0;
for (int i = 0; i< thisVertices.length; i++){
for (int j = 0; j < otherVertices.length; j++){
if (thisVertices[i].equals(otherVertices[j])){
isMatched++;
}
}
}
return isMatched == 4;
}
此外,您还需要修改 Point 类
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
double ERROR_DELTA = 0.000001d;
return Math.abs(x - point.x) < ERROR_DELTA && Math.abs(y - point.y) < ERROR_DELTA;
}
上一个:数字。额外挑战
评论