Skip to content

Commit

Permalink
[es6] Classes
Browse files Browse the repository at this point in the history
  • Loading branch information
felipernb committed Jun 14, 2017
1 parent 4eaa6f9 commit ff600d5
Show file tree
Hide file tree
Showing 16 changed files with 1,371 additions and 1,341 deletions.
74 changes: 38 additions & 36 deletions src/algorithms/geometry/bezier_curve.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,51 @@
* Generates a bezier-curve from a series of points
* @param Array array of control points ([{x: x0, y: y0}, {x: x1, y: y1}])
*/
const BezierCurve = function(points) {
this.n = points.length;
this.p = [];
class BezierCurve {
constructor(points) {
this.n = points.length;
this.p = [];

// The binomial coefficient
const c = [1];
let i;
let j;
for (i = 1; i < this.n; ++i) {
c.push(0);
for (j = i; j >= 1; --j) {
c[j] += c[j - 1];
// The binomial coefficient
const c = [1];
let i;
let j;
for (i = 1; i < this.n; ++i) {
c.push(0);
for (j = i; j >= 1; --j) {
c[j] += c[j - 1];
}
}
}

// the i-th control point times the coefficient
for (i = 0; i < this.n; ++i) {
this.p.push({x: c[i] * points[i].x, y: c[i] * points[i].y});
// the i-th control point times the coefficient
for (i = 0; i < this.n; ++i) {
this.p.push({x: c[i] * points[i].x, y: c[i] * points[i].y});
}
}
};

/**
* @param Number float variable from 0 to 1
*/
BezierCurve.prototype.get = function(t) {
const res = {x: 0, y: 0};
let i;
let a = 1;
let b = 1;
/**
* @param Number float variable from 0 to 1
*/
get(t) {
const res = {x: 0, y: 0};
let i;
let a = 1;
let b = 1;

// The coefficient
const c = [];
for (i = 0; i < this.n; ++i) {
c.push(a);
a *= t;
}
// The coefficient
const c = [];
for (i = 0; i < this.n; ++i) {
c.push(a);
a *= t;
}

for (i = this.n - 1; i >= 0; --i) {
res.x += this.p[i].x * c[i] * b;
res.y += this.p[i].y * c[i] * b;
b *= 1 - t;
for (i = this.n - 1; i >= 0; --i) {
res.x += this.p[i].x * c[i] * b;
res.y += this.p[i].y * c[i] * b;
b *= 1 - t;
}
return res;
}
return res;
};
}

module.exports = BezierCurve;
2 changes: 1 addition & 1 deletion src/algorithms/math/power_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const powerSetRecursive = array => {
powerSetRecursive(array).forEach(elem => {
powerSet.push(elem);
const withFirstElem = [firstElem];
withFirstElem.push.apply(withFirstElem, elem);
withFirstElem.push(...elem);
powerSet.push(withFirstElem);
});

Expand Down
Loading

0 comments on commit ff600d5

Please sign in to comment.