Skip to content

Instantly share code, notes, and snippets.

@vvzen
Last active January 29, 2018 13:49
Show Gist options
  • Save vvzen/4a173a538c7237eced7d965fdb40ea4a to your computer and use it in GitHub Desktop.
Save vvzen/4a173a538c7237eced7d965fdb40ea4a to your computer and use it in GitHub Desktop.
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@title : FUNCTIONAL. Initial study
@date : 27/01/2018
@author: vvz3n
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
import peasy.*;
import controlP5.*;
PeasyCam cam;
ControlP5 cp5;
ArrayList<Integer> collatz_numbers;
ArrayList<Integer> input_numbers;
int branch_length = 8;
float angle = 0;
void setup(){
size(1280, 720, P3D);
ortho();
// cam = new PeasyCam(this, 1200);
// cam.setMinimumDistance(20);
// cam.setMaximumDistance(7000);
cp5 = new ControlP5(this);
cp5.addSlider("angle")
.setPosition(20, 20)
.setRange(-14, 14);
cp5.addSlider("branch_length")
.setPosition(20, 40)
.setRange(1, 16);
input_numbers = new ArrayList<Integer>();
for (int i = 0; i < 100; i++){
int num = floor(random(0, 10000));
// don't add two times the same number
while (true){
if (!input_numbers.contains(num)){
input_numbers.add(num);
break;
}
else {
num = floor(random(0, 10000));
}
}
}
// println("collatz of " + n + ": " + collatz_numbers);
println("input numbers : " + input_numbers);
}
void draw(){
background(31);
stroke(255);
strokeWeight(3);
float rad_angle = radians(angle);
pushMatrix();
translate(100, height, 0);
rotateY(-90);
// translate(map(mouseX, 0, width, -300, 300), 0, 0);
draw_axis();
pushStyle();
for (int i = 0; i < input_numbers.size(); i++){
pushMatrix();
collatz(input_numbers.get(i), branch_length, rad_angle);
popMatrix();
}
popStyle();
popMatrix();
}
ArrayList<Integer> collatz(int n, float branchLength, float radAngle){
ArrayList <Integer> collatz_numbers = new ArrayList<Integer>();
beginShape(POINTS);
while(true){
collatz_numbers.add(n);
stroke(255, 255, 255);
line(0, 0, 0, 0, -branchLength, 0);
strokeWeight(3);
stroke(255, 0, 0);
vertex(0, 0, 0);
translate(0, -branchLength, 0);
vertex(0, 0, 0);
if (n == 1){
endShape();
return collatz_numbers;
}
else if (n % 2 == 0){
rotateX(radAngle);
n = n / 2;
}
else {
rotateX(-radAngle);
n = n * 3 + 1;
}
}
}
// draw xyz reference axis
void draw_axis(){
float size = 100;
pushStyle();
stroke(255, 0, 0);
line(-size, 0, 0, size, 0, 0);
stroke(0, 255, 0);
line(0, -size, 0, 0, size, 0);
stroke(0, 0, 255);
line(0, 0, -size, 0, 0, size);
popStyle();
}
// EVENTS
void mousePressed(){
println("mouse : " + map(mouseX, 0, width, -90, 90));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment