B
B
Biaci_Anj2021-11-29 16:19:47
Java
Biaci_Anj, 2021-11-29 16:19:47

How to implement a quadrilateral check for degeneracy in Java?

There is a Point, each point has x and y. The first thought is to check if the points lie on the same line, but there are difficulties, I do not quite understand how to do this.

public Quadrilateral(Point a, Point b, Point c, Point d) {
// Надо проверить на вырожденность 
}


Here is a method for finding segment intersections.

private Point intersectPoint(Point start1, Point end1, Point start2, Point end2) {
        double x1 = start1.getX();
        double y1 = start1.getY();
        double x2 = end1.getX();
        double y2 = end1.getY();

        double x3 = start2.getX();
        double y3 = start2.getY();
        double x4 = end2.getX();
        double y4 = end2.getY();

        double t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
        double u = ((x1 - x3) * (y1 - y2) - (y1 - y3) * (x1 - x2)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
        double x = x1 + t * (x2 - x1);
        double y = y1 + t * (y2 - y1);
        if (0.0 <= t && t <= 1.0 && 0.0 <= u && u <= 1.0) {
            return new Point(x, y);
        }
        return null;

    }

I will be glad to help

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Cheremisin, 2021-11-29
@leahch

Well, if at least one nodal point coincides, the rectangle is degenerate!

Arraylist points = new Arraylist();
points.add(p1);
..
points.add(p2);

Iterator<String> iter = points.iterator();
while(iter.hasNext()){
 Point p = iter.next());
 iter.remove();
 if(points.contains(p)) return true; // здесь определяем, что точка равна еще какой-то точке.
}

S
Sergey Vodakov, 2021-11-29
@WaterSmith

To determine whether four points lie on the same line, you need to check whether the lines drawn through each pair of points coincide. Details at the link:
mathprofi.ru/zadachi_s_pryamoi_na_ploskosti.html

R
Ruslan., 2021-12-01
@LaRN

A quadrilateral is degenerate if its area is zero.
For a quadrilateral, this will be true if all vertices lie on the same line.
To find out that the vertices lie on one straight line, you can use the scalar product of the vectors
built on the vertices. The scalar product of two parallel vectors is equal to one, which means in our case that the vertices lie on the same line.
For example:
vAB.x=bX-aX
vAB.y=bY-aY
vAC.x=cX-aX
vAC.y=cY-aY
vAD.x=dX-aX
vAD.y=dY-aY
Now we find the scalar product of vectors vAB*vAC and vAB*vAD, if both are equal to one, then the quadrilateral is degenerate.
You can find the scalar product of vector coordinates like this:
s(v1, v2) = (v1.x*v2.x+v1.y*v2.y)/(sqrt(v1.x*v1.x+v1.y*v1 .y)*sqrt(v2.x*v2.x+v2.y*v2.y))
Or the second option, you can go from the straight line equation:
(bx-ax)/(by-ay)=(cx-ax) /(cy-ay)=(dx-ax)/(dy-ay)=t
and here, to get away from division, you can rewrite the equation like this:
(bx-ax)*(cy-ay) =(cx-ax)* (by-ay)
(bx-ax)*(dy-ay) =(dx-ax)*(by-ay)
If both equalities hold, then the quadrilateral is degenerate

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question