-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathbuiltIn.ts
More file actions
194 lines (175 loc) · 6.42 KB
/
Copy pathbuiltIn.ts
File metadata and controls
194 lines (175 loc) · 6.42 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { linspace, sgn, infinity, clamp, space, isValidNumber } from '../utils.mjs'
import { builtIn as builtInEvaluate } from './eval.mjs'
import type { FunctionPlotDatum, FunctionPlotScale, PointFunction, VectorFunction } from '../types.js'
import type { SamplerParams, SamplerFn, BuiltInSamplerResult, BuiltInSamplerResultGroup } from './types.js'
type Asymptote = {
asymptote: boolean
d0: [number, number]
d1: [number, number]
}
function checkAsymptote(
d0: [number, number],
d1: [number, number],
d: FunctionPlotDatum,
sign: number,
level: number
): Asymptote {
if (!level) {
return { asymptote: true, d0, d1 }
}
const n = 10
const x0 = d0[0]
const x1 = d1[0]
const samples = linspace(x0, x1, n)
let oldY: number, oldX: number
for (let i = 0; i < n; i += 1) {
const x = samples[i]
const y = builtInEvaluate(d, 'fn', { x })
if (oldY) {
const deltaY = y - oldY
const newSign = sgn(deltaY)
if (newSign === sign) {
return checkAsymptote([oldX, oldY], [x, y], d, sign, level - 1)
}
}
oldY = y
oldX = x
}
return { asymptote: false, d0, d1 }
}
/**
* Splits the evaluated data into arrays, each array is separated by any asymptote found
* through the process of detecting slope/sign brusque changes
*/
function split(d: FunctionPlotDatum, data: BuiltInSamplerResultGroup, yScale: FunctionPlotScale): BuiltInSamplerResult {
if (data.length === 0) {
// This case is possible when the function didn't render any valid points
// e.g. when evaluating sqrt(x) with all negative values.
return []
}
const samplerResult: BuiltInSamplerResult = []
const yMin = yScale.domain()[0] - infinity()
const yMax = yScale.domain()[1] + infinity()
let samplerGroup: BuiltInSamplerResultGroup = [data[0]]
let i = 1
let deltaX = infinity()
let oldSign: number
while (i < data.length) {
const yOld = data[i - 1][1]
const yNew = data[i][1]
const deltaY = yNew - yOld
const newSign = sgn(deltaY)
// make a new set if:
if (
// we have at least 2 entries (so that we can compute deltaY)
samplerGroup.length >= 2 &&
// sgn(y1) * sgn(y0) < 0 && // there's a change in the evaluated values sign
// there's a change in the slope sign
oldSign !== newSign &&
// the slope is bigger to some value (according to the current zoom scale)
Math.abs(deltaY / deltaX) > 1
) {
// retest this section again and determine if it's an asymptote
const check = checkAsymptote(data[i - 1], data[i], d, newSign, 3)
if (check.asymptote) {
// data[i-1] has an updated [x,y], it was already added to a group (in a previous iteration)
// we just need to update the yCoordinate
data[i - 1][0] = check.d0[0]
data[i - 1][1] = clamp(check.d0[1], yMin, yMax)
samplerResult.push(samplerGroup)
// data[i] has an updated [x,y], create a new group with it.
data[i][0] = check.d1[0]
data[i][1] = clamp(check.d1[1], yMin, yMax)
samplerGroup = [data[i]]
} else {
// false alarm, it's not an asymptote
samplerGroup.push(data[i])
}
} else {
samplerGroup.push(data[i])
}
// wait for at least 2 entries in the group before computing deltaX.
if (samplerGroup.length > 1) {
deltaX = samplerGroup[samplerGroup.length - 1][0] - samplerGroup[samplerGroup.length - 2][0]
oldSign = newSign
}
++i
}
if (samplerGroup.length) {
samplerResult.push(samplerGroup)
}
return samplerResult
}
function linear(samplerParams: SamplerParams): BuiltInSamplerResult {
const allX = space(samplerParams.xAxis, samplerParams.range, samplerParams.nSamples)
const yDomain = samplerParams.yScale.domain()
// const yDomainMargin = yDomain[1] - yDomain[0]
const yMin = yDomain[0] - infinity()
const yMax = yDomain[1] + infinity()
const data: Array<[number, number]> = []
for (let i = 0; i < allX.length; i += 1) {
const x = allX[i]
let y = builtInEvaluate(samplerParams.d, 'fn', { x })
if (isValidNumber(x) && isValidNumber(y)) {
y = clamp(y, yMin, yMax)
data.push([x, y])
}
}
const splitData = split(samplerParams.d, data, samplerParams.yScale)
return splitData
}
function parametric(samplerParams: SamplerParams): BuiltInSamplerResult {
// range is mapped to canvas coordinates from the input
// for parametric plots the range will tell the start/end points of the `t` param
const parametricRange = samplerParams.d.range || [0, 2 * Math.PI]
const tCoords = space(samplerParams.xAxis, parametricRange, samplerParams.nSamples)
const samples: BuiltInSamplerResultGroup = []
for (let i = 0; i < tCoords.length; i += 1) {
const t = tCoords[i]
const x = builtInEvaluate(samplerParams.d, 'x', { t })
const y = builtInEvaluate(samplerParams.d, 'y', { t })
samples.push([x, y])
}
return [samples]
}
function polar(samplerParams: SamplerParams): BuiltInSamplerResult {
// range is mapped to canvas coordinates from the input
// for polar plots the range will tell the start/end points of the `theta` param
const polarRange = samplerParams.d.range || [-Math.PI, Math.PI]
const thetaSamples = space(samplerParams.xAxis, polarRange, samplerParams.nSamples)
const samples: BuiltInSamplerResultGroup = []
for (let i = 0; i < thetaSamples.length; i += 1) {
const theta = thetaSamples[i]
const r = builtInEvaluate(samplerParams.d, 'r', { theta })
const x = r * Math.cos(theta)
const y = r * Math.sin(theta)
samples.push([x, y])
}
return [samples]
}
function points(samplerParams: SamplerParams): BuiltInSamplerResult {
const d: PointFunction = samplerParams.d as PointFunction
return [d.points]
}
function vector(samplerParms: SamplerParams): BuiltInSamplerResult {
const d: VectorFunction = samplerParms.d as VectorFunction
d.offset = d.offset || [0, 0]
return [[d.offset, [d.vector[0] + d.offset[0], d.vector[1] + d.offset[1]]]]
}
const sampler: SamplerFn = function sampler(samplerParams: SamplerParams): BuiltInSamplerResult {
switch (samplerParams.d.fnType) {
case 'linear':
return linear(samplerParams)
case 'parametric':
return parametric(samplerParams)
case 'polar':
return polar(samplerParams)
case 'vector':
return vector(samplerParams)
case 'points':
return points(samplerParams)
default:
throw Error(samplerParams.d.fnType + ' is not supported in the `builtIn` sampler')
}
}
export default sampler