public Point setNextOpen(Point point, int pixelLength, int pixelHeight, int pixelAwayLimit){  Debug.message("declutterdetail", "DeclutterMatrix: Trying to find an open space.");  if (needToRecreate) create();  boolean set = false;   int windex = point.x/x_pix_interval; int hindex = point.y/y_pix_interval;    int xpoint; int ypoint;    Point ret = null;  Point testPoint = new Point();        int round = 0; int pos, i;   indexes.setFromPixels(point.x, point.y, pixelLength, pixelHeight);   if(matrix == null || !indexes.withinMatrix){ return point; }    ret = isAreaClearR(windex, hindex, testPoint); int round_limit; if (pixelAwayLimit < 0){ round_limit = Math.abs(maxy/2 - indexes.yStart) + (maxy / 2); } else { round_limit = pixelAwayLimit; }    while (ret == null) {  Debug.message("declutterdetail", "DeclutterMatrix: round " + round + "\n"); round++; for (pos = 0; (dcPos[pos].position != DCP_MIDDLE) && (ret == null); pos++){       xpoint = windex+(round*dcPos[pos].ewindex); ypoint = hindex+(round*dcPos[pos].nsindex);    if(xpoint <= maxx && ypoint <= maxy && xpoint >=0 && ypoint >=0){          if(dcPos[pos].direction == DCD_NS){ for(i = -1*(round-1); (i < round-1) && !set ; i++){                 ret = isAreaClearBW(xpoint, ypoint+i, testPoint);    if (ret != null) break; } } else {       if(dcPos[pos].direction == DCD_EW){ for(i = round-1; (i >= -1*(round-1)) && !set; i--){ ret = isAreaClearBW(xpoint+i, ypoint, testPoint);   if (ret != null) break; }  } else {       ret = isAreaClearBW(xpoint, ypoint, testPoint); } } } }  if (round > round_limit) { break; } }  if(ret != null){ if (Debug.debugging("declutter")){ Debug.output("Placing object at " + ret.x + " | " + ret.y ); } } else { Debug.message("declutter", "Decluttering: No space for entry.");   ret = testPoint; ret.x = -1*windex; ret.y = -1*hindex; }  return ret;
public Point setNextOpen(Point point, int pixelLength, int pixelHeight) { return setNextOpen(point, pixelLength, pixelHeight, -1);
    public Point setNextOpen(Point point,                              int pixelLength, int pixelHeight,                             int pixelAwayLimit){        Debug.message("declutterdetail",                       "DeclutterMatrix: Trying to find an open space.");        if (needToRecreate) create();        boolean set = false;        // mark the original spot.  These are indexes, not pixels.        int windex = point.x/x_pix_interval;        int hindex = point.y/y_pix_interval;        // intermediate values used for ew/ns spanning.  This are not        // pixels, they are indexes into the matrix        int xpoint;         int ypoint;        // The point to be returned with the new, declutter postition        // for the object.        Point ret = null;        // Test point, for memory allocation savings.        Point testPoint = new Point();        // A round is a cycle through the positions in the search        // algorithm.  With every round the distance from the original        // position increases.  The search pattern looks like an        // expanding square, and it's broken into different pieces:        // Each side, not including the corners, and each corner.        int round = 0;  // Round 1, round 2 - get it!?!?        int pos, i;                // Set up the indexes for the original spot.        indexes.setFromPixels(point.x, point.y, pixelLength, pixelHeight);        // Make sure the graphic is on the visible screen        if(matrix == null || !indexes.withinMatrix){            return point;        }        // Do check for the center here, before looping.  Who knows,        // the space might be open.        ret = isAreaClearR(windex, hindex, testPoint);        int round_limit;        if (pixelAwayLimit < 0){            round_limit = Math.abs(maxy/2 - indexes.yStart) + (maxy / 2);        } else {            round_limit = pixelAwayLimit;        }        // Now that we know it is not open, move on and start searching for        // the right place.        while (ret == null) {            Debug.message("declutterdetail", "DeclutterMatrix: round "                           + round + "\n");            round++;            for (pos = 0; (dcPos[pos].position != DCP_MIDDLE)                      && (ret == null); pos++){                // Need to do this based on the length, so as to skip                // unnecessary checks.  The xpoint and ypoint are the                // starting point for each little incremental search.                // This point, in every round, spirals outward from                // the original desired location.                xpoint = windex+(round*dcPos[pos].ewindex);                ypoint = hindex+(round*dcPos[pos].nsindex);                                // checks to keep starting point away from being                // offscreen                if(xpoint <= maxx && ypoint <= maxy &&                    xpoint >=0 && ypoint >=0){                    // Start at handling the parts of the search                    // square that make up the sides of the square -                    // DCD_NS refers to the fact that the search                    // pattern is traversing up the side of the                    // square, for either side.                    // for the east and west checks, look                    // a north and south variations                    if(dcPos[pos].direction == DCD_NS){                        for(i = -1*(round-1); (i < round-1) && !set ; i++){                            // isAreaClearR used to be used here in                            // order to prevent a name from being                            // written over an icon.  You know, if the                            // icon shouldn't be written over, let it                            // be added to the matrix before the name.                            // Then it won't be covered.//                          // Don't look both ways if directly to the//                          // east, you'll write over the icon//                          if(dcPos[pos].position == DCP_EAST && //                             i <= pixelHeight){//                              ret = isAreaClearR(xpoint,//                                                 ypoint+i, //                                                 testPoint);//                          } else {                                ret = isAreaClearBW(xpoint,                                                    ypoint+i,                                                     testPoint);//                          }                            // If we've found a clear spot, jump out.                            if (ret != null) break;                        }                    } else {                        // Now we're checking the top and bottom of                        // the search square, which moves East-West                        // for the north and south check, look                        // at EW variations                        if(dcPos[pos].direction == DCD_EW){                            for(i = round-1; (i >= -1*(round-1)) && !set; i--){                                ret = isAreaClearBW(xpoint+i,                                                     ypoint,                                                     testPoint);                                // If we've found a clear spot, jump out.                                if (ret != null) break;                            }                                                    } else {                            // This part of the code handles the                            // corners of the square.                            // looking at the corners for the                            // search pattern                            ret = isAreaClearBW(xpoint, ypoint, testPoint);                        }                    }                  }            }            if (round > round_limit) {                break;            }        }  //while (ret == null)        if(ret != null){            if (Debug.debugging("declutter")){                Debug.output("Placing object at " + ret.x +                                   " | " + ret.y );            }        } else {            Debug.message("declutter",                           "Decluttering: No space for entry.");            // If you got here, space not found, toss            // oject offscreen            ret = testPoint;            ret.x = -1*windex;            ret.y = -1*hindex;        }                return ret;    }