Skip to content

Commit

Permalink
fix joining lines
Browse files Browse the repository at this point in the history
  • Loading branch information
dethe committed Jan 31, 2016
1 parent 775dfb4 commit 375d5e2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
17 changes: 16 additions & 1 deletion js/app.js
Expand Up @@ -49,6 +49,21 @@ function rotatePoint(px, py, angle){
return [x, y];
}

function linePoints(e){
var x1=e.num('cx'), y1=e.num('cy'), x2=e.num('x2')+x1, y2=e.num('y2')+y1;
return [[x1, y1], [x2, y2]];
}

function pointsAdjacent(p1, p2){
return (abs(p1[0] - p2[0]) < 5) && (abs(p1[1] - p2[1]) < 5);
}

function endpointsMatch(line1, line2){
var l1 = linePoints(line1);
var l2 = linePoints(line2);
return pointsAdjacent(l1[0], l2[0]) || pointsAdjacent(l1[0], l2[1]) || pointsAdjacent(l1[1], l2[0]) || pointsAdjacent(l1[1], l2[0]);
}

function randomId() {
// Based on Paul Irish's random hex color:http://www.paulirish.com/2009/random-hex-color-code-snippets/
// Theoretically could return non-unique values, not going to let that keep me up at night
Expand Down Expand Up @@ -312,7 +327,7 @@ Snap.plugin(function (Snap, Element, Paper, global, Fragment) {
var selfType = this.attr('type');
var otherType = e.attr('type');
if (selfType === 'line' && otherType === 'line'){
return !! Snap.path.intersection(this, e).length;
return (!! Snap.path.intersection(this, e).length) || endpointsMatch(this, e);
}else if (selfType === 'line'){
var x1=this.num('cx'), x2=this.num('x2')+x1, y1=this.num('cy'), y2=this.num('y2')+y1;
var ex=e.num('cx'), ey=e.num('cy');
Expand Down
10 changes: 5 additions & 5 deletions js/levels.js
Expand Up @@ -19,10 +19,10 @@ var levels = [
['spiral', 0, -75, 25, 500, 3],
['spiral', 0, 0, 25, 20, .75],

['line', 75, -150, 25, 25],
['line', 75, -75, 25, 0],
['line', 75, 0, 25, -25],
['line', 75, 75, 0, 25],
['line', 75, -150, 27, 27],
['line', 75, -75, 27, 0],
['line', 75, 0, 27, -27],
['line', 75, 75, 0, 27],

]
},
Expand All @@ -32,4 +32,4 @@ var levels = [
['line', -25, 0, 100, 0]
]
},
]
]

0 comments on commit 375d5e2

Please sign in to comment.