gorhill / Javascript-Voronoi

A Javascript implementation of Fortune's algorithm to compute Voronoi cells
http://www.raymondhill.net/voronoi/
Other
1.03k stars 166 forks source link

Get Cell vertices ordered clockwise or counter clockwise #26

Closed robincafolla closed 9 years ago

robincafolla commented 9 years ago

Hi there,

Nice library.

I'm trying to get a cell's vertices in clockwise or counter-clockwise order. I've tried

var corners = [], i;                              
for( i = cell.halfedges.length - 1; i > -1; i-- ){
    corners.push(                                 
        cell.halfedges[i].getStartpoint()         
    );                                            
}                                                 

but it doesn't seem to order them properly ( it creates duplicate ). Is there a way to do this?

gorhill commented 9 years ago

They are always ordered counterclockwise. I use this code in one of the demo to render a polygon on a canvas from a cell:

var halfedges = cell.halfedges,
var nHalfedges = halfedges.length;
var v = halfedges[0].getStartpoint();
ctx.beginPath();
ctx.moveTo(v.x,v.y);
for ( var iHalfedge=0; iHalfedge < nHalfedges; iHalfedge++ ) {
    v = halfedges[iHalfedge].getEndpoint();
    ctx.lineTo(v.x,v.y);
}
ctx.fillStyle = '#faa';
ctx.fill();

It wouldn't work if they were not ordered.

robincafolla commented 9 years ago

Cheers, that worked, thanks for the quick response.