/* * Model for a circle rolling on a line, while tracing with pen point. * We presume the pen starts directly below center circle. */ float circRadius = 30; float penRadius = 0.9 * circRadius; // dist of pen from circle center color penColor = color(255,0,0); float cx,cy; // center of moving circle float[] penX = new float[10000]; // points of traced polyline float[] penY = new float[10000]; int numPoints; void setup() { size(600,90); reset(); } void reset() { cx = 0; cy = height-1.5*circRadius; numPoints = 0; } void draw() { // update model of rolling circle // cx = distance traveled = #rotations * PI*diameter // #rotations = cx/(PI*diameter) // current angle is #rotations*TWO_PI = 2*cx/diameter = cx/radius float theta = PI + cx / circRadius; penX[numPoints] = cx + penRadius*cos(theta); penY[numPoints] = cy + penRadius*sin(theta); numPoints++; cx++; // now its time to render the current state background(255); // draw the base line for rolling stroke(0); strokeWeight(1); line(0, cy+circRadius, width, cy+circRadius); // draw the circle and pen point fill(127,127,255); ellipse(cx, cy, 2*circRadius, 2*circRadius); stroke(penColor); strokeWeight(4); point(penX[numPoints-1], penY[numPoints-1]); // draw the traced pen path strokeWeight(1); noFill(); beginShape(); for (int j=0; j < numPoints; j++) { vertex(penX[j], penY[j]); } endShape(); // restart once past the visble end of the canvas if (cx > width + penRadius) { reset(); } }