Asked By : Sioma Dukh
Answered By : jcb
I saw the code base you are using in your other posting on DCELs, so here is the code version: First you will need to augment your vertex data structure to have fields for x and y:
struct vertex { struct half_edge *rep; /* rep->tail == this */ double x, y; }; bool polygon_isleftof(half_edge *he) { return he->next->tail->y > he->tail->y; }
By the way, when dealing with DCELs I have found that for many applications it is best to work with half-edges directly rather than vertices (in other words, if you want to pass a vertex to a function, just pass one of the half-edges for which it is a tail/source). That way you get two pieces of information: 1.) the vertex you are interested in and 2.) the face your are interested in. So in the code above my polygon_isleftof tells you whether the vertex which is the tail of he is to the right of the face which is incident to he. If I instead passed a vertex and a face the code would be much slower:
bool polygon_isleftof_slow(vertex *v, face *f) { //first we need to find a half-edge incident both f and v half_edge *walk = v->rep; //loop over all half-edges with tail equal to v: do { if (walk->left == f) break; //exit the loop if we find a half-edge incident f walk = walk->twin->next; } while (walk != v->rep); //If we didn't find a half-edge incident f and v, then return false: if (walk->left != f) { return false; } else { return walk->next->tail->y > walk->tail->y; } }
Notice that in the first case, polygon_isleftof is $O(1)$ whereas in the second case polygon_isleftof_slow is $O(n)$. And since you are probably traversing the polygon using half-edges anyway, there is no reason not to just operate on half-edges and only access the vertices as needed in functions.
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/18092 Ask a Question Download Related Notes/Documents