📄 sparsegrid2d.java
字号:
for( int y0 = ymin; y0 <= ymax; y0++ ) { final int y_0 = sty(y0); xPos.add( x_0 ); yPos.add( y_0 ); } } } else // not toroidal { // compute xmin and xmax for the neighborhood such that they are within boundaries final int xmax = ((x+dist<=width-1)?x+dist:width-1); final int xmin = ((x-dist>=0)?x-dist:0); for( int x0 = xmin ; x0 <= xmax ; x0++ ) { // compute ymin and ymax for the neighborhood such that they are within boundaries // they depend on the curreny x0 value final int ymax = ((y+(dist-((x0-x>=0)?x0-x:x-x0))<=height-1)?y+(dist-((x0-x>=0)?x0-x:x-x0)):height-1); final int ymin = ((y-(dist-((x0-x>=0)?x0-x:x-x0))>=0)?y-(dist-((x0-x>=0)?x0-x:x-x0)):0); for( int y0 = ymin; y0 <= ymax; y0++ ) { xPos.add( x0 ); yPos.add( y0 ); } } } } public void getNeighborsHexagonalDistance( final int x, final int y, final int dist, final boolean toroidal, IntBag xPos, IntBag yPos ) { // won't work for negative distances if( dist < 0 ) { throw new RuntimeException( "Runtime exception in method getNeighborsHexagonalDistance: Distance must be positive" ); } if( xPos == null || yPos == null ) { throw new RuntimeException( "Runtime exception in method getNeighborsHamiltonianDistance: xPos and yPos should not be null" ); } xPos.clear(); yPos.clear(); if( toroidal && height%2==1 ) throw new RuntimeException( "Runtime exception in getNeighborsHexagonalDistance: toroidal hexagonal environment should have even heights" ); if( toroidal ) { // compute ymin and ymax for the neighborhood int ymin = y - dist; int ymax = y + dist; for( int y0 = ymin ; y0 <= ymax ; y0 = downy(x,y0) ) { xPos.add( stx(x) ); yPos.add( sty(y0) ); } int x0 = x; for( int i = 1 ; i <= dist ; i++ ) { final int temp_ymin = ymin; ymin = dly( x0, ymin ); ymax = uly( x0, ymax ); x0 = dlx( x0, temp_ymin ); for( int y0 = ymin ; y0 <= ymax ; y0 = downy(x0,y0) ) { xPos.add( stx(x0) ); yPos.add( sty(y0) ); } } x0 = x; ymin = y-dist; ymax = y+dist; for( int i = 1 ; i <= dist ; i++ ) { final int temp_ymin = ymin; ymin = dry( x0, ymin ); ymax = ury( x0, ymax ); x0 = drx( x0, temp_ymin ); for( int y0 = ymin ; y0 <= ymax ; y0 = downy(x0,y0) ) { xPos.add( stx(x0) ); yPos.add( sty(y0) ); } } } else // not toroidal { if( x < 0 || x >= width || y < 0 || y >= height ) throw new RuntimeException( "Runtime exception in method getNeighborsHexagonalDistance: invalid initial position" ); // compute ymin and ymax for the neighborhood int ylBound = y - dist; int yuBound = ((y+dist<height)?y+dist:height-1); for( int y0 = ylBound ; y0 <= yuBound ; y0 = downy(x,y0) ) { xPos.add( x ); yPos.add( y0 ); } int x0 = x; int ymin = y-dist; int ymax = y+dist; for( int i = 1 ; i <= dist ; i++ ) { final int temp_ymin = ymin; ymin = dly( x0, ymin ); ymax = uly( x0, ymax ); x0 = dlx( x0, temp_ymin ); yuBound = ((ymax<height)?ymax:height-1); if( x0 >= 0 ) for( int y0 = ylBound ; y0 <= yuBound ; y0 = downy(x0,y0) ) { if( y0 >= 0 ) { xPos.add( x0 ); yPos.add( y0 ); } } } x0 = x; ymin = y-dist; ymax = y+dist; for( int i = 1 ; i <= dist ; i++ ) { final int temp_ymin = ymin; ymin = dry( x0, ymin ); ymax = ury( x0, ymax ); x0 = drx( x0, temp_ymin ); yuBound = ((ymax<height)?ymax:height); if( x0 < width ) for( int y0 = ymin ; y0 <= yuBound; y0 = downy(x0,y0) ) { if( y0 >= 0 ) { xPos.add( x0 ); yPos.add( y0 ); } } } } } /** * Gets all neighbors of a location that satisfy max( abs(x-X) , abs(y-Y) ) <= dist. This region forms a * square 2*dist+1 cells across, centered at (X,Y). If dist==1, this * is equivalent to the so-called "Moore Neighborhood" (the eight neighbors surrounding (X,Y)), plus (X,Y) itself. * Places each x and y value of these locations in the provided IntBags xPos and yPos, clearing the bags first. * Then places into the result Bag the objects at each of those <x,y> locations clearning it first. * Returns the result Bag. * null may be passed in for the various bags, though it is more efficient to pass in a 'scratch bag' for * each one. */ public Bag getNeighborsMaxDistance( final int x, final int y, final int dist, final boolean toroidal, Bag result, IntBag xPos, IntBag yPos ) { if( xPos == null ) xPos = new IntBag(); if( yPos == null ) yPos = new IntBag(); getNeighborsMaxDistance( x, y, dist, toroidal, xPos, yPos ); return getObjectsAtLocations(xPos,yPos,result); } /** * Gets all neighbors of a location that satisfy abs(x-X) + abs(y-Y) <= dist. This region forms a diamond * 2*dist+1 cells from point to opposite point inclusive, centered at (X,Y). If dist==1 this is * equivalent to the so-called "Von-Neumann Neighborhood" (the four neighbors above, below, left, and right of (X,Y)), * plus (X,Y) itself. * Places each x and y value of these locations in the provided IntBags xPos and yPos, clearing the bags first. * Then places into the result Bag the objects at each of those <x,y> locations, clearning it first. * Returns the result Bag (constructing one if null had been passed in). * null may be passed in for the various bags, though it is more efficient to pass in a 'scratch bag' for * each one. */ public Bag getNeighborsHamiltonianDistance( final int x, final int y, final int dist, final boolean toroidal, Bag result, IntBag xPos, IntBag yPos ) { if( xPos == null ) xPos = new IntBag(); if( yPos == null ) yPos = new IntBag(); getNeighborsHamiltonianDistance( x, y, dist, toroidal, xPos, yPos ); return getObjectsAtLocations(xPos,yPos,result); } /** * Gets all neighbors located within the hexagon centered at (X,Y) and 2*dist+1 cells from point to opposite point * inclusive. * If dist==1, this is equivalent to the six neighbors immediately surrounding (X,Y), * plus (X,Y) itself. * Places each x and y value of these locations in the provided IntBags xPos and yPos, clearing the bags first. * Then places into the result Bag the objects at each of those <x,y> locations clearning it first. * Returns the result Bag (constructing one if null had been passed in). * null may be passed in for the various bags, though it is more efficient to pass in a 'scratch bag' for * each one. */ public Bag getNeighborsHexagonalDistance( final int x, final int y, final int dist, final boolean toroidal, Bag result, IntBag xPos, IntBag yPos ) { if( xPos == null ) xPos = new IntBag(); if( yPos == null ) yPos = new IntBag(); getNeighborsHexagonalDistance( x, y, dist, toroidal, xPos, yPos ); return getObjectsAtLocations(xPos,yPos,result); } /** For each <xPos,yPos> location, puts all such objects into the result bag. Returns the result bag. If the provided result bag is null, one will be created and returned. */ public Bag getObjectsAtLocations(final IntBag xPos, final IntBag yPos, Bag result) { if (result==null) result = new Bag(); else result.clear(); final int len = xPos.numObjs; final int[] xs = xPos.objs; final int[] ys = yPos.objs; for(int i=0; i < len; i++) { // a little efficiency: add if we're 1, addAll if we're > 1, // do nothing if we're 0 Bag temp = getObjectsAtLocation(xs[i],ys[i]); if (temp!=null) { int n = temp.numObjs; if (n==1) result.add(temp.objs[0]); else if (n > 1) result.addAll(temp); } } return result; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -