0

I need for my java-program a function that checks for polygon-collision, but the algorithms (for point-in-polygon) I tried were not correct for my needs, the degenerative cases are a problem for me.

This is what i try to reach with my program: I have 2 polygons and want to put them nearest possible together. I want to place them on their vertices and rotate them along the edge to fit optimal. Therefor I need a collision-detection, if they intersect or not.

My biggest problem is that those polygon-edges could be on the same point. The researched algorithms decide if it is in polygon a or b (mostly with y-value).

What I use

  • Polygon with double coordinates for x and y
  • standard java
  • no external librarys

My required rules:

  • polygons can have same edge and same vertices (can be on same boundary, but not complete polygon overlay)
  • the edges should not be allowed to intersect
  • it is not allowed, that one polygon is completly surrounded by another polygon (a hole).
  • (an optional very small epsilon in algorithm would be good, because rotating with double is not very exact)

I tried too the internal classes like Path2D.Double() with contains too without success to this problem. The last algorithm (of about minimum of 8) i tried was this: wiki.cizmar.org/doku.php?id=physics:point-in-polygon_problem_with_simulation_of_simplicity

This is C Code of the linked algorithm (last one I tried)

int i, j, c = 0;
  for (i = 0, j = number_of_vertices-1; i < number_of_vertices; j = i++) {
    if ( ((vertices[i].y>p.y) != (vertices[j].y>p.y)) &&
     (p.x < (vertices[j].x-vertices[i].x) * (p.y-vertices[i].y) / (vertices[j].y-vertices[i].y) + vertices[i].x) )
       c = !c;
  }
  return c;

My adapted JAVA code (Punkt=Point, Form.getCoords = List of Coordinates with x,y)

private boolean testPointInsidePolygon3c(Punkt p, Form f){
    int number_of_vertices = f.getCoords().size();
    int i, j = 0;
    boolean odd = false;
    for (i = 0, j = number_of_vertices-1; i < number_of_vertices; j = i++) {
        if ( ((f.getCoords().get(i).getY() >p.getY()) != (f.getCoords().get(j).getY() >p.getY())) &&
            (   p.getX() < (f.getCoords().get(j).getX() -f.getCoords().get(i).getX())
                * (p.getY() -f.getCoords().get(i).getY())
                / (f.getCoords().get(j).getY() -f.getCoords().get(i).getY())
                + f.getCoords().get(i).getX())
            ){
            odd = !odd;
        }
    }
    return odd;
}

To show that problem: here are pictures with 2 polygons. the blue vertices are the troublesomes. Problem Example #1 example from another source

I hope you got some ideas, links, algorithm or anything for me. i got stucked too long with that problem ;-)

E.B.
  • 1

1 Answers1

0

What a pity - i could not do a complete correct algorithm, that solves my problem.

That is why I now use the JTS-Library! With overlaps and covers/within i got everything correct in my test-cases.

E.B.
  • 1