C++ 生成 voronoi 图 & C++生成泰森多边形图形.md

Edit### C++ 生成 voronoi 图 & C++生成泰森多边形图形.md


1. 功能

生成voronoi图的一个类

2. 代码

VoronoiDiagramGenerator.h

  1. #pragma once //Microsoft Visual Studio 2015 Enterprise #ifndef VORONOI_DIAGRAM_GENERATOR #define VORONOI_DIAGRAM_GENERATOR #include <math.h> #include <stdlib.h> #include <string.h> #ifndef NULL #define NULL 0 #endif #define DELETED -2 #define le 0 #define re 1 structstructstructstructstructstructstructintstructfloat// structure used both for sites and for vertices structstructintintstructfloatstruct2struct2intstructfloatstructstructstructstructintcharstructfloatstructclasspublicbool generateVoronoi(float *xValues, float *yValues, int numPoints, float minX, float maxX, float minY, float maxY, float minDist = 0)void resetIterator() bool getNext(float& x1, float& y1, float& x2, float& y2) if0returnfalsereturntrueprivatevoid cleanup()void cleanupEdges()char *getfree(struct Freelist *fl)int PQempty()structstruct Halfedge *HEcreate(struct Edge *e, int pm)struct Point PQ_min()struct Halfedge *PQextractmin()void freeinit(struct Freelist *fl, int size)void makefree(struct Freenode *curr, struct Freelist *fl)void geominit()void plotinit()bool voronoi(int triangulate)void ref(struct Site *v)void deref(struct Site *v)void endpoint(struct Edge *e, int lr, struct Site * s)void ELdelete(struct Halfedge *he)struct Halfedge *ELleftbnd(struct Point *p)struct Halfedge *ELright(struct Halfedge *he)void makevertex(struct Site *v)void out_triple(struct Site *s1, struct Site *s2, struct Site * s3)void PQinsert(struct Halfedge *he, struct Site * v, float offset)void PQdelete(struct Halfedge *he)bool ELinitialize()void ELinsert(struct Halfedge *lb, struct Halfedge *newHe)struct Halfedge * ELgethash(int b)struct Halfedge *ELleft(struct Halfedge *he)struct Site *leftreg(struct Halfedge *he)void out_site(struct Site *s)bool PQinitialize()int PQbucket(struct Halfedge *he)void clip_line(struct Edge *e)char *myalloc(unsigned n)int right_of(struct Halfedge *el, struct Point *p)struct Site *rightreg(struct Halfedge *he)struct Edge *bisect(struct Site *s1, struct Site *s2)float dist(struct Site *s, struct Site *t)struct Site *intersect(struct Halfedge *el1, struct Halfedge *el2, struct Point *p = 0)void out_bisector(struct Edge *e)void out_ep(struct Edge *e)void out_vertex(struct Site *v)struct Site *nextone()void pushGraphEdge(float x1, float y1, float x2, float y2)void openpl()void line(float x1, float y1, float x2, float y2)void circle(float x, float y, float radius)void range(float minX, float minY, float maxX, float maxY)structstructintintfloatstructintintintintstructstructintstructintstructintintintfloatintfloatfloatint scomp(const void *p1, const void *p2)#endif

VoronoiDiagramGenerator.cpp

  1. "VoronoiDiagramGenerator.h"0000000if0boolfloatfloatintfloatfloatfloatfloatfloatint0010sizeofstruct Sitesizeofif0returnfalse0000for00ifelseififelseif//printf("\n%f %f\n",xValues[i],yValues[i]); sizeof0float0ifif0returntrueboolintsizeof2struct Halfedgesizeofif0returnfalsefor01struct Halfedgestruct Edge0struct Edge0struct Halfedgestruct Halfedge01returntruestruct Halfedgestruct Edgeintstruct Halfedgestruct Halfedgestruct Halfedgestruct Site0returnstruct Halfedge/* Get entry from hash table, pruning any deleted nodes */struct Halfedgeintstruct Halfedgeif0returnstruct Halfedgeifstruct Halfedgestruct Edgereturn/* Hash table points to deleted half edge. Patch as necessary. */struct Halfedgeif10returnstruct Halfedgestruct Halfedgestruct Pointintstruct Halfedge/* Use hash table to get close to desired halfedge */int//use the hash function to find the place in the hash map that this HalfEdge should be if00//make sure that the bucket position in within the range of the hash array if1ifstruct Halfedge//if the HE isn't found, search backwards and forwards in the hash map for the first non-null entry for111ifstruct Halfedgebreakifstruct Halfedgebreak1/* Now search linear list of halfedges for the correct one */ifdowhile//keep going right on the list until either the end is reached, or you find the 1st edge which the point //isn't to the right of else//if the point is to the left of the HalfEdge, then search left for the HE just to the left of the point dowhile/* Update hash table and reference counts */if01ifstruct Halfedge11return/* This delete routine can't reclaim node, since pointers from hash table may be present. */struct Halfedgestruct Edgestruct Halfedgestruct Halfedgereturnstruct Halfedgestruct Halfedgereturnstruct Sitestruct Halfedgeifstruct Edgereturnreturnstruct Sitestruct Halfedgeifstruct Edge//if this halfedge has no edge, return the bottom site (whatever that is) return//if the ELpm field is zero, return the site 0 that this edge bisects, otherwise return site number 1 returnfloatsizeof00float4intstruct Edgestruct Sitefloatstruct Edgestruct Edge0//store the sites that this edge is bisecting 1refref0struct Site//to begin with, there are no endpoints on the bisector - it goes to infinity 1struct Site//get the difference in x dist between the sites 0//make sure that the difference in positive 0float0.5//get the slope of the line if1.0//set formula of line, with x fixed to 1 else1.0//set formula of line, with y fixed to 1 //printf("\nbisect(%d) ((%f,%f) and (%f,%f)",nedges,s1->coord.x,s1->coord.y,s2->coord.x,s2->coord.y); 1return//create a new site where the HalfEdges el1 and el2 intersect - note that the Point in the argument list is not used, don't know why it's there struct Sitestruct Halfedgestruct Halfedgestruct Pointstruct Halfedgefloatintstruct Siteifstruct Edgestruct Edgereturnstruct Site//if the two edges bisect the same parent, return null if11returnstruct Siteif1.0e-101.0e-10returnstruct Siteif111111else1ifreturnstruct Site//create a new site at the point of intersection - this is a new vector event waiting to happen struct Site0return/* returns 1 if p is to right of halfedge e */intstruct Halfedgestruct Pointstruct Edgestruct Siteintfloat1ifreturn1ifreturn0if1.00if0.00.0elseif0.0if1if01.02.0if0.0else/*e->b==1.0 */returnstruct Edgeintstruct Siterefifstruct Sitereturnfloatstruct Sitestruct Sitefloatreturnfloatstruct Site1struct Site1if0refstruct Site1//push the HalfEdge into the ordered linked list of vertices struct Halfedgestruct Sitefloatstruct Halfedgereffloatwhilestruct Halfedge1//remove the HalfEdge from the list of vertices struct Halfedgestruct Halfedgeifstruct Sitewhile1struct Siteintstruct Halfedgeintintif00if1ifreturnintreturn0struct Pointstruct Pointwhilestruct Halfedge1returnstruct Halfedgestruct Halfedge1returnboolint004struct Halfedgesizeofif0returnfalsefor01struct Halfedgereturntruestruct Freelistintstruct Freenodecharstruct Freelistintstruct Freenodeifstruct Freenodestruct Freenodeif0return00for01struct Freenodecharreturncharstruct Freenodestruct Freelistif0000while00if000000while000floatfloatfloatfloatcharchar0charreturn/* for those who don't have Cherry's plot *//* #include <plot.h> */floatfloatfloatfloatfloatfloatfloatfloatfloatfloatfloatstruct Edgestruct Edgestruct Sitestruct Siteifstruct Sitestruct Sitestruct Sitefloatfloat1.1float2.0float2.0float2.0float2.0float350.0struct Edgestruct Sitefloat000000101//if the distance between the two points this line was created from is less than //the square root of 2, then ignore it ifreturnif1.00.010else01if1.0ifstruct Siteif//printf("\nClipped (1) y1 = %f to %f",y1,pymax); //return; ifstruct Siteif//printf("\nClipped (2) y2 = %f to %f",y2,pymin); //return; if//printf("\nClipLine jumping out(3), x1 = %f, pxmin = %f, pxmax = %f",x1,pxmin,pxmax); returnififififelseifstruct Siteif//printf("\nClipped (3) x1 = %f to %f",x1,pxmin); //return; ifstruct Siteif//printf("\nClipped (4) x2 = %f to %f",x2,pxmin); //return; if//printf("\nClipLine jumping out(6), y1 = %f, pymin = %f, pymax = %f",y2,pymin,pymax); returnifififif//printf("\nPushing line (%f,%f,%f,%f)",x1,y1,x2,y2); /* implicit parameters: nsites, sqrt_nsites, xmin, xmax, ymin, ymax, deltax, deltay (can all be estimates). Performance suffers if they are wrong; better to make nsites, deltax, and deltay too big than too small. (?) */boolintstruct Sitestruct Sitestruct Pointintstruct Halfedgestruct Edgeboolifreturnfalsewhile1if//if the lowest site has a smaller y value than the lowest vector intersection, process the site //otherwise process the vector intersection ifstruct Site/* new site is smallest - this is a site event*///output the site //get the first HalfEdge to the LEFT of the new site //get the first HalfEdge to the RIGHT of the new site //if this halfedge has no edge, , bot = bottom site (whatever that is) //create a new edge that bisects //create a new HalfEdge, setting its ELpm field to 0 //insert this new bisector edge between the left and right vectors in a linked list ifstruct Site//if the new bisector intersects with the left edge, remove the left edge's vertex, and put in the new one //create a new HalfEdge, setting its ELpm field to 1 //insert the new HE to the right of the original bisector earlier in the IF stmt ifstruct Site//if this new bisector intersects with the //push the HE into the ordered linked list of vertices elseif/* intersection is smallest - this is a vector event *///pop the HalfEdge with the lowest vector off the ordered list of vectors //get the HalfEdge to the left of the above HE //get the HalfEdge to the right of the above HE //get the HalfEdge to the right of the HE to the right of the lowest HE //get the Site to the left of the left HE which it bisects //get the Site to the right of the right HE which it bisects //output the triple of sites, stating that a circle goes through them //get the vertex that caused this event //set the vertex number - couldn't do this earlier since we didn't know when it would be processed //set the endpoint of the left HalfEdge to be this vector //set the endpoint of the right HalfEdge to be this vector //mark the lowest HE for deletion - can't delete yet because there might be pointers to it in Hash Map //remove all vertex events to do with the right HE //mark the right HE for deletion - can't delete yet because there might be pointers to it in Hash Map //set the pm variable to zero if//if the site to the left of the event is higher than the Site //to the right of it, then swap them and set the 'pm' variable to 1 //create an Edge (or line) that is between the two Sites. This creates //the formula of the line, and assigns a line number to it //create a HE from the Edge 'e', and make it point to that edge with its ELedge field //insert the new bisector to the right of the left HE //set one endpoint to the new edge to be the vector point 'v'. //If the site to the left of this bisector is higher than the right //Site, then this endpoint is put in position 0; otherwise in pos 1 //delete the vector 'v' //if left HE and the new bisector don't intersect, then delete the left HE, and reinsert it ifstruct Site//if right HE and the new bisector don't intersect, then reinsert it ifstruct Siteelsebreakforreturntrueintconstconststruct Pointifreturn1ifreturn1ifreturn1ifreturn1return0/* return a single in-storage site */struct Sitestruct Siteif1returnelsereturnstruct Site

3. 用法示例

  1. #include <stdio.h> #include <search.h> #include <malloc.h> #include "VoronoiDiagramGenerator.h" int main(int argc, char **argv) float4-22-17422float4-93113-5long4-100100-1001003floatprintf"\n-------------------------------\n"whileprintf"GOT Line (%f,%f)->(%f,%f)\n""pause"return0