Merge pull request #3673 from ahocevar/draw-regular-polygon

More control over ol.interaction.Draw, to allow e.g. square drawing
This commit is contained in:
Andreas Hocevar
2015-05-20 12:08:15 +02:00
9 changed files with 394 additions and 110 deletions

View File

@@ -436,10 +436,44 @@ describe('ol.geom.Polygon', function() {
});
});
describe('ol.geom.Polygon.fromCircle', function() {
it('creates a regular polygon', function() {
var circle = new ol.geom.Circle([0, 0, 0], 1, ol.geom.GeometryLayout.XYZ);
var polygon = ol.geom.Polygon.fromCircle(circle);
var coordinates = polygon.getLinearRing(0).getCoordinates();
expect(coordinates[0].length).to.eql(3);
expect(coordinates[0][2]).to.eql(0);
expect(coordinates[32]).to.eql(coordinates[0]);
// east
expect(coordinates[0][0]).to.roughlyEqual(1, 1e-9);
expect(coordinates[0][1]).to.roughlyEqual(0, 1e-9);
// south
expect(coordinates[8][0]).to.roughlyEqual(0, 1e-9);
expect(coordinates[8][1]).to.roughlyEqual(1, 1e-9);
// west
expect(coordinates[16][0]).to.roughlyEqual(-1, 1e-9);
expect(coordinates[16][1]).to.roughlyEqual(0, 1e-9);
// north
expect(coordinates[24][0]).to.roughlyEqual(0, 1e-9);
expect(coordinates[24][1]).to.roughlyEqual(-1, 1e-9);
});
it('creates a regular polygon with custom sides and angle', function() {
var circle = new ol.geom.Circle([0, 0], 1);
var polygon = ol.geom.Polygon.fromCircle(circle, 4, Math.PI / 2);
var coordinates = polygon.getLinearRing(0).getCoordinates();
expect(coordinates[4]).to.eql(coordinates[0]);
expect(coordinates[0][0]).to.roughlyEqual(0, 1e-9);
expect(coordinates[0][1]).to.roughlyEqual(1, 1e-9);
});
});
});
goog.require('ol.extent');
goog.require('ol.geom.Circle');
goog.require('ol.geom.GeometryLayout');
goog.require('ol.geom.LinearRing');
goog.require('ol.geom.Polygon');

View File

@@ -708,6 +708,36 @@ describe('ol.interaction.Draw', function() {
});
});
describe('ol.interaction.Draw.createRegularPolygon', function() {
it('creates a regular polygon in Circle mode', function() {
var draw = new ol.interaction.Draw({
source: source,
type: ol.geom.GeometryType.CIRCLE,
geometryFunction:
ol.interaction.Draw.createRegularPolygon(4, Math.PI / 4)
});
map.addInteraction(draw);
// first point
simulateEvent('pointermove', 0, 0);
simulateEvent('pointerdown', 0, 0);
simulateEvent('pointerup', 0, 0);
// finish on second point
simulateEvent('pointermove', 20, 20);
simulateEvent('pointerdown', 20, 20);
simulateEvent('pointerup', 20, 20);
var features = source.getFeatures();
var geometry = features[0].getGeometry();
expect(geometry).to.be.a(ol.geom.Polygon);
var coordinates = geometry.getCoordinates();
expect(coordinates[0].length).to.eql(5);
expect(coordinates[0][0][0]).to.roughlyEqual(20, 1e-9);
expect(coordinates[0][0][1]).to.roughlyEqual(20, 1e-9);
});
});
});
goog.require('goog.dispose');