Anonymous avatar Anonymous committed 32808c8

made line algos into objects, added to line demo

Comments (0)

Files changed (7)

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

 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.Input;
 import com.badlogic.gdx.InputAdapter;
-import com.badlogic.gdx.graphics.FPSLogger;
 import com.badlogic.gdx.graphics.GL10;
 import com.badlogic.gdx.graphics.Pixmap;
 import com.badlogic.gdx.graphics.Texture;
 
     Console root;
 
-    FPSLogger fps = new FPSLogger();
-
     DemoApplet active;
 
     List<Class<? extends DemoApplet>> allApplets = new ArrayList<Class<? extends DemoApplet>>() {{
+        add(Lines.class);
         add(Fov.class);
         add(Colors.class);
         add(Offscreen.class);
-        add(Lines.class);
         add(Noise.class);
         add(Image.class);
     }};
         Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
         stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
         stage.draw();
-        fps.log();
     }
 
     @Override

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

 import com.google.common.collect.Iterators;
 import net.fishbulb.jcod.Console;
 import net.fishbulb.jcod.util.BlendMode;
+import net.fishbulb.jcod.util.PlotAlgorithm;
+import net.fishbulb.jcod.util.PlotAlgorithms;
+import net.fishbulb.jcod.util.PlotFunction;
 
+import java.text.MessageFormat;
 import java.util.Iterator;
 
 import static java.lang.Math.cos;
 import static java.lang.Math.sin;
-import static net.fishbulb.jcod.util.BlendMode.*;
-import static net.fishbulb.jcod.util.PlotAlgorithms.PlotFunction;
-import static net.fishbulb.jcod.util.PlotAlgorithms.bresenham;
+import static net.fishbulb.jcod.util.BlendMode.None;
+import static net.fishbulb.jcod.util.BlendMode.Set;
 
 public class Lines extends DemoApplet {
 
     Color lineColor = new Color(0.25f, 0.25f, 1.0f, 1.0f); // light blue
+    Color tmp = new Color();
 
     float currentAlpha = 1.0f;
 
+    Iterator<BlendMode> modes = Iterators.cycle(BlendMode.values());
     BlendMode currentMode;
 
-    BlendMode[] allModes = new BlendMode[]{
-            None, Set, Multiply, Lighten, Darken, Screen, ColorDodge, ColorBurn, Add,
-            AddAlpha, Alpha, AlphaLerp, Burn, Overlay,
-    };
-
-    Iterator<BlendMode> modes = Iterators.cycle(allModes);
+    Iterator<? extends PlotAlgorithm> algos = Iterators.cycle(PlotAlgorithms.values());
+    PlotAlgorithm currentAlgo;
 
     PlotFunction listener = new PlotFunction() {
         @Override public boolean apply(int x, int y, float val) {
-            getConsole().setCharBackground(x, y, lineColor, currentMode);
+            tmp.set(lineColor);
+            tmp.mul(val);
+            getConsole().setCharBackground(x, y, tmp, currentMode);
+
             return true;
         }
     };
         currentMode = modes.next();
     }
 
+    private void cycleAlgo() {
+        currentAlgo = algos.next();
+    }
+
     public Lines(Console parent) {
         super(parent);
         console.setDefaultForeground(Color.WHITE);
             }
         }
         cycleMode();
-        cycleMode();
+        cycleAlgo();
     }
 
     @Override public void update() {
 
         switch (currentMode) {
             case Alpha: case AddAlpha: case AlphaLerp:
-                currentAlpha = (float) ((1.0+cos((double)now / 1000 *2))/2.0);
+                currentAlpha = (float) ((1.0 + cos((double) now / 1000 * 2)) / 2.0);
                 break;
             default:
                 currentAlpha = 1.0f;
 
         // render the line
         lineColor.a = currentAlpha;
-        bresenham(xo, yo, xd, yd, listener);
+        currentAlgo.apply(xo, yo, xd, yd, listener);
 
-        // print the current flag
-        console.print(2, 2, currentMode + " (ENTER to change)");
+        // print settings
+        console.print(1, 1, MessageFormat.format("Blend: {0} (ENTER to change)", currentMode));
+        console.print(1, 2, MessageFormat.format("Line : {0} (L to change)", currentAlgo));
     }
 
 
     @Override public boolean keyDown(int keyCode) {
-        if (keyCode == Input.Keys.ENTER) {
-            cycleMode();
-            return true;
+        switch (keyCode) {
+            case Input.Keys.ENTER:
+                cycleMode();
+                return true;
+            case Input.Keys.L:
+                cycleAlgo();
+                return true;
         }
         return false;
     }

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

 package net.fishbulb.jcod.fov;
 
 import lombok.Getter;
-import net.fishbulb.jcod.util.PlotAlgorithms;
+import net.fishbulb.jcod.util.PlotFunction;
 
-import static net.fishbulb.jcod.util.PlotAlgorithms.bresenham;
+import static net.fishbulb.jcod.util.PlotAlgorithms.Bresenham;
 
 /**
  * A Bresenham-based line-of-sight algorithm.
  * Adapted from SquidPony implementation by Eben Howard - http://squidpony.com - howard@squidpony.com
  */
 public class BresenhamLOS implements LOSSolver {
-    class LOSFunction implements PlotAlgorithms.PlotFunction {
+    class LOSFunction implements PlotFunction {
         private float[][] resistanceMap;
         private final int startx;
         private final int starty;
             this.radiusStrategy = radiusStrategy;
         }
 
-        @Override public boolean apply(int x, int y) {
+        @Override public boolean apply(int x, int y, float val) {
             if (x != startx || y != starty) {
                 currentForce *= (1 - resistanceMap[x][y]);
             }
     @Override
     public boolean isReachable(float[][] resistanceMap, int startx, int starty, int targetx, int targety, float force, float decay, RadiusStrategy radiusStrategy) {
         LOSFunction solver = new LOSFunction(resistanceMap, startx, starty, force, decay, radiusStrategy);
-        bresenham(startx, starty, targetx, targety, solver);
+        Bresenham.apply(startx, starty, targetx, targety, solver);
         return solver.isReachable();
     }
 

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

+package net.fishbulb.jcod.util;
+
+public class MathUtils {
+    public static float trunc(float x) {
+        if (x < 0) {
+            return (float) Math.ceil(x);
+        } else {
+            return (float) Math.floor(x);
+        }
+    }
+
+    public static float frac(float x) {
+        return x - trunc(x);
+    }
+
+    public static float invfrac(float x) {
+        return 1 - frac(x);
+    }
+}

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

+package net.fishbulb.jcod.util;
+
+public interface PlotAlgorithm {
+    void apply(int x1, int y1, int x2, int y2, PlotFunction plot);
+}

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

 package net.fishbulb.jcod.util;
 
-public final class PlotAlgorithms {
+import static net.fishbulb.jcod.util.MathUtils.frac;
+import static net.fishbulb.jcod.util.MathUtils.invfrac;
+import static net.fishbulb.jcod.util.MathUtils.trunc;
 
-    private PlotAlgorithms() {}
+public enum PlotAlgorithms implements PlotAlgorithm {
 
-    public interface PlotFunction {
-        boolean apply(int x, int y, float val);
-    }
+    /**
+     * Bresenham's line drawing algorithm
+     * <p/>
+     * Based on SquidLib by Eben Howard - http://squidpony.com - howard@squidpony.com
+     */
+    Bresenham {
+        @Override
+        public void apply(int x1, int y1, int x2, int y2, PlotFunction plot) {
+            int xd, yd;
+            int x, y;
+            int ax, ay;
+            int sx, sy;
+            int dx, dy;
 
-    /** Bresenham algorithm implementation adapted from SquidLib. */
-    public static void bresenham(int x1, int y1, int x2, int y2, PlotFunction plot) {
-        int xd, yd;
-        int x, y;
-        int ax, ay;
-        int sx, sy;
-        int dx, dy;
+            dx = x2 - x1;
+            dy = y2 - y1;
 
-        dx = x2 - x1;
-        dy = y2 - y1;
+            ax = Math.abs(dx) << 1;
+            ay = Math.abs(dy) << 1;
 
-        ax = Math.abs(dx) << 1;
-        ay = Math.abs(dy) << 1;
+            sx = ((dx < 0) ? -1 : (dx > 0) ? 1 : 0);
+            sy = ((dy < 0) ? -1 : (dy > 0) ? 1 : 0);
 
-        sx = ((dx < 0) ? -1 : (dx > 0) ? 1 : 0);
-        sy = ((dy < 0) ? -1 : (dy > 0) ? 1 : 0);
+            x = x1;
+            y = y1;
 
-        x = x1;
-        y = y1;
+            if (ax >= ay) {
+                yd = ay - (ax >> 1);
+                while (true) {
+                    if (!plot.apply(x, y, 1.0f)) return;
+                    if (x == x2) return;
 
-        if (ax >= ay) {
-            yd = ay - (ax >> 1);
-            while (true) {
-                if (!plot.apply(x, y, 1.0f)) return;
-                if (x == x2) return;
+                    if (yd >= 0) {
+                        y += sy;
+                        yd -= ax;
+                    }
 
-                if (yd >= 0) {
+                    x += sx;
+                    yd += ay;
+                }
+            } else {
+                xd = ax - (ay >> 1);
+                while (true) {
+                    if (!plot.apply(x, y, 1.0f)) return;
+                    if (y == y2) return;
+
+                    if (xd >= 0) {
+                        x += sx;
+                        xd -= ay;
+                    }
+
                     y += sy;
-                    yd -= ax;
+                    xd += ax;
+                }
+            }
+        }
+    },
+
+    /**
+     * Based on work by Hugo Elias at
+     * http://freespace.virgin.net/hugo.elias/graphics/x_wuline.htm
+     * which itself is based on the algorithm designed by Xiaolin Wu
+     * <p/>
+     * Based on SquidLib by Eben Howard - http://squidpony.com - howard@squidpony.com
+     */
+    Elias {
+        @SuppressWarnings({"UnusedDeclaration", "SuspiciousNameCombination"})
+        public void apply(int startx, int starty, int endx, int endy, 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;
                 }
 
-                x += sx;
-                yd += ay;
-            }
-        } else {
-            xd = ax - (ay >> 1);
-            while (true) {
-                if (!plot.apply(x, y, 1.0f)) return;
-                if (y == y2) return;
-
-                if (xd >= 0) {
-                    x += sx;
-                    xd -= ay;
-                }
-
-                y += sy;
-                xd += ax;
+                yf += grad;
             }
         }
-    }
+    };
 
-    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);
-    }
+//    public static void bresenham(int x1, int y1, int x2, int y2, PlotFunction plot) {
+//        Bresenham.apply(x1, y1, x2, y2, plot);
+//    }
+//
+//    public static void elias(int x1, int y1, int x2, int y2, PlotFunction plot) {
+//        Elias.apply(x1, y1, x2, y2, plot);
+//    }
 
-    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/PlotFunction.java

+package net.fishbulb.jcod.util;
+
+public interface PlotFunction {
+    boolean apply(int x, int y, float val);
+}
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.