Javascript-Voronoi icon indicating copy to clipboard operation
Javascript-Voronoi copied to clipboard

Feature request : get all adjacent vertices for a given vertex

Open lebesnec opened this issue 10 years ago • 5 comments

I'd like to access all the adjacent vertices of a given vertex. Something like the "getNeighborIds()" method of cells would be great. May be there is already a way to do this that I missed ?

Thanks for this great library !

lebesnec avatar Jan 07 '14 14:01 lebesnec

When you say "adjacent vertices", do you mean the vertices linked through the same edges?

gorhill avatar Jan 08 '14 13:01 gorhill

yes, all the vertices linked through the edges that end/start from this vertex. Like : http://en.wikipedia.org/wiki/Adjacency_list

lebesnec avatar Jan 08 '14 14:01 lebesnec

That would be possible with a somewhat complicated algortihm, using the fact that from a cell's halfedge one can get the other cell sharing the halfedge, and from there finding which other halfedge shares the vertex, etc. until all cells sharing the vertex have been processed.

Theoritically I could put code in the core algorithm in order for each vertex to keep a list of back references to all edges which refer to the vertex, but thinking of the impact on performance I would rather not do that.

gorhill avatar Jan 08 '14 14:01 gorhill

@gorhill you could make it a function on the Edge class. If you want to loop over all edges it's required to have this sort of functionality. I'm doing something similar to what you suggested but it's not working because I can't look up the edges by vertex reliably -- some times they're not found

eranimo avatar Nov 02 '16 04:11 eranimo

@eranimo , this request is pretty old, but as ticket is still open, I document it for documentation's sake. I wrapped this library in a higher level one using a cell class, where I created links to previous and next edges, this greatly facilitates circular drawing algorithms such as SVG paths creation I import the cells in a new class

    from_rhill_cell(c){
        this.seed = c.site
        this.edges = []
        for(let i=0;i<c.halfedges.length;i++){
            const he = c.halfedges[i]
            let [v1,v2] = ccw_vertices(he)
            let edge = {v1:v1,v2:v2}
            edge.l = he_length(he)
            edge.c = center(edge.v1,edge.v2)
            edge.a = c.halfedges[i].angle
            this.edges.push(edge)
        }
    }

then I create links to neighbors, on edge level though, not on vertex level, but if you need a neighboring vertex you simply then need to loop through edges, take prev next edge and using v1 only

    add_prev_next(edges){
        for(let i=0;i<edges.length;i++){
            let edge = edges[i]
            edge.prev = (i==0)?edges.slice(-1)[0]:edges[i-1]
            edge.next = (i==edges.length-1)?edges[0]:edges[i+1]
        }
    }

more here in case of interest of the details in the full integration

wassfila avatar Apr 12 '20 07:04 wassfila