-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.js
173 lines (138 loc) · 3.78 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
const PLOT_HEIGHT = 500;
window.addEventListener('load', () => {
let graph = new Graph("graph");
let bezierCurve = new BezierCurve([
new Point(10, 10),
new Point(70, 310),
new Point(210, 210),
new Point(210, 10),
// new Point(350, 50),
// new Point(200, 290),
// new Point(400, 80),
// new Point(420, 100)
]);
drawHandles(graph, bezierCurve);
graph.drawCurveFromPoints(bezierCurve.drawingPoints);
});
/*
* Class : Point( x coordinate, y coordinate )
* -------------------------------------------
* Represents a single point on the plot.
*/
class Point {
constructor(x = 0, y = 0) {
this.x = x;
/*
The reason to the following is because we
want the origin to be at bottom left corner
instead of the top left.
*/
this.y = PLOT_HEIGHT - y;
}
x() {
return this.x;
}
y() {
return this.y;
}
mark() {
document.getElementById('graph').insertAdjacentHTML('beforeend', `<circle cx="${this.x}" cy="${this.y}" r="5" fill="#000" />`);
}
}
/*
* Class : BezierCurve( Collection of n points for a curve of degree n)
* ------------------------------------------------------------------
* Represents a Bezier curve, the number of points passed in the con-
* structor determine the degree of the curve.
*/
class BezierCurve {
constructor(points) {
if ( points instanceof Point ) {
this.points = [];
for ( let i = 0; i < arguments.length; i++ ) {
if ( arguments[i] instanceof Point ) {
this.points.push(arguments[i]);
}
}
} else if ( typeof points === 'object' ) {
this.points = points;
} else {
this.points = [];
}
// Drawing points are the number of points that render the curve,
// the more the number of drawing points, smoother the curve.
this.numDrawingPoints = 100;
this.drawingPoints = [];
this.calculateDrawingPoints();
}
calculateDrawingPoints() {
let interval = 1 / this.numDrawingPoints;
let t = interval;
this.drawingPoints.push(this.calculateNewPoint(0));
for( let i = 0; i < this.numDrawingPoints; i++ ) {
this.drawingPoints.push(this.calculateNewPoint(t));
t += interval;
}
}
calculateNewPoint(t) {
// Coordinates calculated using the general formula are relative to
// origin at bottom left.
let x = 0;
let y = 0;
let n = this.points.length - 1;
for ( let i = 0; i <= n; i++ ) {
let bin = C(n, i) * Math.pow((1-t), (n-i)) * Math.pow(t, i);
x += bin * this.points[i].x;
y += bin * this.points[i].y;
}
return (new Point(x, PLOT_HEIGHT - y));
}
}
/*
* Class : Graph(id of the svg container)
* -------------------------------------------
* Represents a graph and exports methods for
* drawing lines and curves.
*/
class Graph {
constructor(id) {
this.el = document.getElementById(id);
}
drawLine(point1, point2, stroke = 2, color = '#000000') {
this.el.insertAdjacentHTML('beforeend', `<line x1="${point1.x}" y1="${point1.y}" x2="${point2.x}" y2="${point2.y}" stroke="${color}" stroke-width="${stroke}" id="line"/>`);
}
drawCurveFromPoints(points) {
for ( let i = 0; i < points.length; i++ ) {
if ( i+1 < points.length )
this.drawLine(points[i], points[i+1]);
}
}
}
// Utilty functions
function drawHandles(graph, curve) {
if (curve.points.length === 1) {
curve.points[0].mark();
return;
}
for (let i = 1; i < curve.points.length; i++) {
if (i == 1 || i == curve.points.length-1) {
curve.points[i-1].mark();
curve.points[i].mark();
}
graph.drawLine(curve.points[i-1], curve.points[i], 1, (i==1||i==curve.points.length-1)?'#00FF00':'#AA4444');
}
if (curve.points.length === 1) {
curve.points[0].mark();
return;
}
}
function C(n, k) {
if ( (typeof n !== 'number') || (typeof k !== 'number') ) {
return false;
}
var coeff = 1;
for ( var x = n-k+1; x <= n; x++ ) coeff *= x;
for ( x = 1; x <= k; x++ ) coeff /= x;
return coeff;
}