Skip to content

Commit dc0ee73

Browse files
committed
Merge pull request #111 from quietshu/master
Geometry, basic Bézier-curve algorithm (and its test)
2 parents 4bd1b6d + f706569 commit dc0ee73

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
3+
/**
4+
* 2D bezier-curve, https://en.wikipedia.org/wiki/B%C3%A9zier_curve
5+
* Usage:
6+
* var b = new BezierCurve([ [0, 0], [10, 3] ]);
7+
* b.get(0.5); // [5, 1.5]
8+
*/
9+
10+
/**
11+
* Generates a bezier-curve from a series of points
12+
* @param Array array of control points ([[x0, y0], [x1, y1]])
13+
*/
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+
}
25+
}
26+
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;
35+
};
36+
37+
/**
38+
* @param Number float variable from 0 to 1
39+
*/
40+
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;
57+
};
58+
59+
module.exports = BezierCurve;

src/geometry.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
// Geometry algorithms
4+
module.exports = {
5+
bezierCurve: require('./algorithms/geometry/bezier_curve')
6+
};

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var lib = {
44
DataStructures: require('./data_structures'),
55
Graph: require('./graph'),
6+
Geometry: require('./geometry'),
67
Math: require('./math'),
78
Search: require('./search'),
89
Sorting: require('./sorting'),
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
var root = require('../../../'),
4+
BezierCurve = root.Geometry.bezierCurve,
5+
assert = require('assert');
6+
7+
// Testing with http://pomax.github.io/bezierjs/
8+
9+
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+
});
35+
});

0 commit comments

Comments
 (0)