xxxxxxxxxx
260
delta = 5;
angleStep = 0.1;
typicalSize = 100;
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);
//wigglyCircle(width/2,height/2,100,20);
boxwidth = 200 + 30*random();
boxheight = 200+ 30*random();
hairyBox(150,150,boxwidth,boxheight);
//wigglyCircle(width/2,height/2-100,100,10);
//hairyLine(0,0,100,100,20,10,2,0.2);
}
function hairyLine(x1,y1,x2,y2,hairLength=20,hairWidth=10,thickness=2,ratio=0.5) {
randomNess = 5;
end = wigglyLine(x1,y1,x2,y2);
length = sqrt(pow((x1-x2),2)+pow((y1-y2),2));
angle = findAngle(x2-x1+0.01,y2-y1+0.01);
print(len);
currx = x1;
curry = y1;
let deltaX = hairWidth*cos(angle);
let deltaY = hairWidth*sin(angle);
let perpX = hairLength*sin(angle);
let perpY = -hairLength*cos(angle);
let q = 0;
for (q=0;q<((length/hairWidth)-1);q++) {
strokeWeight(abs(thickness));
//print(angle);
currx = currx + deltaX;
curry = curry + deltaY;
//let perpX = hairLength*sin(angle);
//let perpY = -hairLength*cos(angle);
perpX += randomNess*(random()-0.5);
perpY += randomNess*(random()-0.5);
//deltaX += randomNess/20*(random()-0.5);
//deltaY += randomNess/20*(random()-0.5);
//print('Delta '+dx);
//print(length/hairWidth);
//ends = wigglyLine(x,y,x+perpX,y+perpY);
wigglyLine(currx-perpX*ratio,curry-perpY*ratio,currx+perpX,curry+perpY);
print('Here ',currx);
}
print('Here at the end '+ q);
return end;
}
function wigglyCircle(x,y,radius,N) {
// time to draw a wiggly circle
xs = [];
ys = [];
//N = 20;
for (let k =0; k<N;k++) {
xs[k] = x + radius*cos(2*PI*k/N);
ys[k] = y + radius*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 hairyBox(x1,y1,width,height) {
let x2 = x1+width;
let y2 = y1+height;
// the coordinates are the top left and bottom right corners
end1 = hairyLine(x1,y1,x2,y1);
end2 = hairyLine(end1[0],end1[1],x2,y2);
end3 = hairyLine(end2[0],end2[1],x1,y2);
//wigglyLine(end3[0],end3[1],x1,y1);
print('End x:'+ end3[0]);
hairyLine(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);
let x = x1;
let y = y1;
for (let i=0;i<(len/delta);i++) {
strokeWeight(abs(thickness));
let dx = delta*cos(ang);
let 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)+1*(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);
}