Give points a getCentroid/centroid method.

This commit is contained in:
Marc Jansen
2012-06-22 16:15:49 +02:00
parent 1456e234e2
commit 09d7f15fb2
4 changed files with 109 additions and 17 deletions

View File

@@ -111,3 +111,12 @@ ol.geom.Point.prototype.projection = function(opt_arg){
return this.getProjection();
}
};
/**
* Returns the centroid of this point; which is a clone of the point itself.
*
* @return {ol.geom.Point} The centroid.
*/
ol.geom.Point.prototype.centroid = function() {
return this.getCentroid();
};

View File

@@ -144,3 +144,20 @@ ol.geom.Point.prototype._transform = function(proj) {
return new ol.geom.Point(point['x'], point['y'], this.z_, proj);
};
/**
* Returns the centroid of the point.
*
* @returns {ol.geom.Point} The centroid of the point.
*/
ol.geom.Point.prototype.getCentroid = function() {
return new ol.geom.Point(this.x_, this.y_, this.z_, this.projection_);
};
/**
* Returns the area of the geometry whcih is always 0.
*
* @returns {number} The area of the point (always 0).
*/
ol.geom.Point.prototype.getArea = function() {
return 0;
};

View File

@@ -166,4 +166,47 @@ describe("ol.geom.point", function() {
expect(p_obj.projection()).toEqual(jasmine.any(ol.Projection));
});
});
describe("the centroid method is functional", function(){
it("returns an instance of ol.geom.Point", function(){
expect(pNoZ_arr.centroid()).toBeA(ol.geom.Point);
expect(pWithZ_arr.centroid()).toBeA(ol.geom.Point);
expect(p_arr.centroid()).toBeA(ol.geom.Point);
expect(pNoZ_obj.centroid()).toBeA(ol.geom.Point);
expect(pWithZ_obj.centroid()).toBeA(ol.geom.Point);
expect(p_obj.centroid()).toBeA(ol.geom.Point);
});
it("does return a clone and not the point itself", function(){
expect(pNoZ_arr.centroid()).not.toBe(pNoZ_arr);
expect(pWithZ_arr.centroid()).not.toBe(pWithZ_arr);
expect(p_arr.centroid()).not.toBe(p_arr);
expect(pNoZ_obj.centroid()).not.toBe(pNoZ_obj);
expect(pWithZ_obj.centroid()).not.toBe(pWithZ_obj);
expect(p_obj.centroid()).not.toBe(p_obj);
});
it("has the expected coordinates", function(){
var c1 = pNoZ_arr.centroid(),
c2 = pWithZ_arr.centroid(),
c3 = p_arr.centroid(),
c4 = pNoZ_obj.centroid(),
c5 = pWithZ_obj.centroid(),
c6 = p_obj.centroid();
expect(c1.x() + ',' + c1.y()).toBe('21,4');
expect(c2.x() + ',' + c2.y()).toBe('21,4');
expect(c3.x() + ',' + c3.y()).toBe('21,4');
expect(c4.x() + ',' + c4.y()).toBe('21,4');
expect(c5.x() + ',' + c5.y()).toBe('21,4');
expect(c6.x() + ',' + c6.y()).toBe('21,4');
});
it("returns point(0,0) when the point was constructed without args", function(){
var c = pNoArgs.centroid();
expect(c.x() + ',' + c.y()).toBe('0,0');
});
});
});

View File

@@ -123,4 +123,27 @@ describe("ol.geom.Point", function() {
});
});
describe("the getCentroid method is functional", function(){
it("returns an instance of ol.geom.Point", function(){
expect(p2Args.getCentroid()).toBeA(ol.geom.Point);
expect(p3Args.getCentroid()).toBeA(ol.geom.Point);
expect(p4Args.getCentroid()).toBeA(ol.geom.Point);
});
it("does return a clone and not the point itself", function(){
expect(p2Args.getCentroid()).not.toBe(p2Args);
expect(p3Args.getCentroid()).not.toBe(p3Args);
expect(p4Args.getCentroid()).not.toBe(p4Args);
});
it("has the expected coordinates", function(){
var c2 = p2Args.getCentroid(),
c3 = p3Args.getCentroid(),
c4 = p4Args.getCentroid();
expect(c2.getX() + ',' + c2.getY()).toBe('21,4');
expect(c3.getX() + ',' + c3.getY()).toBe('21,4');
expect(c4.getX() + ',' + c4.getY()).toBe('21,4');
});
});
});