Calculating points on a curve
In the Drawing curves recipe, you've learned how to draw Bézier curves and Catmull-Rom splines. This example will teach you how you can use the bezierPoint()
and curvePoint()
functions to calculate points on those curves.
How to do it...
This is the code for the recipe. I've used the noise()
function to animate the point as it moves along the curve. Mouse movement is used to animate the curve drawn with the curve()
function.
float noiseOffset; void setup() { size( 640, 480 ); smooth(); noiseOffset = 0.0; rectMode( CENTER ); } void draw() { noiseOffset += 0.01; background( 255 ); // Bézier curve stroke( 0 ); noFill(); bezier( 40, 200, 120, 40, 300, 240, 600, 40 ); stroke( 255, 0, 0 ); line( 40, 200, 120, 40 ); line( 600, 40, 300, 240 ); fill( 255 ); rect( 120, 40, 4, 4 ); rect( 300, 240, 4, 4 ); float n = noise( noiseOffset ); float x = bezierPoint( 40, 120, 300, 600, n ); float y = bezierPoint( 200, 40, 240, 40...