Find Centroid of Quadrilateral

1.2k Views Asked by At

Help me please with how to find Centroid of Quadrilateral? I have this constructor:

 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;
    }

Then I find four centroids of triangles inside quadrilateral:

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);
    }

Then I need to find two diagonals between "leftUp" - "rightDown" and "leftDown"-"rightUp" centroids. But how can I find position of centroid to make correct pairs of them? And how to find then centroid of all quadrilateral?

1

There are 1 best solutions below

0
On

Good day. After finding centers of triangles you need to find intersection of lines which made with (center1,center2) and (center3,center4). So point of intersection is Centroid of Quadrilateral

  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));

    }

Segment:

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);

    }

}