请帮助我如何找到四边形的质心?我有一个构造函数:
public Quadrilateral(Point a, Point b, Point c, Point d) {
if (a == null || b == null || c == null || d == null) {
throw new IllegalArgumentException();
}
if (collinear(a, b, c)) {
throw new IllegalArgumentException();
}
double[][] points = { { a.getX(), a.getY() }, { b.getX(), b.getY() },
{ c.getX(), c.getY() }, { d.getX(), d.getY() } };
if (!isConvex(points)) {
throw new IllegalArgumentException();
}
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
然后我在四边形中找到了四个三角形的质心:
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 null;
}
private Point centroidTriangle(Point a, Point b, Point c) {
double xx = (a.getX() + b.getX() + c.getX()) / 3;
double xy = (a.getY() + b.getY() + c.getY()) / 3;
return new Point(xx, xy);
}
那么我需要找到"0"之间的两条对角线;leftUp"-"rightDown";以及";leftDown"-"rightUp";质心。但我怎样才能找到质心的位置,使它们正确配对呢?那个么如何找到所有四边形的质心呢?
您好。找到三角形的中心后,你需要找到由(center1,center2(和(center3,center4(组成的线的交点。所以交点是四边形的质心
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));
}
细分市场:
import java.util.Objects;
import static java.lang.Math.sqrt;
import static java.lang.StrictMath.pow;
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;
}
double length() {
return sqrt(pow(end.getX() - start.getX(), 2) + pow(end.getY() - start.getY(), 2));
}
Point middle() {
return new Point((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2);
}
Point intersection(Segment another) {
double k1 = (end.getY() - start.getY()) / (end.getX() - start.getX());
double k2 = (another.end.getY() - another.start.getY()) / (another.end.getX() - another.start.getX());
if (k1 == k2) 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) / (k1 - k2);
double y = (k1 * b2 - k2 * b1) / (k1 - k2);
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);
}
}