Platform
Solutions
Resources
Pricing
Sign in
Sign up
Fil
Vocateur.
Workspace
Fork
Published
Interpolation
By
Fil
Edited
Nov 22, 2019
1 star
Insert cell
Insert cell
draw
(
interpolateRound
(
0
,
2
)
)
// unbalanced 😱
Insert cell
draw
(
interpolateBalanced
(
0
,
2
)
)
// balanced 😇
Insert cell
interpolateBalanced
(
0
,
2
)
(
0.7
)
Insert cell
interpolateRound
(
0
,
2
)
(
0.7
)
Insert cell
interpolateRound
=
(
a
,
b
)
=>
{
// d3.interpolateRound
return
function
(
t
)
{
return
Math
.
round
(
a
*
(
1
-
t
)
+
b
*
t
)
;
}
;
}
Insert cell
interpolateBalanced
=
(
a
,
b
)
=>
{
// epsilon is needed to have an exact value for t=1
var
div
=
Math
.
abs
(
b
-
a
)
+
epsilon
;
return
function
(
t
)
{
t
+=
(
t
-
1
/
2
)
/
div
;
return
Math
.
round
(
a
*
(
1
-
t
)
+
b
*
t
)
;
}
;
}
Insert cell
interpolateBalanced2
=
(
a
,
b
)
=>
{
// suggested by @jrus
// epsilon is needed to have an exact value for t=1
var
sign
=
Math
.
sign
(
b
-
a
)
;
return
interpolateRound
(
a
-
sign
*
(
0.5
-
epsilon
)
,
b
+
sign
*
(
0.5
-
epsilon
)
)
;
}
Insert cell
draw
(
interpolateBalanced
(
0
,
-
2
)
)
Insert cell
draw
(
interpolateBalanced
(
0
,
0
)
)
Insert cell
draw
(
interpolateRound
(
0
,
7
)
)
Insert cell
draw
(
interpolateBalanced
(
0
,
7
)
)
Insert cell
draw
(
interpolateBalanced2
(
0
,
7
)
)
Insert cell
draw
(
interpolateBalanced
(
7
,
0
)
)
Insert cell
draw
(
interpolateBalanced2
(
7
,
0
)
)
Insert cell
draw
(
interpolateBalanced
(
0.1
,
2.5
)
)
Insert cell
function
draw
(
interpolate
)
{
const
context
=
DOM
.
context2d
(
width
,
30
)
;
for
(
let
i
=
0
;
i
<
width
;
i
++
)
{
context
.
fillStyle
=
color
(
interpolate
(
i
/
(
width
-
1
)
)
)
context
.
fillRect
(
i
,
0
,
2
,
30
)
;
}
return
context
.
canvas
}
Insert cell
color
=
d3
.
scaleOrdinal
(
d3
.
schemeCategory10
)
Insert cell
d3
=
require
(
"d3@5"
)
Insert cell
import
{
epsilon
}
from
"@fil/math"
Insert cell
One platform
to build and deploy the best data apps
Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
interpolateRound
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
interpolateBalanced
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
interpolateBalanced2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
draw
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
color
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
d3
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML