Skip to content

Instantly share code, notes, and snippets.

@gabrielflorit
Last active June 30, 2018 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielflorit/da0dc35113c79da2a8eb2e194329e991 to your computer and use it in GitHub Desktop.
Save gabrielflorit/da0dc35113c79da2a8eb2e194329e991 to your computer and use it in GitHub Desktop.
SCRIPT-8
// title: quadratic bezier
const p0 = [1, 1]
const p2 = [126, 1]
const r = 62
initialState = {
a: 0
}
update = state => {
state.a -= 0.05
}
const bezier = (t, p0, p1, p2) =>
(1 - t) ** 2 * p0 + 2 * (1 - t) * t * p1 + t * t * p2
const reproject = p => [p[0], 127 - p[1]]
draw = state => {
clear()
print(0, 0, 'this is a', 0)
print(93, 0, 'quadratic', 0)
print(0, 8, 'bezier', 0)
print(109, 8, 'curve', 0)
const { a } = state
const z = [64 + r * Math.cos(a), 64 + r * Math.sin(a)]
const points = range(0, 1.1, 0.05).map(t => [
bezier(t, p0[0], z[0], p2[0]),
bezier(t, p0[1], z[1], p2[1])
])
line(...reproject(p0), ...reproject(z), 6)
line(...reproject(p2), ...reproject(z), 6)
circFill(...reproject(z), 2, 4)
points.forEach((p, i) => {
circFill(...reproject(p), 2, 0)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment