Yet another approach. A bit more code, but more prototypical and solid.
// Common point class
/**
* Creates simple point class
* Point can be passed as two separate coordinates or as an array (coordinates pair). In case of array, second argument is omitted.
* @param {String|Number|Array} a
* @param {String|Number|undefined} b
* @returns {Point}
*/
var Point = function (a, b) {
if (!!a && !!b) {
this.a = parseFloat(a);
this.b = parseFloat(b);
} else if (a.constructor === Array ? a.length === 2 : false) {
this.a = parseFloat(a[0]);
this.b = parseFloat(a[1]);
} else {
throw 'Wrong data provided for `Point`';
}
}
/**
* @returns {Array} Rounded coordinates pair
*/
Point.prototype.round = function () {
return [Math.round(this.a), Math.round(this.b)];
}
/**
* @returns {Array} Raw coordinates pair (as they were passed to constructor)
*/
Point.prototype.value = function () {
return [this.a, this.b];
}
// Bezier point class
/**
* Creates a Bezier point instance
* @param {Array|Point} point
* @param {Array|Point} left
* @param {Array|Point} right
* @returns {BezierPoint}
*/
var BezierPoint = function (point, left, right) {
this.p = point instanceof Point ? point : new Point(point);
this.l = left instanceof Point ? left : new Point(left);
this.r = right instanceof Point ? right : new Point(right);
}
// Operation
var bezierPoint = new BezierPoint([0.0,0.0], [-50.43794, 0.0], [25.54714,4.78643]);
Each point can be passed to BezierPoint as an array or already well-formed Point class.
UPD: As an extension for existing answer ability to define arbitrary number of points can be provided next way.
var ArbitraryNumberOfPoints = function (args) {
this.points = args.length === 0 ? [] :
args.map(function (arg) {
return arg instanceof Point ? arg : new Point(arg);
});
}
ArbitraryNumberOfPoints.prototype.round = function () {
return this.points.map(function (pointInstance) {
return pointInstance.round();
});
}
prototype?bezierPoint.lseems to return an array, the only way to get what you want is to add to the Array constructor, which you probably shouldn'tPoint.getRoundedthat would return a roundedtop,leftandright.bezierPoint.getRoundedto return[0,0], [-50, 0], [26, 5].bezierPoint.getRounded(p)instead of 'bezierPoint.p.getRounded()'