Leaflet/spec/suites/geometry/LineUtilSpec.js

91 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2013-02-05 19:51:27 +08:00
describe('LineUtil', function () {
describe('#clipSegment', function () {
var bounds;
beforeEach(function () {
bounds = L.bounds([5, 0], [15, 10]);
});
it('clips a segment by bounds', function () {
2013-02-05 19:51:27 +08:00
var a = new L.Point(0, 0);
var b = new L.Point(15, 15);
var segment = L.LineUtil.clipSegment(a, b, bounds);
2013-03-02 05:49:20 +08:00
expect(segment[0]).to.eql(new L.Point(5, 5));
expect(segment[1]).to.eql(new L.Point(10, 10));
2013-02-05 19:51:27 +08:00
var c = new L.Point(5, -5);
var d = new L.Point(20, 10);
var segment2 = L.LineUtil.clipSegment(c, d, bounds);
2013-03-02 05:49:20 +08:00
expect(segment2[0]).to.eql(new L.Point(10, 0));
expect(segment2[1]).to.eql(new L.Point(15, 5));
2013-02-05 19:51:27 +08:00
});
it('uses last bit code and reject segments out of bounds', function () {
2013-02-05 19:51:27 +08:00
var a = new L.Point(15, 15);
var b = new L.Point(25, 20);
var segment = L.LineUtil.clipSegment(a, b, bounds, true);
2013-03-02 05:49:20 +08:00
expect(segment).to.be(false);
2013-02-05 19:51:27 +08:00
});
2015-02-07 08:36:27 +08:00
it('can round numbers in clipped bounds', function () {
var a = new L.Point(4, 5);
var b = new L.Point(8, 6);
2015-02-07 08:36:27 +08:00
var segment1 = L.LineUtil.clipSegment(a, b, bounds);
2015-02-07 08:36:27 +08:00
expect(segment1[0]).to.eql(new L.Point(5, 5.25));
expect(segment1[1]).to.eql(b);
2015-02-07 08:36:27 +08:00
var segment2 = L.LineUtil.clipSegment(a, b, bounds, false, true);
2015-02-07 08:36:27 +08:00
expect(segment2[0]).to.eql(new L.Point(5, 5));
expect(segment2[1]).to.eql(b);
});
2013-02-05 19:51:27 +08:00
});
describe('#pointToSegmentDistance & #closestPointOnSegment', function () {
var p1 = new L.Point(0, 10);
var p2 = new L.Point(10, 0);
var p = new L.Point(0, 0);
it('calculates distance from point to segment', function () {
2013-03-02 05:49:20 +08:00
expect(L.LineUtil.pointToSegmentDistance(p, p1, p2)).to.eql(Math.sqrt(200) / 2);
2013-02-05 19:51:27 +08:00
});
it('calculates point closest to segment', function () {
2013-03-02 05:49:20 +08:00
expect(L.LineUtil.closestPointOnSegment(p, p1, p2)).to.eql(new L.Point(5, 5));
2013-02-05 19:51:27 +08:00
});
});
describe('#simplify', function () {
it('simplifies polylines according to tolerance', function () {
2013-02-05 19:51:27 +08:00
var points = [
new L.Point(0, 0),
new L.Point(0.01, 0),
new L.Point(0.5, 0.01),
new L.Point(0.7, 0),
new L.Point(1, 0),
new L.Point(1.999, 0.999),
new L.Point(2, 1)
];
var simplified = L.LineUtil.simplify(points, 0.1);
2013-03-02 05:49:20 +08:00
expect(simplified).to.eql([
2013-02-05 19:51:27 +08:00
new L.Point(0, 0),
new L.Point(1, 0),
new L.Point(2, 1)
]);
});
});
});