Hello! I’m new here, hopefully this is the right place to ask.
I have an odd problem I’m trying to resolve. attempting to curve off a tutorial and turn the project into something of my own.
The problem being; I have a series of lines, creating a unique curved line. however these lines were created by translating the screen to 0, -len (10 in this case) to make the positioning of the lines easier. (not too difficult, but the screen was also rotated.)
The problem I’ve been working on for a while now is attempting to instead of using lines, turn the program into one that instead creates a shape using vertices, and draws the stroke. The code is a mess, but here’s what I’ve got so far.
import processing.pdf.*;
void setup() {
//set xpos and ypos here so they're not set each loop later.
float xpos = 1;
float ypos = 1;
size(1200, 600);
beginRecord(PDF, "collatz.pdf");
background(0);
//This loop is just for looks right now, not using until one line works
//the loop will create multiple lines.
for (int i = 1; i < 100; i++) {
IntList sequence = new IntList();
//n = value for the line to process, will be set to i once working. 2000 for testing purposes
int n = 2000;
do {
//sequence is an arraylist used to store the collatz results for n
sequence.append(n);
//collatz class defined below runs the collatz conjecture
n = collatz(n);
} while (n != 1);
sequence.append(1);
//reversing sequence to draw from one instead of 2000
sequence.reverse();
//len defines length of individual line segments (will be distance between vertices)
int len = 5;
//angle from previous vertex
float angle = 0;
float weight = 10;
PVector vec;
resetMatrix();
//points are moving below the screen, set 0 to middle to view temporarily
translate(width/2, height/2);
//creates the rules for moving points based off odd or even numbers
for (int j = 1; j < sequence.size(); j++) {
int value = sequence.get(j);
if (weight >= .2) {
weight -= .2;
}
float rand = radians(noise(i));
if (value % 2 == 0) {
angle -= rand;
} else {
angle += rand;
}
//create a new pvector (vec) using starting xpos and ypos
vec = new PVector(xpos, ypos);
println(vec);
//rotate the vector based on the angle determined above
vec.rotate(angle);
//thought normalizing and multiplying would give a point further out (as defined by mult())?
vec.normalize();
println(vec);
vec.mult(-100);
stroke(255);
strokeWeight(weight);
//using lines for debugging purposes while trying to get the code to work without transform
line(xpos, ypos, vec.x, vec.y);
//set xpos to vec.x and ypos to vec.y to move forward on the line
xpos = vec.x;
ypos = vec.y;
}
}
endRecord();
}
int collatz(int n) {
//even
if (n%2==0) {
return n/2;
} else {
//odd
return (n * 3 + 1)/2;
}
}
Any thoughts/advice?
All credit for the vast majority of this code to Mr. Daniel Shiffman and his very helpful videos