Anonymous avatar Anonymous committed a213c64

converted elias to callback API (which now takes a plot value)

Comments (0)

Files changed (6)

src/main/java/net/fishbulb/jcod/demo/Lines.java

     Iterator<BlendMode> modes = Iterators.cycle(allModes);
 
     PlotFunction listener = new PlotFunction() {
-        @Override public boolean apply(int x, int y) {
+        @Override public boolean apply(int x, int y, float val) {
             getConsole().setCharBackground(x, y, lineColor, currentMode);
             return true;
         }

src/main/java/net/fishbulb/jcod/fov/Direction.java

     UP(0, -1), DOWN(0, 1), LEFT(-1, 0), RIGHT(1, 0),
     UP_LEFT(-1, -1), UP_RIGHT(1, -1), DOWN_LEFT(-1, 1), DOWN_RIGHT(1, 1),
     NONE(0, 0);
-    /**
-     * An array which holds only the four cardinal directions.
-     */
+
+    /** An array which holds only the four cardinal directions. */
     public static final Direction[] CARDINALS = {UP, DOWN, LEFT, RIGHT};
-    /**
-     * An array which holds only the four diagonal directions.
-     */
+
+    /** An array which holds only the four diagonal directions. */
     public static final Direction[] DIAGONALS = {UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT};
-    /**
-     * An array which holds all eight OUTWARDS directions.
-     */
+
+    /** An array which holds all eight OUTWARDS directions. */
     public static final Direction[] OUTWARDS = {UP, DOWN, LEFT, RIGHT, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT};
-    /**
-     * The x coordinate difference for this direction.
-     */
-    public int deltaX;
-    /**
-     * The y coordinate difference for this direction.
-     */
-    public int deltaY;
+
+    /** The x coordinate difference for this direction. */
+    public final int deltaX;
+
+    /** The y coordinate difference for this direction. */
+    public final int deltaY;
 
     /**
      * Returns the direction that most closely matches the input.
     /**
      * Returns the Direction one step clockwise including diagonals.
      *
-     * @param dir
-     * @return
      */
     public Direction clockwise() {
         switch (this) {
     /**
      * Returns the Direction one step counterclockwise including diagonals.
      *
-     * @param dir
-     * @return
      */
     public Direction counterClockwise() {
         switch (this) {

src/main/java/net/fishbulb/jcod/fov/EliasLOS.java

 import net.fishbulb.jcod.util.Elias;
 
 import java.awt.Point;
-import java.util.LinkedList;
 import java.util.List;
-import java.util.Queue;
 
 /**
  * Uses Wu's Algorithm as modified by Elias to draw the line.
- *
+ * <p/>
  * The side view parameter is how far along the antialiased path to consider
  * when determining if the far end can be seen, from 0.0 to 1.0 with 1.0
  * considering the entire line. The closer to 1.0 this value is, the more
  * complete and symmetrical end-to-end the line is, but the computation cost
  * also increases.
- *
+ * <p/>
  * If undesired artifacts are seen when running the lines (missing squares
  * perhaps), try increasing the sideview value.
  *
 @Beta
 public class EliasLOS implements LOSSolver {
 
-    private Queue<Point> lastPath = new LinkedList<>();
     private float sideview = 0.75f;
 
-    /**
-     * Creates this solver with the default side view parameter.
-     */
+    /** Creates this solver with the default side view parameter. */
     public EliasLOS() {
     }
 
     @Override
     public boolean isReachable(float[][] resistanceMap, int startx, int starty, int targetx, int targety, float force, float decay, RadiusStrategy radiusStrategy) {
         List<Point> path = Elias.line(startx, starty, targetx, targety);
-        lastPath = new LinkedList<>(path);//save path for later retreival
 
-        BresenhamLOS los1 = new BresenhamLOS(),
-                los2 = new BresenhamLOS();
+        BresenhamLOS los1 = new BresenhamLOS();
+        BresenhamLOS los2 = new BresenhamLOS();
 
         float checkRadius = radiusStrategy.radius(startx, starty) * 0.75f;
         while (!path.isEmpty()) {
                     && los1.isReachable(resistanceMap, p.x, p.y, targetx, targety, force - (radiusStrategy.radius(startx, starty, p.x, p.y) * decay), decay, radiusStrategy)
                     && los2.isReachable(resistanceMap, startx, starty, p.x, p.y, force, decay, radiusStrategy)) {
 
-                //record actual sight path used
-                lastPath = new LinkedList<>(los1.lastPath);
-                lastPath.addAll(los2.lastPath);
                 return true;
             }
         }

src/main/java/net/fishbulb/jcod/util/Elias.java

         return lightMap;
     }
 
-    /**
-     * Gets the line between the two points.
-     *
-     * @param startx
-     * @param starty
-     * @param endx
-     * @param endy
-     * @return
-     */
+
     public static List<Point> line(double startx, double starty, double endx, double endy) {
         path = new LinkedList<>();
         width = (int) (Math.max(startx, endx) + 1);
         return path;
     }
 
-    /**
-     * Marks the location as having the visibility given.
-     *
-     * @param x
-     * @param y
-     * @param c
-     */
+
     private static void mark(double x, double y, double c) {
         //check bounds overflow from antialiasing
         if (x >= 0 && x < width && y >= 0 && y < height) {

src/main/java/net/fishbulb/jcod/util/PlotAlgorithms.java

     private PlotAlgorithms() {}
 
     public interface PlotFunction {
-        boolean apply(int x, int y);
+        boolean apply(int x, int y, float val);
     }
 
     /** Bresenham algorithm implementation adapted from SquidLib. */
         if (ax >= ay) {
             yd = ay - (ax >> 1);
             while (true) {
-                if (!plot.apply(x, y)) return;
+                if (!plot.apply(x, y, 1.0f)) return;
                 if (x == x2) return;
 
                 if (yd >= 0) {
         } else {
             xd = ax - (ay >> 1);
             while (true) {
-                if (!plot.apply(x, y)) return;
+                if (!plot.apply(x, y, 1.0f)) return;
                 if (y == y2) return;
 
                 if (xd >= 0) {
             }
         }
     }
-}
+
+    private static float trunc(float x) {
+        if (x < 0) {
+            return (float) Math.ceil(x);
+        } else {
+            return (float) Math.floor(x);
+        }
+    }
+
+    private static float frac(float x) {
+        return x - trunc(x);
+    }
+
+    private static float invfrac(float x) {
+        return 1 - frac(x);
+    }
+
+    /**
+     *
+     * Based on work by Hugo Elias at
+     * http://freespace.virgin.net/hugo.elias/graphics/x_wuline.htm which is in turn
+     * base on work by Wu.
+     *
+     * Based on SquidLib by Eben Howard - http://squidpony.com - howard@squidpony.com
+     */
+    @SuppressWarnings({"UnusedDeclaration", "SuspiciousNameCombination"})
+    private static void elias(int startx, int starty, int endx, int endy, PlotAlgorithms.PlotFunction plot) {
+        float x1 = startx, y1 = starty, x2 = endx, y2 = endy;
+        float grad, xd, yd, length, xm, ym, xgap, ygap, xend, yend, xf, yf, brightness1, brightness2;
+        int x, y, ix1, ix2, iy1, iy2;
+        boolean shallow = false;
+
+        xd = x2 - x1;
+        yd = y2 - y1;
+
+        if (Math.abs(xd) > Math.abs(yd)) {
+            shallow = true;
+        }
+
+        if (!shallow) {
+            float temp = x1;
+            x1 = y1;
+            y1 = temp;
+            temp = x2;
+            x2 = y2;
+            y2 = temp;
+            xd = x2 - x1;
+            yd = y2 - y1;
+        }
+        if (x1 > x2) {
+            float temp = x1;
+            x1 = x2;
+            x2 = temp;
+            temp = y1;
+            y1 = y2;
+            y2 = temp;
+            xd = x2 - x1;
+            yd = y2 - y1;
+        }
+
+        grad = yd / xd;
+
+        //add the first end point
+        xend = trunc(x1 + .5f);
+        yend = y1 + grad * (xend - x1);
+
+        xgap = invfrac(x1 + .5f);
+
+        ix1 = (int) xend;
+        iy1 = (int) yend;
+
+        brightness1 = invfrac(yend) * xgap;
+        brightness2 = frac(yend) * xgap;
+
+        if (shallow) {
+            if (!plot.apply(ix1, iy1, brightness1)) return;
+            if (!plot.apply(ix1, iy1 + 1, brightness2)) return;
+        } else {
+            if (!plot.apply(iy1, ix1, brightness1)) return;
+            if (!plot.apply(iy1 + 1, ix1, brightness2)) return;
+        }
+
+        yf = yend + grad;
+
+        //add the second end point
+        xend = trunc(x2 + .5f);
+        yend = y2 + grad * (xend - x2);
+
+        xgap = invfrac(x2 - .5f);
+
+        ix2 = (int) xend;
+        iy2 = (int) yend;
+
+        brightness1 = invfrac(yend) * xgap;
+        brightness2 = frac(yend) * xgap;
+
+        if (shallow) {
+            if (!plot.apply(ix2, iy2, brightness1)) return;
+            if (!plot.apply(ix2, iy2 + 1, brightness2)) return;
+        } else {
+            if (!plot.apply(iy2, ix2, brightness1)) return;
+            if (!plot.apply(iy2 + 1, ix2, brightness2)) return;
+        }
+
+        //add the in-between points
+        for (x = ix1 + 1; x < ix2; x++) {
+            brightness1 = invfrac(yf);
+            brightness2 = frac(yf);
+
+            if (shallow) {
+                if (!plot.apply(x, (int) yf, brightness1)) return;
+                if (!plot.apply(x, (int) yf + 1, brightness2)) return;
+            } else {
+                if (!plot.apply((int) yf, x, brightness1)) return;
+                if (!plot.apply((int) yf + 1, x, brightness2)) return;
+            }
+
+            yf += grad;
+        }
+    }
+}
+

src/main/java/net/fishbulb/jcod/util/Point3D.java

-package net.fishbulb.jcod.util;
-
-import java.awt.Point;
-
-/**
- * Generic three dimensional coordinate class.
- *
- * @author Lewis Potter
- * @author Eben Howard - http://squidpony.com - howard@squidpony.com
- */
-public class Point3D extends Point {
-    public int z;
-
-    /**
-     * Creates a three dimensional coordinate with the given location.
-     * 
-     * @param x
-     * @param y
-     * @param z 
-     */
-    public Point3D(int x, int y, int z) {
-        super(x, y);
-        this.z = z;
-    }
-
-    /**
-     * Returns the linear distance between this coordinate point and the provided one.
-     * 
-     * @param other
-     * @return 
-     */
-    public double distance(Point3D other) {
-        return Math.sqrt(squareDistance(other));
-    }
-
-    /**
-     * Returns the square of the linear distance between this coordinate
-     * point and the provided one.
-     * 
-     * @param other
-     * @return 
-     */
-    public double squareDistance(Point3D other) {
-        double dx = x - other.x;
-        double dy = y - other.y;
-        double dz = z - other.z;
-        return dx * dx + dy * dy + dz * dz;
-    }
-
-    /**
-     * Returns the Manhattan distance between this point and the provided one.
-     * The Manhattan distance is the distance between each point on each separate
-     * axis all added together.
-     * 
-     * @param other
-     * @return 
-     */
-    public int manhattanDistance(Point3D other) {
-        int distance = Math.abs(x - other.x);
-        distance += Math.abs(y - other.y);
-        distance += Math.abs(z - other.z);
-        return distance;
-    }
-
-    /**
-     * Returns the largest difference between the two points along any one axis.
-     * 
-     * @param other
-     * @return 
-     */
-    public int maxAxisDistance(Point3D other) {
-        return Math.max(Math.max(Math.abs(x - other.x), Math.abs(y - other.y)), Math.abs(z - other.z));
-    }
-
-    @Override
-    public int hashCode() {
-        int hash = 5;
-        hash = 73 * hash + x;
-        hash = 73 * hash + y;
-        hash = 73 * hash + z;
-        return hash;
-    }
-
-    @Override
-    public boolean equals(Object o) {
-        if (o instanceof Point3D) {
-            Point3D other = (Point3D) o;
-            return x == other.x && y == other.y && z == other.z;
-        } else {
-            return false;
-        }
-    }
-}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.