Created
April 19, 2021 03:32
-
-
Save L0ry-git/0b6da3250eafdc4ecbf2392b13e6efcd to your computer and use it in GitHub Desktop.
Simple Collatz conjecture visualizer I made while I was bored.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.Canvas; | |
| import java.awt.Color; | |
| import java.awt.Graphics; | |
| public class CollatzSheet extends Canvas { | |
| private static final long serialVersionUID = -7304438045996947968L; | |
| //current number whose sequence is being drawn | |
| private long num; | |
| //the scale of the sheet | |
| private ISheetScale scale; | |
| //value function - describes of what number the sequence is visualized at instant t | |
| private IValueFunction valueFunction; | |
| //opacity of every point | |
| private int drawAlpha = 50; | |
| //sequences starting from odd numbers are red, from even numbers are green | |
| private static final Color ODD = Color.RED, EVEN = Color.GREEN; | |
| private Color dc; | |
| //"time" elapsed (amount of numbers whose sequence has been visualized) | |
| private int t = 1; | |
| //maximum value ever found - gets visualized in the black graph | |
| private int currentMax = 0; | |
| //multiplier for the scale used to show how many steps takes each number to get to 1 | |
| private static final double STEP_MULTIPLIER = 0.70; | |
| //constructor | |
| public CollatzSheet() { | |
| this.scale = ISheetScale.LINEAR_SCALE(1); | |
| this.valueFunction = IValueFunction.LINEAR(1); | |
| this.setNum(1); | |
| } | |
| //sets the current number to visualize and updates the color | |
| public void setNum(long num) { | |
| this.num = num; | |
| this.dc = (num % 2 == 0) ? EVEN : ODD; | |
| } | |
| //sets the scale | |
| public void setScale(ISheetScale scale) { | |
| this.scale = scale; | |
| } | |
| //sets value function | |
| public void setValueFunction(IValueFunction valueFunction) { | |
| this.valueFunction = valueFunction; | |
| } | |
| //sets the draw alpha | |
| public void setDrawAlpha(int drawAlpha) { | |
| this.drawAlpha = drawAlpha; | |
| } | |
| //draws the current step | |
| private long drawStep(Graphics g, long value, int step) { | |
| //applies the scale and draws - bottom left corner is the origin | |
| int scaledValue = this.scale.apply(value); | |
| g.fillRect(step * 2, this.getHeight() - scaledValue, 2, 2); | |
| //updates scaled max value | |
| if(scaledValue > currentMax) currentMax = scaledValue; | |
| //applies the Collatz sequence | |
| return applyCollatz(value); | |
| } | |
| /* Collatz sequence s(k): | |
| * - s(0) is an integer n | |
| * s(k) is | |
| * - if s(k-1) is even, s(k-1)/2 | |
| * - if s(k-1) is odd, (s(k-1)*3)+1 | |
| */ | |
| private long applyCollatz(long value) { | |
| return (value % 2 == 0) ? (value / 2) : (value * 3 + 1); | |
| } | |
| //draws on the screen | |
| @Override | |
| public void paint(Graphics g) { | |
| //set color based on the current draw color and the draw alpha | |
| g.setColor(new Color(dc.getRed(), dc.getGreen(), dc.getBlue(), this.drawAlpha)); | |
| //current value to start the sequence from | |
| long value = num; | |
| //the amount of steps the visualization is taking | |
| int step = 0; | |
| //executes the visualization until value is 1 | |
| while(value != 1) { | |
| //draws current value and applies the sequence on it | |
| value = drawStep(g, value, step); | |
| //increments the amount of steps | |
| step++; | |
| } | |
| //shows the maximum value for this instant t | |
| g.setColor(Color.BLACK); | |
| g.fillRect(t / 2, currentMax, 2, 2); | |
| //shows how many steps it took to get to 1 | |
| g.setColor(Color.BLUE); | |
| g.fillRect(t / 2, (int)((double)step * STEP_MULTIPLIER), 2, 2); | |
| } | |
| //loops by incrementing time t and visualizing values according to the value function | |
| public void loop(int sleep) { | |
| //current number being visualized | |
| long currentNum = 1; | |
| //applies the value function until the current number is below (Long.MAX_VALUE / 2) | |
| while((currentNum = this.valueFunction.apply(t)) < Long.MAX_VALUE / 2) { | |
| try { | |
| //sets the number | |
| this.setNum(currentNum); | |
| this.paint(getGraphics()); | |
| //waits given amount of nanoseconds | |
| Thread.sleep(0, sleep); | |
| //increments instant t | |
| this.t++; | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @FunctionalInterface | |
| public interface ISheetScale { | |
| public static ISheetScale LINEAR_SCALE(double fac) { return (v) -> (int)(fac * (double)v); } | |
| public static ISheetScale LOG_SCALE(double fac) { return (v) -> (int)(fac * Math.log10(v)); } | |
| public int apply(long value); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @FunctionalInterface | |
| public interface IValueFunction { | |
| public static IValueFunction LINEAR(double fac) { return (t) -> (long)(fac * t); } | |
| public static IValueFunction POLYNOMIAL(double fac, double exp) { return (t) -> (long)(fac * Math.pow(t, exp)); } | |
| public static IValueFunction EXP(double fac) { return (t) -> (long)Math.pow(Math.E, fac * t); } | |
| public long apply(long t); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.swing.JFrame; | |
| public class Main { | |
| //sheet scale | |
| public static final ISheetScale SHEET_SCALE = ISheetScale.LOG_SCALE(10); | |
| //value function | |
| public static final IValueFunction VALUE_FUNCTION = IValueFunction.EXP(0.06); | |
| //draw alpha | |
| public static final int DRAW_ALPHA = 25; | |
| //sleep time in nanoseconds | |
| public static final int SLEEP_NANOS = 10; | |
| public static void main(String[] args) throws InterruptedException { | |
| //sets up the JFrame | |
| JFrame f = new JFrame(); | |
| f.setSize(890, 600); | |
| f.setResizable(false); | |
| f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| f.setLocationRelativeTo(null); | |
| //sets up the CollatzSheet | |
| CollatzSheet cs = new CollatzSheet(); | |
| cs.setDrawAlpha(DRAW_ALPHA); | |
| cs.setScale(SHEET_SCALE); | |
| cs.setValueFunction(VALUE_FUNCTION); | |
| //adds the CollatzSheet | |
| f.add(cs); | |
| //shows the JFrame | |
| f.setVisible(true); | |
| //starts the visualization | |
| cs.loop(SLEEP_NANOS); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment