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.

65 lines
2.1 KiB

function connected_lines_get_rnd_params() {
return {
start: new Point(Math.random(), Math.random()),
offs: new Point(0.05, 0.05),
num_steps: 5,
};
}
function connected_lines(params) {
if(typeof params == 'undefined') {
var params = connected_lines_get_rnd_params();
}
var lines = [];
for(var i=0; i<2; i++) {
var x = params.start.x + i*params.offs.x;
var y = params.start.y + i*params.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);
// TODO param: conn lines density
var num_steps2_0 = (o0end - o0start) / (7/500);
var num_steps2_1 = (o1end - o1start) / (7/500);
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]);
}