/** * Generates a 2D Bresenham line between two points. * * @param a the starting point * @param b the ending point * @return The path between {@code a} and {@code b}. */ public static Coord[] line2D_(Coord a, Coord b) { return line2D_(a.x, a.y, b.x, b.y); }
/** * Generates a 3D Bresenham line between two points. * * @param a Coord to start from. This will be the first element of the list * @param b Coord to end at. This will be the last element of the list. * @return A list of points between a and b. */ public static Queue<Coord3D> line3D(Coord3D a, Coord3D b) { return line3D(a.x, a.y, a.z, b.x, b.y, b.z); }
/** * Generates a 2D Bresenham line between two points. If you don't need * the {@link Queue} interface for the returned reference, consider * using {@link #line2D_(Coord, Coord)} to save some memory. * * @param a the starting point * @param b the ending point * @return The path between {@code a} and {@code b}. */ public static Queue<Coord> line2D(Coord a, Coord b) { return line2D(a.x, a.y, b.x, b.y); }
/** * Constructs a ProjectileEffect with explicit settings for some fields. The valid cells this can affect will be * the full expanse of the IPackedColorPanel. * @param targeting the IPackedColorPanel to affect * @param duration the duration of this PanelEffect in seconds, as a float * @param startPoint the starting point of the projectile; may be best if it is adjacent to whatever fires it * @param endPoint the point to try to hit with the projectile; this should always succeed with no obstructions */ public ProjectileEffect(IPackedColorPanel targeting, float duration, Coord startPoint, Coord endPoint) { super(targeting, duration); this.startPoint = startPoint; this.endPoint = endPoint; affected = Bresenham.line2D_(startPoint.x, startPoint.y, endPoint.x, endPoint.y); } /**
/** * Returns a list of colors starting at the first color and moving to the * second color. The end point colors are included in the list. * * @param color1 starting color * @param color2 ending color * @return an ArrayList of SColor going from color1 to color2; the length should be no greater than 256. */ public ArrayList<SColor> asGradient(SColor color1, SColor color2) { String name = paletteNamer(color1, color2); if (palettes.containsKey(name)) { return palettes.get(name); } //get the gradient Queue<Coord3D> gradient = Bresenham.line3D(scolorToCoord3D(color1), scolorToCoord3D(color2)); ArrayList<SColor> ret = new ArrayList<>(); for (Coord3D coord : gradient) { ret.add(coord3DToSColor(coord)); } palettes.put(name, ret); return ret; }
/** * Generates a 2D Bresenham line between two points. If you don't need * the {@link Queue} interface for the returned reference, consider * using {@link #line2D_(int, int, int, int)} to save some memory. * <br> * Uses ordinary Coord values for points, and these can be pooled * if they aren't beyond what the current pool allows (it starts, * by default, pooling Coords with x and y between -3 and 255, * inclusive). If the Coords are pool-able, it can significantly * reduce the work the garbage collector needs to do, especially * on Android. * * @param startx the x coordinate of the starting point * @param starty the y coordinate of the starting point * @param endx the x coordinate of the starting point * @param endy the y coordinate of the starting point * @return a Queue (internally, an ArrayDeque) of Coord points along the line */ public static Queue<Coord> line2D(int startx, int starty, int endx, int endy) { // largest positive int for maxLength; a Queue cannot actually be given that many elements on the JVM return line2D(startx, starty, endx, endy, 0x7fffffff); }
/** * Generates a 2D Bresenham line between two points. Returns an array * of Coord instead of a Queue. * <br> * Uses ordinary Coord values for points, and these can be pooled * if they aren't beyond what the current pool allows (it starts, * by default, pooling Coords with x and y between -3 and 255, * inclusive). If the Coords are pool-able, it can significantly * reduce the work the garbage collector needs to do, especially * on Android. * * @param startx the x coordinate of the starting point * @param starty the y coordinate of the starting point * @param endx the x coordinate of the starting point * @param endy the y coordinate of the starting point * @return an array of Coord points along the line */ public static Coord[] line2D_(int startx, int starty, int endx, int endy) { // largest positive int for maxLength; it is extremely unlikely that this could be reached return line2D_(startx, starty, endx, endy, 0x7fffffff); }
/** * Constructs a ProjectileEffect with explicit settings for most fields. * @param targeting the IPackedColorPanel to affect * @param duration the duration of this PanelEffect in seconds, as a float * @param valid the valid cells that can be changed by this PanelEffect, as a GreasedRegion * @param startPoint the starting point of the projectile; may be best if it is adjacent to whatever fires it * @param endPoint the point to try to hit with the projectile; this may not be reached if the path crosses a cell not in valid */ public ProjectileEffect(IPackedColorPanel targeting, float duration, GreasedRegion valid, Coord startPoint, Coord endPoint) { super(targeting, duration, valid); this.startPoint = startPoint; this.endPoint = endPoint; affected = Bresenham.line2D_(startPoint.x, startPoint.y, endPoint.x, endPoint.y); for (int i = 0; i < affected.length; i++) { if(!validCells.contains(affected[i])) affected[i] = null; } }
private boolean bresenhamReachable(Radius radiusStrategy) { Coord[] path = Bresenham.line2D_(startx, starty, targetx, targety); lastPath = new ArrayDeque<>(); double rad = radiusStrategy.radius(startx, starty, targetx, targety); if(rad == 0.0) { lastPath.add(Coord.get(startx, starty)); return true; // already at the point; we can see our own feet just fine! } double decay = 1 / rad; double currentForce = 1; Coord p; for (int i = 0; i < path.length; i++) { p = path[i]; lastPath.offer(p); if (p.x == targetx && p.y == targety) { return true;//reached the end } if (p.x != startx || p.y != starty) {//don't discount the start location even if on resistant cell currentForce -= resistanceMap[p.x][p.y]; } double r = radiusStrategy.radius(startx, starty, p.x, p.y); if (currentForce - (r * decay) <= 0) { return false;//too much resistance } } return false;//never got to the target point }
!AreaUtils.verifyReach(aoe.getReach(), user, possibleTarget)) return false; Radius radiusStrategy = aoe.getMetric(); Coord[] path = Bresenham.line2D_(user.x, user.y, possibleTarget.x, possibleTarget.y); double rad = radiusStrategy.radius(user.x, user.y, possibleTarget.x, possibleTarget.y); if(rad < aoe.getMinRange() || rad > aoe.getMaxRange()) {