OK, I have the main artery working. But I've run into a major snag. Since I have to instantiate the PGraphic object with a static width, the canvas space very quickly runs out. Not to mention, it's crazy slow.
Are there any other ways to accomplish this? What about drawing the vein system on a sphere and simply rotating the sphere to keep the head of the vein in view? Is it possible draw a line() directly onto a sphere()?
Here is the code I have so far. It works, but the artery hits the edge of the PGraphic pretty quickly:
Code:
PApplet app = this;
Vein artery;
void setup() {
size(800, 500);
background(0);
frameRate(50);
artery = new Vein(Vein.MAX_THICKNESS, width/2, height/2, random(2));
}
void draw() {
background(0);
}
public class Vein {
// contants
final static int MAX_THICKNESS = 5;
public float _y;
public float _x;
// private properties;
private PGraphics pg;
private int angle = 0;
private int thickness;
private int originalThickness;
private int count = 10;
private float yLast = 0;
private float xLast;
Vein (int thickness, float x, float y, float angle) {
// instantiate an instance of PGraphics
pg = createGraphics(1600, 1000, JAVA2D);
// set line width
thickness = originalThickness = thickness;
pg.strokeWeight(thickness);
// set x coordinate
_x = xLast = x;
// set y coordinate
_y = yLast = y;
// set initial angle
angle = angle;
// set color (not implemented)
// register to be drawn every frame
app.registerDraw(this);
}
void draw () {
// Generate a random seed (higher for thinner lines)
// This has the effect of causing smaller lines to move
// More eratically while the larger lines make less pronounced turns
float seed = MAX_THICKNESS-thickness+1;
// Add a random angle to the existing angle
angle += PI/180*(random(seed*20)-seed*10);
// Length to grow on this frame
float length = random(7)+seed+2;
// Add the length plus the cosine of the random angle
_x += length*cos(angle);
// Add the length plus the sine of the random angle
_y += length*sin(angle);
translate((-_x+width/2), (-_y+height/2));
// Draw the new addition
pg.beginDraw();
pg.stroke(255);
pg.strokeWeight(count);
pg.line(xLast, yLast, _x, _y);
pg.endDraw();
// store the current x and y for the next loop
xLast = _x;
yLast = _y;
// add the PGraphic to the sketch
image(pg, 50, 50);
}
}