diff --git a/js/id/geo.js b/js/id/geo.js index 2137cdc35..7f3eb9457 100644 --- a/js/id/geo.js +++ b/js/id/geo.js @@ -118,7 +118,7 @@ iD.geo.lineIntersection = function(a, b) { } return null; -} +}; // Return whether point is contained in polygon. // diff --git a/test/spec/geo.js b/test/spec/geo.js index a6a86ecb2..289bb8911 100644 --- a/test/spec/geo.js +++ b/test/spec/geo.js @@ -156,6 +156,34 @@ describe('iD.geo', function() { }); }); + describe('.lineIntersection', function() { + it('returns null if lines are colinear with overlap', function() { + var a = [[0, 0], [10, 0]], + b = [[-5, 0], [5, 0]]; + expect(iD.geo.lineIntersection(a, b)).to.be.null; + }); + it('returns null if lines are colinear but disjoint', function() { + var a = [[5, 0], [10, 0]], + b = [[-10, 0], [-5, 0]]; + expect(iD.geo.lineIntersection(a, b)).to.be.null; + }); + it('returns null if lines are parallel', function() { + var a = [[0, 0], [10, 0]], + b = [[0, 5], [10, 5]]; + expect(iD.geo.lineIntersection(a, b)).to.be.null; + }); + it('returns the intersection point between 2 lines', function() { + var a = [[0, 0], [10, 0]], + b = [[5, 10], [5, -10]]; + expect(iD.geo.lineIntersection(a, b)).to.eql([5, 0]); + }); + it('returns null if lines are not parallel but not intersecting', function() { + var a = [[0, 0], [10, 0]], + b = [[-5, 10], [-5, -10]]; + expect(iD.geo.lineIntersection(a, b)).to.be.null; + }); + }); + describe('.pointInPolygon', function() { it('says a point in a polygon is on a polygon', function() { var poly = [[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]; diff --git a/test/spec/svg/midpoints.js b/test/spec/svg/midpoints.js index 499e9405b..55634d170 100644 --- a/test/spec/svg/midpoints.js +++ b/test/spec/svg/midpoints.js @@ -10,7 +10,7 @@ describe("iD.svg.Midpoints", function () { .call(iD.svg.Surface(context)); }); - it("finds the location of the midpoints", function () { + it("creates midpoint on segment completely within the extent", function () { var a = iD.Node({loc: [0, 0]}), b = iD.Node({loc: [50, 0]}), line = iD.Way({nodes: [a.id, b.id]}), @@ -23,7 +23,7 @@ describe("iD.svg.Midpoints", function () { expect(surface.select('.midpoint').datum().loc).to.eql([25, 0]); }); - it("doesn't create midpoints on segments with pixel length less than 40", function () { + it("doesn't create midpoint on segment with pixel length less than 40", function () { var a = iD.Node({loc: [0, 0]}), b = iD.Node({loc: [39, 0]}), line = iD.Way({nodes: [a.id, b.id]}), @@ -35,4 +35,44 @@ describe("iD.svg.Midpoints", function () { expect(surface.selectAll('.midpoint')[0]).to.have.length(0); }); + + it("doesn't create midpoint on segment completely outside of the extent", function () { + var a = iD.Node({loc: [-100, 0]}), + b = iD.Node({loc: [-50, 0]}), + line = iD.Way({nodes: [a.id, b.id]}), + graph = iD.Graph([a, b, line]), + extent = iD.geo.Extent([0, 0], [100, 100]); + + context.selectedIDs = function() { return [line.id]; }; + surface.call(iD.svg.Midpoints(projection, context), graph, [line], filter, extent); + + expect(surface.selectAll('.midpoint')[0]).to.have.length(0); + }); + + it("creates midpoint on extent edge for segment partially outside of the extent", function () { + var a = iD.Node({loc: [50, 0]}), + b = iD.Node({loc: [500, 0]}), + line = iD.Way({nodes: [a.id, b.id]}), + graph = iD.Graph([a, b, line]), + extent = iD.geo.Extent([0, 0], [100, 100]); + + context.selectedIDs = function() { return [line.id]; }; + surface.call(iD.svg.Midpoints(projection, context), graph, [line], filter, extent); + + expect(surface.select('.midpoint').datum().loc).to.eql([100, 0]); + }); + + it("doesn't create midpoint on extent edge for segment with pixel length less than 20", function () { + var a = iD.Node({loc: [81, 0]}), + b = iD.Node({loc: [500, 0]}), + line = iD.Way({nodes: [a.id, b.id]}), + graph = iD.Graph([a, b, line]), + extent = iD.geo.Extent([0, 0], [100, 100]); + + context.selectedIDs = function() { return [line.id]; }; + surface.call(iD.svg.Midpoints(projection, context), graph, [line], filter, extent); + + expect(surface.selectAll('.midpoint')[0]).to.have.length(0); + }); + });