/** Makes a copy of the vector without using the complex clone interface. * * @return copy of the vector */ public IVector2 copy() { return new Vector2Int(this); }
/** * @return the best location */ public IVector2 getBestLocation() { double best = desire[myX][myY]; int bx = myX; int by = myY; for(int i = w; i-->0;) { for(int j = h; j-->0;) { if(!field.obstacles[i][j]) { double q = desire[i][j]+0.01f*(w*h-field.distance[i][j])/w*h; if(q>best) { best = q; bx = i; by = j; } } } } return new Vector2Int(bx, by); }
/** * Get a vector for two doubles. * @param a The first value. * @param b The second value. * @return The vector (null if at least one of args is null). */ public static IVector2 getVector2(Integer a, Integer b) { IVector2 ret = null; if(a!=null && b!=null) ret = a.intValue()==0 && b.intValue()==0? ZERO: new Vector2Int(a.intValue(), b.intValue()); return ret; }
/** * Retrieves a random position within the simulation area with a minimum * distance from the edge. * @param distance minimum distance from the edge, null or zero for no distance */ public IVector2 getRandomGridPosition(IVector2 distance) { synchronized(monitor) { IVector2 ret = null; ret = new Vector2Int(getRandomPosition(distance)); return ret; } }
/** * If set to true, inverts the y-axis (top-down instead of bottom-up). * * @param b if true, inverts the y-axis */ public void setInvertY(boolean b) { if(b) { inversionFlag_ = new Vector2Int(inversionFlag_.getXAsInteger(), 1); } else { inversionFlag_ = new Vector2Int(inversionFlag_.getXAsInteger(), 0); } }
/** * If set to true, inverts the x-axis (right-left instead of left-right). * * @param b if true, inverts the x-axis */ public void setInvertX(boolean b) { if(b) { inversionFlag_ = new Vector2Int(1, inversionFlag_.getYAsInteger()); } else { inversionFlag_ = new Vector2Int(0, inversionFlag_.getYAsInteger()); } }
/** * Set the area size. * @param areasize The area size. */ public void setAreaSize(IVector2 areasize) { synchronized(monitor) { this.areasize = areasize==null? null: new Vector2Int(areasize.getXAsInteger(), areasize.getYAsInteger()); } }
/** * Get the possible moves. * @param space The 2D space to move in. * @param sourcepos The source position. * @return The directions to go (i.e. all possible directions excluding 'none' or an array with only 'none'). */ public static String[] getPossibleDirections(Grid2D space, IVector2 sourcepos) { Map moves = new HashMap(); moves.put(DIRECTION_LEFT, new Vector2Int(sourcepos.getXAsInteger()-1, sourcepos.getYAsInteger())); moves.put(DIRECTION_RIGHT, new Vector2Int(sourcepos.getXAsInteger()+1, sourcepos.getYAsInteger())); moves.put(DIRECTION_UP, new Vector2Int(sourcepos.getXAsInteger(), sourcepos.getYAsInteger()-1)); moves.put(DIRECTION_DOWN, new Vector2Int(sourcepos.getXAsInteger(), sourcepos.getYAsInteger()+1)); for(Iterator it=moves.keySet().iterator(); it.hasNext(); ) { IVector2 pos = (IVector2)moves.get(it.next()); Collection obstacles = space.getSpaceObjectsByGridPosition(pos, "obstacle"); if(obstacles!=null && !obstacles.isEmpty()) { it.remove(); } } return moves.isEmpty() ? new String[]{DIRECTION_NONE} : (String[])moves.keySet().toArray(new String[moves.size()]); }
/** * Get the possible moves. * @param space The 2D space to move in. * @param sourcepos The source position. * @return The directions to go (i.e. all possible directions excluding 'none' or an array with only 'none'). */ public static String[] getPossibleDirections(Grid2D space, IVector2 sourcepos) { Map moves = new HashMap(); moves.put(DIRECTION_LEFT, new Vector2Int(sourcepos.getXAsInteger()-1, sourcepos.getYAsInteger())); moves.put(DIRECTION_RIGHT, new Vector2Int(sourcepos.getXAsInteger()+1, sourcepos.getYAsInteger())); moves.put(DIRECTION_UP, new Vector2Int(sourcepos.getXAsInteger(), sourcepos.getYAsInteger()-1)); moves.put(DIRECTION_DOWN, new Vector2Int(sourcepos.getXAsInteger(), sourcepos.getYAsInteger()+1)); for(Iterator it=moves.keySet().iterator(); it.hasNext(); ) { IVector2 pos = (IVector2)moves.get(it.next()); Collection obstacles = space.getSpaceObjectsByGridPosition(pos, "obstacle"); if(obstacles!=null && !obstacles.isEmpty()) { it.remove(); } } return moves.isEmpty() ? new String[]{DIRECTION_NONE} : (String[])moves.keySet().toArray(new String[moves.size()]); }
/** * Sets the position of the viewport. */ public void setPosition(IVector2 pos) { position_ = pos; IVector2 pixSize = getPixelSize(); pixPosition_ = position_.copy().divide(pixSize); pixPosition_ = (new Vector2Double(new Vector2Int(pixPosition_))).multiply(pixSize); }
public AbstractViewport(IPerspective perspective) { rendering = false; this.perspective = perspective; bgColor_ = Color.BLACK; inversionFlag_ = new Vector2Int(0); position_ = Vector2Double.ZERO.copy(); preserveAR_ = true; size_ = new Vector2Double(1.0); areaSize_ = new Vector2Double(1.0); paddedSize_ = new Vector2Double(1.0); drawObjects_ = Collections.synchronizedSet(new HashSet()); objectLayers_ = Collections.synchronizedSortedSet(new TreeSet()); objectList_ = Collections.synchronizedList(new ArrayList()); preLayers_ = new Layer[0]; postLayers_ = new Layer[0]; listeners_ = Collections.synchronizedSet(new HashSet()); zoomLimit_ = 20.0; }
/** * This method will be executed by the object before the process gets added * to the execution queue. * @param clock The clock. * @param space The space this process is running in. */ public void start(IClockService clock, IEnvironmentSpace space) { this.lasttick = clock.getTick(); // Initialize the field. Space2D grid = (Space2D)space; int sizex = grid.getAreaSize().getXAsInteger(); int sizey = grid.getAreaSize().getYAsInteger(); for(int x=0; x<sizex; x++) { for(int y=0; y<sizey; y++) { Map props = new HashMap(); Boolean alive = Boolean.valueOf(Math.random()>0.7); props.put("alive", alive); props.put(Space2D.PROPERTY_POSITION, new Vector2Int(x, y)); grid.createSpaceObject("cell", props, null); } } // System.out.println("create waste process started."); }
/** * Creates a new {@link ContinuousSpace2D} with a special ID. * @param name the name of this space * @param areasize the size of the 2D area * @param actionexecutor executor for component actions */ public Grid2D(Object name, IVector2 areasize) { super(areasize==null? null: new Vector2Int(areasize.getXAsInteger(), areasize.getYAsInteger())); this.setProperty("name", name); this.objectsygridpos = new MultiCollection(); }
for(int i=0; i<n && ret==null; i++) ret = new Vector2Int(getRandomPosition(Vector2Int.ZERO)); if(objectsygridpos.containsKey(ret)) ret = null; ret = new Vector2Int(x, y); if(objectsygridpos.containsKey(ret)) ret = null;
props.put("heat", Double.valueOf(heat)); props.put(Space2D.PROPERTY_POSITION, new Vector2Int(x, y)); grid.createSpaceObject("patch", props, null);
/** * Compute the next position. */ protected static IVector2 computeNextPosition(IVector2 pos, int sizex, int sizey) { // Go right in even lanes // if(pos==null) // System.out.println("testi2"); if(pos.getXAsInteger()+1<sizex && pos.getYAsInteger()%2==0) { pos = new Vector2Int(pos.getXAsInteger()+1, pos.getYAsInteger()); } // Go left in odd lanes else if(pos.getXAsInteger()-1>=0 && pos.getYAsInteger()%2!=0) { pos = new Vector2Int(pos.getXAsInteger()-1, pos.getYAsInteger()); } // Go down else else { pos = new Vector2Int(pos.getXAsInteger(), (pos.getYAsInteger()+1)%sizey); } return pos; }
/** * Compute the next position. */ protected static IVector2 computeNextPosition(IVector2 pos, int sizex, int sizey) { // Go right in even lanes // if(pos==null) // System.out.println("testi2"); if(pos.getXAsInteger()+1<sizex && pos.getYAsInteger()%2==0) { pos = new Vector2Int(pos.getXAsInteger()+1, pos.getYAsInteger()); } // Go left in odd lanes else if(pos.getXAsInteger()-1>=0 && pos.getYAsInteger()%2!=0) { pos = new Vector2Int(pos.getXAsInteger()-1, pos.getYAsInteger()); } // Go down else else { pos = new Vector2Int(pos.getXAsInteger(), (pos.getYAsInteger()+1)%sizey); } return pos; }
/** * Destroys an object in this space. * @param objectId the object's ID */ public void destroySpaceObject(Object id) { try { synchronized(monitor) { // remove the object from grid IVector2 pos = (IVector2)getSpaceObject(id).getProperty(Space2D.PROPERTY_POSITION); if(pos!=null) { IVector2 fieldpos = new Vector2Int(pos.getXAsInteger(), pos.getYAsInteger()); objectsygridpos.removeObject(fieldpos, spaceobjects.get(id)); } super.destroySpaceObject(id); } } catch(Exception e) { e.printStackTrace(); } }
/** * Destroys an object in this space. * @param objectId the object's ID */ public boolean destroyAndVerifySpaceObject(Object id) { boolean ret = false; try { synchronized(monitor) { // remove the object from grid IVector2 pos = (IVector2)getSpaceObject(id).getProperty(Space2D.PROPERTY_POSITION); if(pos!=null) { IVector2 fieldpos = new Vector2Int(pos.getXAsInteger(), pos.getYAsInteger()); objectsygridpos.removeObject(fieldpos, spaceobjects.get(id)); } super.destroySpaceObject(id); ret = true; } } catch(Exception e) { ret = false; } return ret; }
if(oldpos!=null) oldpos = new Vector2Int(oldpos.getXAsInteger(), oldpos.getYAsInteger()); if(objectsygridpos.containsKey(oldpos)) if(newpos!=null) objectsygridpos.add(new Vector2Int(newpos.getXAsInteger(), newpos.getYAsInteger()), obj);