moomoomoo309

Processing3DGraph

Sep 19th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.61 KB | None | 0 0
  1. import processing.core.PApplet;
  2.  
  3. import java.io.File;
  4. import java.util.Objects;
  5. import java.util.function.Function;
  6.  
  7. @FunctionalInterface
  8. interface TriFunction<A, B, C, R> {
  9.  
  10.     R apply(A a, B b, C c);
  11.  
  12.     default <V> TriFunction<A, B, C, V> andThen(
  13.             Function<? super R, ? extends V> after) {
  14.         Objects.requireNonNull(after);
  15.         return (A a, B b, C c) -> after.apply(apply(a, b, c));
  16.     }
  17. }
  18.  
  19. public class ThreeDGraph extends PApplet {
  20.     //Change the variables below as you like.
  21.     private int delta = 5; //The lower the number, the less pixelated. pixelCount=sizeX*sizeY/delta, so be careful!
  22.     private final float speed = .065f; //How fast it animates.
  23.     private final boolean stroke = false; //Draw a grid?
  24.     private final boolean colored = false; //Use rainbows instead of gray scale (true is hue=f(x,y,time), false is shade=f(x,y,time))
  25.     private final boolean animate = true; //Animate the graph
  26.     private final boolean deltaTime = false; //Makes the program run using delta time instead of frame count
  27.     private final boolean clearWhenDone = false; //Makes the screen clear when the spiral has finished drawing.
  28.     private final int rectsPerFrame = -1; //How many rectangles/"pixels" can be drawn per frame
  29.     private final float numCirclesX = 5; //How many circles to draw on the X axis
  30.     private final float numCirclesY = 5; //How many circles to draw on the Y axis
  31.     private final float rotateVal = random(75); //How much to rotate each pixel
  32.     private final boolean rotate = false; //If the graph should be rotated
  33.     private final boolean saveImages = false; //Whether or not each frame should be saved as an image automatically.
  34.     private final String autoSpiralsDir = "C:/Users/Nicholas/Pictures/autoSpirals/"; //The directory to store auto-saved images
  35.     private final String manualSpiralsDir = "C:/Users/Nicholas/Pictures/manualSpirals/"; //The directory to store manually saved images
  36.     private final TriFunction<Float, Float, Float, Float> shadeFunction = (x, y, time) -> map(((sin(y)+cos(x)+time)+1)*2%2-1, -1, 1, 0, 255); //What 4D function to graph (3D w/time)
  37.  
  38.     //Don't change the variables below.
  39.     private boolean finished = false;
  40.     private boolean clear = false;
  41.     private float xFactor = 0, yFactor = 0;
  42.     private int lastI = -1;
  43.     private int currentFrame = 0;
  44.  
  45.  
  46.     public void setup() {
  47.         delta = delta >= 1 ? delta : 1; //Delta must be at least 1.
  48.         background(0);
  49.         strokeWeight(1);
  50.     }
  51.  
  52.     public void settings() {
  53.         size(1000, 1000);
  54.     }
  55.  
  56.     public void keyPressed() {
  57.         if (key == 'a') //Shifts the graph to down
  58.             xFactor -= rotateVal!=0 ? (float)(1D / rotateVal) : 0;
  59.         else if (key == 'q') //Shifts the graph up
  60.             xFactor += rotateVal!=0 ? (float)(1D / rotateVal) : 0;
  61.         else if (key == 'w') //Shifts the graph to the left
  62.             yFactor += rotateVal!=0 ? (float)(1D / rotateVal) : 0;
  63.         else if (key == 's') //Shifts the graph to the right
  64.             yFactor -= rotateVal!=0 ? (float)(1D / rotateVal) : 0;
  65.         else if (key == ' ') //Makes the graph only show what's currently being drawn
  66.             clear = !clear;
  67.         else if (key == 'd') { //Saves an image of the current screen
  68.             int i;
  69.             for (i = 0; new File(manualSpiralsDir + i + ".png").exists(); i++) {
  70.             }
  71.             saveFrame(manualSpiralsDir + i + ".png");
  72.         }
  73.  
  74.         println(xFactor + "," + yFactor);
  75.         background(0);
  76.     }
  77.  
  78.     //Initializing vars; don't change these.
  79.     int lastTick = 0;
  80.     private float time = 1;
  81.  
  82.     public void draw() {
  83.         if (finished) {
  84.             if (saveImages) {
  85.                 int spiral = -1;
  86.                 while (new File(autoSpiralsDir + (++spiral) + ".png").exists()) {
  87.                 }
  88.                 saveFrame(autoSpiralsDir + spiral + ".png");
  89.             }
  90.             if (!deltaTime)
  91.                 time += speed;
  92.             else
  93.                 time += (millis() - lastTick) * speed / 40; //Yay, delta time!
  94.         }
  95.         if (deltaTime)
  96.             lastTick = millis(); //More delta time!
  97.         int rectX, rectY;
  98.         if (clear)
  99.             background(0);
  100.         if (!stroke)
  101.             noStroke();
  102.         float x, y, shade;
  103.         for (int i = lastI + 1; i < width * height / delta; i++) {
  104.             rectX = delta * (i / (width / delta));
  105.             rectY = delta * (i % (width / delta));
  106.             x = map(rectX, 0, width / delta, 0, numCirclesX * PI / delta);
  107.             y = map(rectY, 0, height / delta, 0, numCirclesY * PI / delta);
  108.             shade = shadeFunction.apply(x, y, time);
  109.             if (colored) { //Make the Z axis of the graph hue
  110.                 colorMode(HSB);
  111.                 fill(shade, 360, 360);
  112.             } else { //Make the Z axis of the graph grayscale shade
  113.                 colorMode(RGB);
  114.                 fill(shade);
  115.             }
  116.             rect(rectX , rectY, delta, delta);
  117.             if (i != 0 && rectsPerFrame!=-1 && i % rectsPerFrame == 0) {
  118.                 finished = false;
  119.                 lastI = i;
  120.                 break;
  121.             } else if (i == width * height / delta - 1) {
  122.                 finished = true;
  123.                 lastI = -1;
  124.                 if (clearWhenDone)
  125.                     background(0);
  126.             }
  127.  
  128.             translate(width * xFactor, height * yFactor);
  129.             if (rotate)
  130.                 rotate(rotateVal);
  131.         }
  132.     }
  133.  
  134.     public static void main(String[] args) {
  135.         PApplet.main(new String[] {ThreeDGraph.class.getName()});
  136.     }
  137. }
Add Comment
Please, Sign In to add comment