/** * Finds the node nearest to the point with specified coordinates. */ private Node findNodeNearest(double x, double y) { // First, find the nearest node among the sampled nodes. _nmin = _nroot; _dmin = distanceSquared(_nmin,x,y); for (Node n:_sampledNodes) { double d = distanceSquared(n,x,y); if (d<_dmin) { _dmin = d; _nmin = n; } } // Then, recursively search node nabors to find the nearest node. // Note that we clear the node marks once, so that we never compute // the distance to a node twice, but that we clear the tri marks // *inside* the loop, to ensure that we visit all tri nabors of the // current nearest node. clearNodeMarks(); double dmin; do { clearTriMarks(); dmin = _dmin; findNodeNaborNearest(x,y,_nmin,_nmin._tri); } while (_dmin<dmin); return _nmin; }
/** * If the point with specified (x,y) coordinates is nearer to the * specified node than the nearest node found so far, then replaces * the nearest node (and the node root) with the specified node. * Does nothing if the specified node is marked. * (Currently unused.) */ private void updateNodeNearest(double x, double y, Node n) { if (!isMarked(n)) { mark(n); double d = distanceSquared(n,x,y); if (d<_dmin) { _dmin = d; _nmin = n; _nroot = n; } } }
/** * Locates a point. */ private PointLocation locatePoint(double x, double y) { // If no tris yet, search the node list for an exact match. // Here, we use unperturbed node coordinates. if (_troot==null) { if (_nroot!=null) { Node node = _nroot; do { if (x==node.x() && y==node.y()) return new PointLocation(node); node = node._next; } while (node!=_nroot); } return new PointLocation(null,false); } // Otherwise, find a good tri in which to begin the recursive search. Node nmin = _nroot; double dmin = distanceSquared(nmin,x,y); for (Node n:_sampledNodes) { double d = distanceSquared(n,x,y); if (d<dmin) { dmin = d; nmin = n; } } Tri tri = nmin._tri; return locatePoint(tri,x,y); }
private void findNodeNaborNearest( double x, double y, Node node, Node na, Node nb, Tri ta, Tri tb) { if (!isMarked(na)) { mark(na); double da = distanceSquared(na,x,y); if (da<_dmin) { _dmin = da; _nmin = na; } } if (!isMarked(nb)) { mark(nb); double db = distanceSquared(nb,x,y); if (db<_dmin) { _dmin = db; _nmin = nb; } } if (ta!=null && !isMarked(ta)) findNodeNaborNearest(x,y,node,ta); if (tb!=null && !isMarked(tb)) findNodeNaborNearest(x,y,node,tb); }