1
1
import canvas from './draw.js' ;
2
2
3
3
const canvasSize = 1000 ;
4
+ const domainMargin = 1 ;
4
5
5
6
function radioValue ( name ) {
6
7
return document . querySelector ( `input[type=radio][name=${ name } ]:checked` ) . value ;
7
8
}
8
- function value ( name ) {
9
- return document . querySelector ( `input[name=${ name } ]` ) . value ;
9
+ function intValue ( name ) {
10
+ return parseInt ( document . querySelector ( `input[name=${ name } ]` ) . value ) ;
10
11
}
11
12
12
13
function range ( stop ) {
@@ -25,17 +26,21 @@ let ip = []; // interpolated points
25
26
// randomly generate control points
26
27
function runGeneration ( ) {
27
28
const margin = 0.2 ;
28
- cp0 = range ( 10 ) . map ( i => [
29
- canvasSize * margin + Math . random ( ) * canvasSize * ( 1 - 2 * margin ) ,
30
- canvasSize * margin + Math . random ( ) * canvasSize * ( 1 - 2 * margin )
31
- ] ) ;
32
- // console.log(cp);
29
+ cp0 = range ( 10 ) . map ( i => {
30
+ let p = [
31
+ canvasSize * margin + Math . random ( ) * canvasSize * ( 1 - 2 * margin ) ,
32
+ canvasSize * margin + Math . random ( ) * canvasSize * ( 1 - 2 * margin )
33
+ ] ;
34
+ p . param = i ; // save parameter
35
+ return p ;
36
+ } ) ;
37
+ // console.log(cp0);
33
38
}
34
39
35
40
// calculate intermediate points
36
41
function runInterpolation ( ) {
37
- let num = value ( 'points' ) ;
38
- let res = value ( 'intermediates' ) ;
42
+ let num = intValue ( 'points' ) ; // number of control points
43
+ let int = intValue ( 'intermediates' ) ; // number of intermediates (between two adjacent control points)
39
44
let method = radioValue ( 'method' ) ;
40
45
let clip = radioValue ( 'clip' ) ;
41
46
let cubicTension = radioValue ( 'cubicTension' ) ;
@@ -46,14 +51,24 @@ function runInterpolation() {
46
51
ip = [ ] ;
47
52
let s = Smooth ( cp , { method, clip, cubicTension, sincFilterSize, sincWindow } ) ;
48
53
49
- let pcs = cp . length - 1 ; // number of curve pieces
50
- for ( let p = - 1 ; p < cp . length ; p ++ ) {
51
- for ( let i = 0 ; i < res ; i ++ ) {
52
- let u = p + i / res ;
53
- ip . push ( s ( u ) ) ;
54
- }
54
+ num += 2 * domainMargin ;
55
+ let pieces = num - 1 ; // number of curve pieces
56
+ let total = num + pieces * int ; // total number of points/values
57
+ let step = pieces / ( total - 1 ) ;
58
+
59
+ // console.log({
60
+ // points: num,
61
+ // intermediates: int,
62
+ // total
63
+ // });
64
+
65
+ for ( let i = 0 ; i < total ; i ++ ) {
66
+ let u = i * step - domainMargin ; // parameter value
67
+ let v = s ( u ) ; // interpolated value
68
+ v . param = u ; // save parameter
69
+ ip . push ( v ) ;
55
70
}
56
- // console.log(ip);
71
+ console . log ( ip ) ;
57
72
}
58
73
59
74
function drawCurve ( scanvas , cp , ip ) {
@@ -68,18 +83,14 @@ function drawMain() {
68
83
}
69
84
70
85
function drawSide ( ) {
71
- let du = cp . length - 1 + 2 ; // length of parameter domain
72
- let sidecp = cp . map ( ( p , i ) => [
73
- ( canvasSize / du ) * ( i + 1 ) ,
74
- p [ 1 ]
75
- ] ) ;
76
- let sideip = ip . map ( ( p , i ) => [
77
- canvasSize / ( ip . length - 1 ) * i ,
78
- p [ 1 ]
79
- ] ) ;
86
+ let dlen = cp . length - 1 + 2 * domainMargin ; // length of parameter domain
87
+ let p2x = p => canvasSize / dlen * ( p + domainMargin ) ; // parameter to x value
88
+
89
+ let sidecp = cp . map ( p => [ p2x ( p . param ) , p [ 1 ] ] ) ;
90
+ let sideip = ip . map ( p => [ p2x ( p . param ) , p [ 1 ] ] ) ;
80
91
drawCurve ( side , sidecp , sideip ) ;
81
- side . lines ( [ [ canvasSize / du , 0 ] , [ canvasSize / du , canvasSize ] ] , 'lightgray' , 1 ) ;
82
- side . lines ( [ [ canvasSize / du * ( sidecp . length ) , 0 ] , [ canvasSize / du * ( sidecp . length ) , canvasSize ] ] , 'lightgray' , 1 ) ;
92
+ side . lines ( [ [ p2x ( 0 ) , 0 ] , [ p2x ( 0 ) , canvasSize ] ] , 'lightgray' , 1 ) ;
93
+ side . lines ( [ [ p2x ( cp . length - 1 ) , 0 ] , [ p2x ( cp . length - 1 ) , canvasSize ] ] , 'lightgray' , 1 ) ;
83
94
}
84
95
85
96
function generate ( ) {
0 commit comments