xxxxxxxxxx
194
delta = 5;
angleStep = 0.1;
function setup() {
createCanvas(500, 500);
background(255);
// let's try and draw a wiggly box
//for (let i =0; i<3;i++) {
//for (let j=0; j<3;j++) {
// wigglyRandom(i*200,j*200,200,200,int(random()*10)+5);
//}
// }
//wigglyBox(150,150,200,200);
//line(10,100,10,10);
//wigglyLine(11,101,10,10);
// time to draw a wiggly circle
xs = [];
ys = [];
N = 20;
for (let k =0; k<N;k++) {
xs[k] = 200 + 100*cos(2*PI*k/N);
ys[k] = 200 + 100*sin(2*PI*k/N);
//circle(xs[k],ys[k],2);
}
//wigglyCurve([10,100,100,10],[10,10,100,100]);
wigglyCurve(xs,ys);
}
function wigglyBox(x1,y1,width,height) {
x2 = x1+width;
y2 = y1+height;
// the coordinates are the top left and bottom right corners
end1 = wigglyLine(x1,y1,x2,y1);
end2 = wigglyLine(end1[0],end1[1],x2,y2);
end3 = wigglyLine(end2[0],end2[1],x1,y2);
//wigglyLine(end3[0],end3[1],x1,y1);
print('End x:'+ end3[0]);
wigglyLine(x1,y2,x1,y1);
}
function wigglyRandom(x1,y1,width,height,n) {
// draws a random closed series of lines with n endpoints
oldX = x1;
oldY = y1;
for (let i=0;i<n;i++) {
newX = random()*width+x1;
newY = random()*height+y1;
end = wigglyLine(oldX,oldY,newX,newY);
oldX = end[0];
oldY = end[1];
}
wigglyLine(oldX,oldY,x1,y1);
}
function draw() {
}
function wigglyLine(x1,y1,x2,y2,thickness=2,deltaThickness=0.5) {
//line(x1+2,y1+2,x2+2,y2+2);
// find the length
len = sqrt(pow((x1-x2),2)+pow((y1-y2),2));
// find the angle
//ang = atan((y2-y1)/(x2-x1));
ang = findAngle(x2-x1+0.01,y2-y1+0.01);
// adding that 0.01 to avoid the singularities that sometimes occur
origAng = ang;
print(len);
x = x1;
y = y1;
for (let i=0;i<(len/delta);i++) {
strokeWeight(abs(thickness));
dx = delta*cos(ang);
dy = delta*sin(ang);
line(x,y,x+dx,y+dy);
x = x + dx;
y = y + dy;
// make the angle do a random walk with a little bit of bias
// to help it find its way.
ang += (random()-0.5+(origAng-ang))*angleStep;
// also make the thickness go through a random walk
thickness += deltaThickness*(random()-0.5);
}
// return the endpoint, which possibly is not
// exactly where you wanted.
arr = [];
arr[0]=x;
arr[1]=y;
return arr;
}
function wigglyCurve(xs,ys,thickness=2,deltaThickness=0.2) {
// okay, so this is going to draw a curve though the array
// of points
newX = 0;
newY = 0;
// accumulated random walk
deltaAng = 0;
oldX = xs[0];
oldY = ys[0];
for (let j=1; j<=xs.length;j++) {
// loop through all the points here
if (j==xs.length) {
newX = xs[0];
newY = ys[0];
}
else {
newX = xs[j];
newY = ys[j];
}
// now basically draw a line through the points
// find the length
len = sqrt(pow((newX-oldX),2)+pow((newY-oldY),2));
// find the angle
//ang = atan((y2-y1)/(x2-x1));
// find the angle that we are going towards
origAng = findAngle(newX-oldX+0.01,newY-oldY+0.01);
if (j == 1) {
ang = origAng;
}
else {
p = 0;
ang = ((p*ang)+((1-p)*origAng));
}
print(len);
x = oldX;
y = oldY;
for (let i=0;i<(len/delta);i++) {
strokeWeight(abs(thickness));
dx = delta*cos(ang);
dy = delta*sin(ang);
line(x,y,x+dx,y+dy);
x = x + dx;
y = y + dy;
// make the angle do a random walk with a little bit of bias
// to help it find its way.
deltaAng += ((random()-0.5)+(origAng-ang))*angleStep;
ang += deltaAng;
print('Delta ang is '+ deltaAng);
// also make the thickness go through a random walk
thickness += deltaThickness*(random()-0.5);
}
oldX = x;
oldY = y;
}
}
// next, I think I can make a wiggly "curve" option
// just draw a series of lines that have blobs
// this is a little snippet of code to deal with getting
// the angle part to work right, very annoying.
function findAngle(x,y) {
r = sqrt(pow(x,2) + pow(y,2));
angle = asin(abs(y)/r);
// now I need to put it into the right quadrant, which is somehow shockingly hard.
if (x< 0 && y > 0)
angle = PI - angle;
if (x < 0 && y < 0)
angle = PI + angle;
if (x > 0 && y < 0)
angle = 2*PI - angle;
return(angle);
}