Add a method to scale geometries

This commit is contained in:
Tim Schaub
2016-08-04 10:23:00 -06:00
parent f50f1f401c
commit 795cee876e
8 changed files with 184 additions and 0 deletions

View File

@@ -60,6 +60,39 @@ ol.geom.flat.transform.rotate = function(flatCoordinates, offset, end, stride, a
};
/**
* Scale the coordinates.
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
* @param {number} end End.
* @param {number} stride Stride.
* @param {number} sx Scale factor in the x-direction.
* @param {number} sy Scale factor in the y-direction.
* @param {Array.<number>} anchor Scale anchor point.
* @param {Array.<number>=} opt_dest Destination.
* @return {Array.<number>} Transformed coordinates.
*/
ol.geom.flat.transform.scale = function(flatCoordinates, offset, end, stride, sx, sy, anchor, opt_dest) {
var dest = opt_dest ? opt_dest : [];
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 + sx * deltaX;
dest[i++] = anchorY + sy * deltaY;
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

@@ -180,6 +180,20 @@ ol.geom.Geometry.prototype.getExtent = function(opt_extent) {
ol.geom.Geometry.prototype.rotate = function(angle, anchor) {};
/**
* Scale the geometry (with an optional origin). This modifies the geometry
* coordinates in place.
* @abstract
* @param {number} sx The scaling factor in the x-direction.
* @param {number=} opt_sy The scaling factor in the y-direction (defaults to
* sx).
* @param {ol.Coordinate=} opt_anchor The scale origin (defaults to the center
* of the geometry extent).
* @api
*/
ol.geom.Geometry.prototype.scale = function(sx, opt_sy, opt_anchor) {};
/**
* Create a simplified version of this geometry. For linestrings, this uses
* the the {@link

View File

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

View File

@@ -278,6 +278,30 @@ ol.geom.SimpleGeometry.prototype.rotate = function(angle, anchor) {
};
/**
* @inheritDoc
* @api
*/
ol.geom.SimpleGeometry.prototype.scale = function(sx, opt_sy, opt_anchor) {
var sy = opt_sy;
if (sy === undefined) {
sy = sx;
}
var anchor = opt_anchor;
if (!anchor) {
anchor = ol.extent.getCenter(this.getExtent());
}
var flatCoordinates = this.getFlatCoordinates();
if (flatCoordinates) {
var stride = this.getStride();
ol.geom.flat.transform.scale(
flatCoordinates, 0, flatCoordinates.length,
stride, sx, sy, anchor, flatCoordinates);
this.changed();
}
};
/**
* @inheritDoc
* @api stable