Add new ol.geom.Geometry#rotate function

This commit is contained in:
Frederic Junod
2016-03-07 15:27:55 +01:00
parent 6c74e90c5d
commit d5564a04db
5 changed files with 117 additions and 0 deletions

View File

@@ -35,6 +35,39 @@ ol.geom.flat.transform.transform2D = function(flatCoordinates, offset, end, stri
};
/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
* @param {number} end End.
* @param {number} stride Stride.
* @param {number} angle Angle.
* @param {Array.<number>} anchor Rotation anchor point.
* @param {Array.<number>=} opt_dest Destination.
* @return {Array.<number>} Transformed coordinates.
*/
ol.geom.flat.transform.rotate = function(flatCoordinates, offset, end, stride, angle, anchor, opt_dest) {
var dest = opt_dest ? opt_dest : [];
var cos = Math.cos(angle);
var sin = Math.sin(angle);
var anchorX = anchor[0];
var anchorY = anchor[1];
var i = 0;
for (var j = offset; j < end; j += stride) {
var deltaX = flatCoordinates[j] - anchorX;
var deltaY = flatCoordinates[j + 1] - anchorY;
dest[i++] = anchorX + deltaX * cos - deltaY * sin;
dest[i++] = anchorY + deltaX * sin + deltaY * cos;
for (var k = j + 2; k < j + stride; ++k) {
dest[i++] = flatCoordinates[k];
}
}
if (opt_dest && dest.length != i) {
dest.length = i;
}
return dest;
};
/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.

View File

@@ -169,6 +169,16 @@ ol.geom.Geometry.prototype.getExtent = function(opt_extent) {
};
/**
* Rotate the geometry around a given coordinate. This modifies the geometry
* coordinates in place.
* @param {number} angle Rotation angle in radians.
* @param {ol.Coordinate} anchor The rotation center.
* @api
*/
ol.geom.Geometry.prototype.rotate = goog.abstractMethod;
/**
* Create a simplified version of this geometry. For linestrings, this uses
* the the {@link

View File

@@ -231,6 +231,19 @@ ol.geom.GeometryCollection.prototype.isEmpty = function() {
};
/**
* @inheritDoc
* @api
*/
ol.geom.GeometryCollection.prototype.rotate = function(angle, anchor) {
var geometries = this.geometries_;
for (var i = 0, ii = geometries.length; i < ii; ++i) {
geometries[i].rotate(angle, anchor);
}
this.changed();
};
/**
* Set the geometries that make up this geometry collection.
* @param {Array.<ol.geom.Geometry>} geometries Geometries.

View File

@@ -260,6 +260,22 @@ ol.geom.SimpleGeometry.prototype.applyTransform = function(transformFn) {
};
/**
* @inheritDoc
* @api
*/
ol.geom.SimpleGeometry.prototype.rotate = function(angle, anchor) {
var flatCoordinates = this.getFlatCoordinates();
if (flatCoordinates) {
var stride = this.getStride();
ol.geom.flat.transform.rotate(
flatCoordinates, 0, flatCoordinates.length,
stride, angle, anchor, flatCoordinates);
this.changed();
}
};
/**
* @inheritDoc
* @api stable