Add snapping abilities on circles

This commit is contained in:
Thomas Chandelle
2017-05-23 13:11:34 +02:00
parent a1355ee766
commit 074fdeb212
4 changed files with 110 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
goog.provide('ol.test.coordinate');
goog.require('ol.coordinate');
goog.require('ol.geom.Circle');
describe('ol.coordinate', function() {
@@ -89,6 +90,19 @@ describe('ol.coordinate', function() {
});
});
describe('#closestOnCircle', function() {
var center = [5, 10];
var circle = new ol.geom.Circle(center, 10);
it('can find the closest point on circle', function() {
expect(ol.coordinate.closestOnCircle([-20, 10], circle))
.to.eql([-5, 10]);
});
it('can handle coordinate equal circle center', function() {
expect(ol.coordinate.closestOnCircle(center, circle))
.to.eql([15, 10]);
});
});
describe('#closestOnSegment', function() {
it('can handle points where the foot of the perpendicular is closest',
function() {

View File

@@ -4,6 +4,7 @@ goog.require('ol.Collection');
goog.require('ol.Feature');
goog.require('ol.Map');
goog.require('ol.View');
goog.require('ol.geom.Circle');
goog.require('ol.geom.Point');
goog.require('ol.geom.LineString');
goog.require('ol.interaction.Snap');
@@ -109,6 +110,27 @@ describe('ol.interaction.Snap', function() {
expect(event.coordinate).to.eql([10, 0]);
});
it('snaps to circle', function() {
var circle = new ol.Feature(new ol.geom.Circle([0, 0], 10));
var snapInteraction = new ol.interaction.Snap({
features: new ol.Collection([circle]),
pixelTolerance: 5
});
snapInteraction.setMap(map);
var event = {
pixel: [5 + width / 2, height / 2 - 5],
coordinate: [5, 5],
map: map
};
ol.interaction.Snap.handleEvent_.call(snapInteraction, event);
expect(event.coordinate).to.eql([
Math.sin(Math.PI / 4) * 10,
Math.sin(Math.PI / 4) * 10
]);
});
it('handle feature without geometry', function() {
var feature = new ol.Feature();
var snapInteraction = new ol.interaction.Snap({