xxxxxxxxxx
91
points = []
offset1 = []
offset2 = []
lineList = []
record = false
function setup() {
createCanvas(400, 400);
stroke(0);
strokeWeight(1);
frameRate(20)
}
function weighted(x1, y1, x2, y2, x3, y3, x4, y4){
noFill();
threshold = 10
counter = 1
dx1 = (x4 - x1) / threshold
dx2 = (x3- x2) / threshold
dy1 = (y4 - y1) / threshold
dy2 = (y3 - y2) / threshold
while(counter < threshold){
lineList.push([int(x1), int(y1), int(x2), int(y2)])
// line(x1, y1, x2, y2)
x1 += dx1
x2 += dx2
y1 += dy1
y2 += dy2
counter += 1
if(lineList.length > 100 * 10){
lineList.shift();
}
}
}
function calcOff(i){
dx = points[i][0]-points[i + 1][0]
dy = points[i][1]-points[i + 1][1]
vx = 1
vy = -vx * dx / (dy + 0.01)
m = sqrt(vx*vx + vy*vy)
m1 = sqrt(dx * dx + dy * dy)
vx = vx / (m + 0.01) * 20 * (4.0 / (m1 + 1.0))
vy = vy / (m + 0.01) * 10* (4.0 / (m1 + 1.0))
if(sqrt(vx*vx + vy*vy) > 20.0){
return
}
offset1.push([points[i][0] + vx, points[i][1] + vy])
// print(points[i][0] - vx)
// print(points[i][1] - vy)
offset2.push([points[i][0] - vx, points[i][1] - vy])
}
function draw() {
background(255);
points.push([mouseX, mouseY]);
if(points.length >= 2){
calcOff(points.length - 2)
}
if(points.length > 100){
points.shift();
}
if(offset1.length > 100){
offset1.shift();
}
if(offset2.length > 100){
offset2.shift();
}
for( i = 0; i < offset1.length - 1; i ++){
weighted(offset1[i][0], offset1[i][1],
offset1[i + 1][0], offset1[i + 1][1],
offset2[i + 1][0], offset2[i + 1][1],
offset2[i][0], offset2[i][1])
}
for( i = 0; i < lineList.length - 1; i ++){
[x1, y1, x2, y2] = lineList[i]
line(x1, y1, x2, y2)
}
if(record){
save('calligraphic_polyline.svg')
record = false
}
}
function keyPressed(){
record = true
}