Skip to content

Commit f3737b1

Browse files
committed
Use objects instead of array to represent points in Bezier-Curve
1 parent dc0ee73 commit f3737b1

File tree

2 files changed

+66
-63
lines changed

2 files changed

+66
-63
lines changed

src/algorithms/geometry/bezier_curve.js

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,55 @@
33
/**
44
* 2D bezier-curve, https://en.wikipedia.org/wiki/B%C3%A9zier_curve
55
* Usage:
6-
* var b = new BezierCurve([ [0, 0], [10, 3] ]);
7-
* b.get(0.5); // [5, 1.5]
6+
* var b = new BezierCurve([{x: 0, y: 0}, {x: 10, y: 3}]);
7+
* b.get(0.5); // {x: 5, y: 1.5}
88
*/
99

1010
/**
1111
* Generates a bezier-curve from a series of points
12-
* @param Array array of control points ([[x0, y0], [x1, y1]])
12+
* @param Array array of control points ([{x: x0, y: y0}, {x: x1, y: y1}])
1313
*/
14-
var BezierCurve = function (array) {
15-
var n = array.length;
16-
17-
// The binomial coefficient
18-
var c = [1];
19-
var i, j;
20-
for (i = 1; i < n; ++i) {
21-
c.push(0);
22-
for (j = i; j >= 1; --j) {
23-
c[j] += c[j - 1];
24-
}
14+
var BezierCurve = function (points) {
15+
this.n = points.length;
16+
this.p = [];
17+
18+
// The binomial coefficient
19+
var c = [1];
20+
var i, j;
21+
for (i = 1; i < this.n; ++i) {
22+
c.push(0);
23+
for (j = i; j >= 1; --j) {
24+
c[j] += c[j - 1];
2525
}
26+
}
2627

27-
// the i-th control point times the coefficient
28-
var p = [];
29-
for (i = 0; i < n; ++i) {
30-
p.push([c[i] * array[i][0], c[i] * array[i][1]]);
31-
}
32-
33-
this.p = p;
34-
this.n = n;
28+
// the i-th control point times the coefficient
29+
for (i = 0; i < this.n; ++i) {
30+
this.p.push({x: c[i] * points[i].x, y: c[i] * points[i].y});
31+
}
3532
};
3633

3734
/**
3835
* @param Number float variable from 0 to 1
3936
*/
4037
BezierCurve.prototype.get = function (t) {
41-
var res = [0, 0], i;
42-
var a = 1, b = 1;
43-
44-
// The coefficient
45-
var c = [];
46-
for (i = 0; i < this.n; ++i) {
47-
c.push(a);
48-
a *= t;
49-
}
50-
51-
for (i = this.n - 1; i >= 0; --i) {
52-
res[0] += this.p[i][0] * c[i] * b;
53-
res[1] += this.p[i][1] * c[i] * b;
54-
b *= 1 - t;
55-
}
56-
return res;
38+
var res = {x: 0, y: 0};
39+
var i;
40+
var a = 1, b = 1;
41+
42+
// The coefficient
43+
var c = [];
44+
for (i = 0; i < this.n; ++i) {
45+
c.push(a);
46+
a *= t;
47+
}
48+
49+
for (i = this.n - 1; i >= 0; --i) {
50+
res.x += this.p[i].x * c[i] * b;
51+
res.y += this.p[i].y * c[i] * b;
52+
b *= 1 - t;
53+
}
54+
return res;
5755
};
5856

5957
module.exports = BezierCurve;

src/test/algorithms/geometry/bezier_curve.js

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,34 @@ var root = require('../../../'),
77
// Testing with http://pomax.github.io/bezierjs/
88

99
describe('Bézier-Curve Algorithm', function () {
10-
it('should get a linear Bézier-curve', function () {
11-
var b = new BezierCurve([[0, 0], [10, 3]]);
12-
13-
// Ends
14-
assert.deepEqual(b.get(0), [0, 0]);
15-
assert.deepEqual(b.get(1), [10, 3]);
16-
17-
// Middle
18-
assert.deepEqual(b.get(0.5), [5, 1.5]);
19-
20-
// 1/4 and 3/4
21-
assert.deepEqual(b.get(0.25), [2.5, 0.75]);
22-
assert.deepEqual(b.get(0.75), [7.5, 2.25]);
23-
});
24-
it('should get a quadratic Bézier-curve', function () {
25-
var b = new BezierCurve([[150, 40], [80, 30], [105, 150]]);
26-
27-
assert.deepEqual(b.get(0.5), [103.75, 62.5]);
28-
assert.deepEqual(b.get(0.25), [120.9375, 43.125]);
29-
});
30-
it('should get a cubic Bézier-curve', function () {
31-
var b = new BezierCurve([[150, 40], [80, 30], [105, 150], [100, 100]]);
32-
33-
assert.deepEqual(b.get(0.5), [100.625, 85]);
34-
});
10+
it('should get a linear Bézier-curve', function () {
11+
var b = new BezierCurve([{x: 0, y: 0}, {x: 10, y: 3}]);
12+
13+
// Ends
14+
assert.deepEqual(b.get(0), {x: 0, y: 0});
15+
assert.deepEqual(b.get(1), {x: 10, y: 3});
16+
17+
// Middle
18+
assert.deepEqual(b.get(0.5), {x: 5, y: 1.5});
19+
20+
// 1/4 and 3/4
21+
assert.deepEqual(b.get(0.25), {x: 2.5, y: 0.75});
22+
assert.deepEqual(b.get(0.75), {x: 7.5, y: 2.25});
23+
});
24+
it('should get a quadratic Bézier-curve', function () {
25+
var b = new BezierCurve([{x: 150, y: 40},
26+
{x: 80, y: 30},
27+
{x: 105, y: 150}]);
28+
29+
assert.deepEqual(b.get(0.5), {x: 103.75, y: 62.5});
30+
assert.deepEqual(b.get(0.25), {x: 120.9375, y: 43.125});
31+
});
32+
it('should get a cubic Bézier-curve', function () {
33+
var b = new BezierCurve([{x: 150, y: 40},
34+
{x: 80, y: 30},
35+
{x: 105, y: 150},
36+
{x: 100, y: 100}]);
37+
38+
assert.deepEqual(b.get(0.5), {x: 100.625, y: 85});
39+
});
3540
});

0 commit comments

Comments
 (0)