int main(){ char** word=cz(MAX); count* ct=sz(MAX); int i=0; for (i=0;i<MAX;++i){ word[i]=NULL; ct[i].pv=0; } create(word,ct); deal(word,ct); HeapSort(ct,MAX); sortput(word,ct); return 0; }
/* * do iterative refinement instead of iterative simplification * works well for small apoly. * * 1. find farthest point(1) from the center of points. * 2. find farthest point(2) from point(1). * 3. we now have a (degenerate) polygon of two points * 4. repeatedly add the point with maximum distance to its current polygon segment, * but the generated segments must not intersect any of the old segments */ int fitpoly(int *poly, int apoly, short *pt, int npt, int dstthr) { int i, j, npoly, pi, ni; int dst2, maxdst2, maxi; short cntr[2], *cpt = cntr; int polymaxi[apoly]; int polymaxdst2[apoly]; if(npt < 2) return -1; /* * in theory, any point should do. * using the top-left corner instead of centroid * made no visible difference. */ cpt[0] = 0; cpt[1] = 0; npoly = 0; for(j = 0; j < 2; j++){ maxi = -1; maxdst2 = -1; for(i = 0; i < npt; i++){ dst2 = ptptdst2(pt + 2*i, cpt); if(dst2 > maxdst2){ maxdst2 = dst2; maxi = i; } } if(maxdst2 < dstthr*dstthr || npoly+1 >= apoly) goto out; sortput(poly, npoly, maxi); npoly++; cpt = pt + 2*maxi; } findmaxdst2(pt, npt, poly[1], poly[0], polymaxi+1, polymaxdst2+1); findmaxdst2(pt, npt, poly[0], poly[1], polymaxi+0, polymaxdst2+0); while(npoly < apoly){ int maxi2 = -1; int maxd2 = -1; for(i = 0; i < npoly; i++){ if(polymaxdst2[i] > maxd2){ maxd2 = polymaxdst2[i]; maxi2 = polymaxi[i]; } } maxi = maxi2; maxdst2 = maxd2; if(maxdst2 == -1) return -1; if(maxdst2 < dstthr*dstthr) goto out; i = sortput(poly, npoly, maxi); memmove(polymaxi+i+1, polymaxi+i, (npoly-i) * sizeof polymaxi[0]); memmove(polymaxdst2+i+1, polymaxdst2+i, (npoly-i) * sizeof polymaxdst2[0]); npoly++; pi = (i+npoly-1); pi = pi < npoly ? pi : pi-npoly; ni = i+1; ni = ni < npoly ? ni : ni-npoly; findmaxdst2(pt, npt, poly[pi], poly[i], polymaxi+pi, polymaxdst2+pi); findmaxdst2(pt, npt, poly[i], poly[ni], polymaxi+i, polymaxdst2+i); } out: return npoly; }