function find_vertex(x, y, i,id,dx,dy,d,ilo,ihi,xmin,xhi) { # Returns the ID of the vertex with coordinates (x,y) # within a tolerance eps. # Simplify coordinates: x = int(x - xbase); y = int(y - ybase); # Candidate vertices have vx in the range [x-eps .. x+eps]: xmin = x - eps; xmax = x + eps; # Binary search for first i such that vx[i] >= xmin: ilo = 0; ihi = nv-1; while (ilo <= ihi) { # At this point ilo >= 0, vx[ilo-1] < xmin, and vx[ihi+1] >= xmin i = int((ilo + ihi)/2); if (vx[i] < xmin) { ilo = i + 1; } else { ihi = i - 1; } } i = ilo; if (((i < nv) && (vx[i] < xmin)) || ((i >= 0) && (vx[i-1] >= xmin))) { prog_error("binary search error"); } # Sequential search among points in range [xmin..xmax]: while ((i < nv) && (vx[i] <= xmax)) { dx = x - vx[i]; dx = ( dx < 0 ? -dx : dx); dy = y - vy[i]; dy = ( dy < 0 ? -dy : dy); d = dx + dy; if (d <= eps) { if ((dx != 0) || (dy != 0)) { data_warning(("vertex position noise = (" dx "," dy ")")); } return vid[i]; } i++; } # New vertex, assign its ID: id = nv; # Point not found, must add it to the table. # Insertion sort for x: i = nv; while ((i > 0) && (vx[i-1] > x)) { vx[i] = vx[i-1]; vy[i] = vy[i-1]; vid[i] = vid[i-1]; i--; } vx[i] = x; vy[i] = y; vid[i] = id; nv++; vin[id] = 0; vot[id] = 0; return id; }