Am making a drawing machine for a thesis. I want it to make vaguely round blob polygons. No bezier curves - I want to use a polygon detection algorithm. I've got a method worked out of turning round slowly and then cutting off. Exterior angles should add up to TWO_PI, that's when we stop turning.
Problem is, the line sometimes over shoots the start or turns in to quickly. Instead of a nice wobbly blob it has a nasty kink in it. I thought of cutting it off if it gets too close to the start but that'd just not nice enough.
Any suggestions?
Code:
Tracer test;
void setup(){
size(400,400);
test = new Tracer();
}
void draw(){
test.draw();
}
void mousePressed(){
test.trace(mouseX,mouseY);
}
class Tracer{
Polygon [] polys;
boolean write;
float theta,radian;
Tracer(){
polys = new Polygon[1];
polys[0] = new Polygon(random(width),random(height));
}
void trace(float x, float y){
float theta = random(TWO_PI);
float circum = TWO_PI-0.5;
float xStart = x;
float yStart = y;
boolean write = true;
int num;
if (polys.length < 2)
num = 0;
else{
newPolygon(polys[polys.length-1]);
num = polys.length-1;
}
polys[num] = new Polygon(x,y);
float turn = 0.0;
while(turn < circum){
x += cos(theta+turn)*random(4,8);
y += sin(theta+turn)*random(4,8);
polys[num].addVertex(x,y);
turn += random(0.4);
if(turn > PI && dist(x,y,xStart,yStart) < 10) break;
}
}
void draw(){
for(int i = 0; i < polys.length; i++){
polys[i].draw();
}
}
void newPolygon(Polygon newPolys) {
Polygon [] temp = new Polygon[polys.length + 1];
System.arraycopy(polys, 0, temp, 0, polys.length);
temp[polys.length] = newPolys;
polys = temp;
}
}
class Polygon{
float [] x,y;
Polygon (float [] points){
x = new float[points.length/2];
y = new float[points.length/2];
for(int i = 0; i < points.length; i+=2){
x[i/2] = points[i];
y[i/2] = points[i+1];
}
}
Polygon (float startX, float startY){
x = new float[1];
y = new float[1];
x[0] = startX;
y[0] = startY;
}
void addVertex(float addX, float addY){
x = append(x, addX);
y = append(y, addY);
}
void draw(){
beginShape(POLYGON);
for(int i = 0; i < x.length; i++)
vertex(x[i],y[i]);
endShape();
}
}