I'm currently trying to get the clipped cells from a Polygon-Voronoi-Intersection.
Here is what I've got so far:
I have a polygon and computed some points in it to calculate a voronoi diagram and the red lines on the figure below are the voronoi edges. Then I used an algorithm to get the corner points from every cell and now I need to get the corners in the right direction (clockwise) to generate the cell polygon.
Found Corners for one cell
First I was using this method:
private List<Vector> sortClockwise(List<Vector> points)
{
points = points.OrderBy(x => Math.Atan2(x.X, x.Y)).ToList();
return points;
}
but in some special concave polygons this doesn't work and the right order gets mixed up.
Does anyone have a suggetion or hint how this could be done the most simplest way? Consider that the polygon points are in the right order already and the voronoi corners are mixed up and need to get sorted to the polygon points.
My idea:
- Find first polygon point in cell corners
- go along polygon direction and look if point of voronoi vertices is on that line.
- if yes: get endpoint of found voronoi edge and look for shared voronoi edges.
- if shared edges found, always take the most right one
- do until you reach fist point
Is that the only way I could do that?
EDIT - UPDATE
Okay I have some sort of half answer now.
As I said, I have all the vertices which belong to one of the voronoi's cells but the order is still messed up so I thought I could sort them by angle from the centroid like so:
private List<Vector> sortClockwiseBySentroid(List<Vector> points, Vector center)
{
points = points.OrderBy(x => Math.Atan2(x.X - center.X, x.Y - center.Y)).ToList();
return points;
}
But this doesn't always work. Here are the examples when its working and when not. The problem is that on concave edges the angle from the centorid to the corner is sometimes smaller than the one I really need. Any suggestion on how to fix this?
Here its working
Here its not working...