Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.core.PApplet;
- import java.io.File;
- import java.util.Objects;
- import java.util.function.Function;
- @FunctionalInterface
- interface TriFunction<A, B, C, R> {
- R apply(A a, B b, C c);
- default <V> TriFunction<A, B, C, V> andThen(
- Function<? super R, ? extends V> after) {
- Objects.requireNonNull(after);
- return (A a, B b, C c) -> after.apply(apply(a, b, c));
- }
- }
- public class ThreeDGraph extends PApplet {
- //Change the variables below as you like.
- private int delta = 5; //The lower the number, the less pixelated. pixelCount=sizeX*sizeY/delta, so be careful!
- private final float speed = .065f; //How fast it animates.
- private final boolean stroke = false; //Draw a grid?
- 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))
- private final boolean animate = true; //Animate the graph
- private final boolean deltaTime = false; //Makes the program run using delta time instead of frame count
- private final boolean clearWhenDone = false; //Makes the screen clear when the spiral has finished drawing.
- private final int rectsPerFrame = -1; //How many rectangles/"pixels" can be drawn per frame
- private final float numCirclesX = 5; //How many circles to draw on the X axis
- private final float numCirclesY = 5; //How many circles to draw on the Y axis
- private final float rotateVal = random(75); //How much to rotate each pixel
- private final boolean rotate = false; //If the graph should be rotated
- private final boolean saveImages = false; //Whether or not each frame should be saved as an image automatically.
- private final String autoSpiralsDir = "C:/Users/Nicholas/Pictures/autoSpirals/"; //The directory to store auto-saved images
- private final String manualSpiralsDir = "C:/Users/Nicholas/Pictures/manualSpirals/"; //The directory to store manually saved images
- 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)
- //Don't change the variables below.
- private boolean finished = false;
- private boolean clear = false;
- private float xFactor = 0, yFactor = 0;
- private int lastI = -1;
- private int currentFrame = 0;
- public void setup() {
- delta = delta >= 1 ? delta : 1; //Delta must be at least 1.
- background(0);
- strokeWeight(1);
- }
- public void settings() {
- size(1000, 1000);
- }
- public void keyPressed() {
- if (key == 'a') //Shifts the graph to down
- xFactor -= rotateVal!=0 ? (float)(1D / rotateVal) : 0;
- else if (key == 'q') //Shifts the graph up
- xFactor += rotateVal!=0 ? (float)(1D / rotateVal) : 0;
- else if (key == 'w') //Shifts the graph to the left
- yFactor += rotateVal!=0 ? (float)(1D / rotateVal) : 0;
- else if (key == 's') //Shifts the graph to the right
- yFactor -= rotateVal!=0 ? (float)(1D / rotateVal) : 0;
- else if (key == ' ') //Makes the graph only show what's currently being drawn
- clear = !clear;
- else if (key == 'd') { //Saves an image of the current screen
- int i;
- for (i = 0; new File(manualSpiralsDir + i + ".png").exists(); i++) {
- }
- saveFrame(manualSpiralsDir + i + ".png");
- }
- println(xFactor + "," + yFactor);
- background(0);
- }
- //Initializing vars; don't change these.
- int lastTick = 0;
- private float time = 1;
- public void draw() {
- if (finished) {
- if (saveImages) {
- int spiral = -1;
- while (new File(autoSpiralsDir + (++spiral) + ".png").exists()) {
- }
- saveFrame(autoSpiralsDir + spiral + ".png");
- }
- if (!deltaTime)
- time += speed;
- else
- time += (millis() - lastTick) * speed / 40; //Yay, delta time!
- }
- if (deltaTime)
- lastTick = millis(); //More delta time!
- int rectX, rectY;
- if (clear)
- background(0);
- if (!stroke)
- noStroke();
- float x, y, shade;
- for (int i = lastI + 1; i < width * height / delta; i++) {
- rectX = delta * (i / (width / delta));
- rectY = delta * (i % (width / delta));
- x = map(rectX, 0, width / delta, 0, numCirclesX * PI / delta);
- y = map(rectY, 0, height / delta, 0, numCirclesY * PI / delta);
- shade = shadeFunction.apply(x, y, time);
- if (colored) { //Make the Z axis of the graph hue
- colorMode(HSB);
- fill(shade, 360, 360);
- } else { //Make the Z axis of the graph grayscale shade
- colorMode(RGB);
- fill(shade);
- }
- rect(rectX , rectY, delta, delta);
- if (i != 0 && rectsPerFrame!=-1 && i % rectsPerFrame == 0) {
- finished = false;
- lastI = i;
- break;
- } else if (i == width * height / delta - 1) {
- finished = true;
- lastI = -1;
- if (clearWhenDone)
- background(0);
- }
- translate(width * xFactor, height * yFactor);
- if (rotate)
- rotate(rotateVal);
- }
- }
- public static void main(String[] args) {
- PApplet.main(new String[] {ThreeDGraph.class.getName()});
- }
- }
Add Comment
Please, Sign In to add comment