Merge pull request #9416 from fredj/circle_rotate

Implement rotate and translate functions for circle geometry
This commit is contained in:
Frédéric Junod
2019-04-15 08:22:29 +02:00
committed by GitHub
2 changed files with 53 additions and 0 deletions

View File

@@ -5,6 +5,8 @@ import {createOrUpdate, forEachCorner, intersects} from '../extent.js';
import GeometryType from './GeometryType.js';
import SimpleGeometry from './SimpleGeometry.js';
import {deflateCoordinate} from './flat/deflate.js';
import {rotate, translate} from './flat/transform.js';
/**
* @classdesc
@@ -212,6 +214,29 @@ class Circle extends SimpleGeometry {
this.flatCoordinates[this.stride] = this.flatCoordinates[0] + radius;
this.changed();
}
/**
* @inheritDoc
* @api
*/
rotate(angle, anchor) {
const center = this.getCenter();
const stride = this.getStride();
this.setCenter(rotate(center, 0, center.length, stride, angle, anchor, center));
this.changed();
}
/**
* @inheritDoc
* @api
*/
translate(deltaX, deltaY) {
const center = this.getCenter();
const stride = this.getStride();
this.setCenter(translate(center, 0, center.length, stride, deltaX, deltaY, center));
this.changed();
}
}

View File

@@ -263,6 +263,34 @@ describe('ol.geom.Circle', function() {
});
describe('#rotate', function() {
it('rotates the center around the anchor', function() {
circle.setCenter([1, 0]);
circle.rotate(Math.PI / 2, [2, 0]);
expect(circle.getCenter()).to.eql([2, -1]);
expect(circle.getExtent()).to.eql([1, -2, 3, 0]);
});
it('does not change if the anchor equals the center', function() {
const center = [1, 0];
circle.setCenter(center);
const extent = circle.getExtent();
circle.rotate(Math.PI / 2, center);
expect(circle.getCenter()).to.eql(center);
expect(circle.getExtent()).to.eql(extent);
});
});
describe('#translate', function() {
it('translates the circle', function() {
circle.setCenter([1, 1]);
circle.translate(5, 10);
expect(circle.getCenter()).to.eql([6, 11]);
expect(circle.getExtent()).to.eql([5, 10, 7, 12]);
});
});
});
});