You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.2 KiB

function connected_lines_get_rnd_params() {
return {
start_x: Math.random(),
start_y: Math.random(),
offs_x: 0.05,
offs_y: 0.05,
num_steps: 5,
line_distance: 7/500,
};
}
function connected_lines(params) {
if(typeof params == 'undefined') {
var params = connected_lines_get_rnd_params();
}
var start = new paper.Point(params.start_x, params.start_y);
var offs = new paper.Point(params.offs_x, params.offs_y);
var lines = [];
for(var i=0; i<2; i++) {
var x = start.x + i*offs.x;
var y = start.y + i*offs.y;
var path = new paper.Path({strokeWidth: 5, strokeColor: 'black'});
for(var s=0; s<params.num_steps; s++) {
x = (4*x*(1-x)) % 1;
y = (x+y) % 1;
var p = new paper.Point(x, y);
path.add(p);
}
lines.push(path);
}
var conlines = new paper.Group();
for(var i=0; i<params.num_steps-1; i++) {
// TODO use less variables?
var o0start = lines[0].getOffsetOf(lines[0].segments[i].point);
var o0end = lines[0].getOffsetOf(lines[0].segments[i+1].point);
var o1start = lines[1].getOffsetOf(lines[1].segments[i].point);
var o1end = lines[1].getOffsetOf(lines[1].segments[i+1].point);
var num_steps2_0 = (o0end - o0start) / params.line_distance;
var num_steps2_1 = (o1end - o1start) / params.line_distance;
var num_steps2 = Math.min(num_steps2_0, num_steps2_1);
for(var j=0; j<num_steps2; j++) {
var p0 = lines[0].getPointAt(o0start + (o0end-o0start) * (j/num_steps2));
var p1 = lines[1].getPointAt(o1start + (o1end-o1start) * (j/num_steps2));
var path = new paper.Path.Line({
from: p0, to: p1,
strokeWidth: 2,
strokeColor: 'black'
});
// TODO param: conn lines min length
if(path.length < 0.001) {
path.removeSegments();
delete path;
} else {
conlines.addChild(path);
}
}
}
lines[1].removeSegments();
delete lines[1];
return new paper.Group([lines[0], conlines]);
}