Answer the question
In order to leave comments, you need to log in
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) {
// Надо проверить на вырожденность
}
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;
}
Answer the question
In order to leave comments, you need to log in
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; // здесь определяем, что точка равна еще какой-то точке.
}
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
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 questionAsk a Question
731 491 924 answers to any question