Accept flat coordinates in Circle constructor
This commit is contained in:
+27
-37
@@ -3,7 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
import {inherits} from '../util.js';
|
import {inherits} from '../util.js';
|
||||||
import {createOrUpdate, forEachCorner, intersects} from '../extent.js';
|
import {createOrUpdate, forEachCorner, intersects} from '../extent.js';
|
||||||
import GeometryLayout from '../geom/GeometryLayout.js';
|
|
||||||
import GeometryType from '../geom/GeometryType.js';
|
import GeometryType from '../geom/GeometryType.js';
|
||||||
import SimpleGeometry from '../geom/SimpleGeometry.js';
|
import SimpleGeometry from '../geom/SimpleGeometry.js';
|
||||||
import {deflateCoordinate} from '../geom/flat/deflate.js';
|
import {deflateCoordinate} from '../geom/flat/deflate.js';
|
||||||
@@ -13,16 +12,22 @@ import {deflateCoordinate} from '../geom/flat/deflate.js';
|
|||||||
* Circle geometry.
|
* Circle geometry.
|
||||||
*
|
*
|
||||||
* @constructor
|
* @constructor
|
||||||
* @extends {module:ol/geom/SimpleGeometry}
|
* @extends {!module:ol/geom/SimpleGeometry}
|
||||||
* @param {module:ol/coordinate~Coordinate} center Center.
|
* @param {!module:ol/coordinate~Coordinate} center Center. (For internal use,
|
||||||
|
* flat coordinates in combination with `opt_layout` and no `opt_radius` are
|
||||||
|
* also accepted.)
|
||||||
* @param {number=} opt_radius Radius.
|
* @param {number=} opt_radius Radius.
|
||||||
* @param {module:ol/geom/GeometryLayout=} opt_layout Layout.
|
* @param {module:ol/geom/GeometryLayout=} opt_layout Layout.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
const Circle = function(center, opt_radius, opt_layout) {
|
const Circle = function(center, opt_radius, opt_layout) {
|
||||||
SimpleGeometry.call(this);
|
SimpleGeometry.call(this);
|
||||||
const radius = opt_radius ? opt_radius : 0;
|
if (opt_layout !== undefined && opt_radius === undefined) {
|
||||||
this.setCenterAndRadius(center, radius, opt_layout);
|
this.setFlatCoordinatesInternal(opt_layout, center);
|
||||||
|
} else {
|
||||||
|
const radius = opt_radius ? opt_radius : 0;
|
||||||
|
this.setCenterAndRadius(center, radius, opt_layout);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
inherits(Circle, SimpleGeometry);
|
inherits(Circle, SimpleGeometry);
|
||||||
@@ -35,9 +40,7 @@ inherits(Circle, SimpleGeometry);
|
|||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
Circle.prototype.clone = function() {
|
Circle.prototype.clone = function() {
|
||||||
const circle = new Circle(null);
|
return new Circle(this.flatCoordinates.slice(), undefined, this.layout);
|
||||||
circle.setFlatCoordinates(this.layout, this.flatCoordinates.slice());
|
|
||||||
return circle;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -170,37 +173,34 @@ Circle.prototype.setCenter = function(center) {
|
|||||||
for (let i = 1; i < stride; ++i) {
|
for (let i = 1; i < stride; ++i) {
|
||||||
flatCoordinates[stride + i] = center[i];
|
flatCoordinates[stride + i] = center[i];
|
||||||
}
|
}
|
||||||
this.setFlatCoordinates(this.layout, flatCoordinates);
|
this.setFlatCoordinatesInternal(this.layout, flatCoordinates);
|
||||||
|
this.changed();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the center (as {@link module:ol/coordinate~Coordinate coordinate}) and the radius (as
|
* Set the center (as {@link module:ol/coordinate~Coordinate coordinate}) and the radius (as
|
||||||
* number) of the circle.
|
* number) of the circle.
|
||||||
* @param {module:ol/coordinate~Coordinate} center Center.
|
* @param {!module:ol/coordinate~Coordinate} center Center.
|
||||||
* @param {number} radius Radius.
|
* @param {number} radius Radius.
|
||||||
* @param {module:ol/geom/GeometryLayout=} opt_layout Layout.
|
* @param {module:ol/geom/GeometryLayout=} opt_layout Layout.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
Circle.prototype.setCenterAndRadius = function(center, radius, opt_layout) {
|
Circle.prototype.setCenterAndRadius = function(center, radius, opt_layout) {
|
||||||
if (!center) {
|
this.setLayout(opt_layout, center, 0);
|
||||||
this.setFlatCoordinates(GeometryLayout.XY, null);
|
if (!this.flatCoordinates) {
|
||||||
} else {
|
this.flatCoordinates = [];
|
||||||
this.setLayout(opt_layout, center, 0);
|
|
||||||
if (!this.flatCoordinates) {
|
|
||||||
this.flatCoordinates = [];
|
|
||||||
}
|
|
||||||
/** @type {Array.<number>} */
|
|
||||||
const flatCoordinates = this.flatCoordinates;
|
|
||||||
let offset = deflateCoordinate(
|
|
||||||
flatCoordinates, 0, center, this.stride);
|
|
||||||
flatCoordinates[offset++] = flatCoordinates[0] + radius;
|
|
||||||
for (let i = 1, ii = this.stride; i < ii; ++i) {
|
|
||||||
flatCoordinates[offset++] = flatCoordinates[i];
|
|
||||||
}
|
|
||||||
flatCoordinates.length = offset;
|
|
||||||
this.changed();
|
|
||||||
}
|
}
|
||||||
|
/** @type {Array.<number>} */
|
||||||
|
const flatCoordinates = this.flatCoordinates;
|
||||||
|
let offset = deflateCoordinate(
|
||||||
|
flatCoordinates, 0, center, this.stride);
|
||||||
|
flatCoordinates[offset++] = flatCoordinates[0] + radius;
|
||||||
|
for (let i = 1, ii = this.stride; i < ii; ++i) {
|
||||||
|
flatCoordinates[offset++] = flatCoordinates[i];
|
||||||
|
}
|
||||||
|
flatCoordinates.length = offset;
|
||||||
|
this.changed();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -216,16 +216,6 @@ Circle.prototype.getCoordinates = function() {};
|
|||||||
Circle.prototype.setCoordinates = function(coordinates, opt_layout) {};
|
Circle.prototype.setCoordinates = function(coordinates, opt_layout) {};
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {module:ol/geom/GeometryLayout} layout Layout.
|
|
||||||
* @param {Array.<number>} flatCoordinates Flat coordinates.
|
|
||||||
*/
|
|
||||||
Circle.prototype.setFlatCoordinates = function(layout, flatCoordinates) {
|
|
||||||
this.setFlatCoordinatesInternal(layout, flatCoordinates);
|
|
||||||
this.changed();
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the radius of the circle. The radius is in the units of the projection.
|
* Set the radius of the circle. The radius is in the units of the projection.
|
||||||
* @param {number} radius Radius.
|
* @param {number} radius Radius.
|
||||||
|
|||||||
@@ -173,18 +173,11 @@ describe('ol.geom.Circle', function() {
|
|||||||
describe('#setFlatCoordinates', function() {
|
describe('#setFlatCoordinates', function() {
|
||||||
|
|
||||||
it('sets both center and radius', function() {
|
it('sets both center and radius', function() {
|
||||||
circle.setFlatCoordinates('XY', [1, 2, 4, 2]);
|
circle.setFlatCoordinatesInternal('XY', [1, 2, 4, 2]);
|
||||||
expect(circle.getCenter()).to.eql([1, 2]);
|
expect(circle.getCenter()).to.eql([1, 2]);
|
||||||
expect(circle.getRadius()).to.be(3);
|
expect(circle.getRadius()).to.be(3);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fires a single change event', function() {
|
|
||||||
const spy = sinon.spy();
|
|
||||||
circle.on('change', spy);
|
|
||||||
circle.setFlatCoordinates('XY', [1, 2, 4, 2]);
|
|
||||||
expect(spy.calledOnce).to.be(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#setRadius', function() {
|
describe('#setRadius', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user