Page 201 - Hacking Roomba
P. 201

182       Part II — Fun Things to Do





                               Listing 9-1: SpiroExplorer

                               float R,r,d;
                               float t,dt;
                               float x,y,xo,yo;
                               void setup() {
                                 size(400,400);
                                 framerate(15);
                                 background(127);
                                 strokeWeight(7);
                                 t = 0.0;   // time
                                 dt = 0.2;  // time increment
                                 R = 110;   // radius of outer fixed circle
                                 r = 64;    // radius of inner rolling circle
                                 d = 90;    // distance from center of rolling circle
                                 update_xy();
                               }
                               void update_xy() {
                                 xo = x; yo = y;  // save old values
                                 x = (R-r)*cos(t) + d*cos( ((R-r)/r)*t );    // hypotrochoid
                                 y = (R-r)*sin(t) - d*sin( ((R-r)/r)*t );    // hypotrochoid
                                 // x = (R+r)*cos(t) - d*cos( ((R+r)/r)*t ); // epitrochoid
                                 // y = (R+r)*sin(t) - d*sin( ((R+r)/r)*t ); // epitrochoid
                                 // x = R * sin(d*t);  // lissajous
                                 // y = R * sin(r*t);  // lissajous
                                 // x = r * cos(t);    // circle
                                 // y = r * sin(t);    // circle
                                 t += dt;
                               }
                               void draw() {
                                 update_xy();
                                 pushMatrix();
                                 translate(width/2,height/2);
                                 line(x,y, xo,yo);
                                 popMatrix();
                               }



                             Another way to explore these fun sets of curves is with a graphing calculator. Mathematica
                             from Wolfram Research (available on all platforms) is the pinnacle of equation visualization
                             but is expensive if you’re not a student. Mac OS X comes with a free 2D/3D-graphing calcula-
                             tor called simply Grapher that can graph extremely complex equations. It is easier to explore a
                             much larger space of possible equations than SpiroExplorer, but it’s not as interactive for a
                             given equation. Figure 9-14 shows an example of it in use, graphing both a hypotrochoid and
                             a Lissajous equation. For other platforms, there is the free online program GCalc at http://
                             gcalc.net/. It’s a Java application or applet and will graph parametric equations. It’s not as
                             easy to use as Grapher, but it’s a great free resource you can use from any computer anywhere.
                             Once you’ve explored how the equations change and have found some curves that look nice to
                             you, it’s time to try to draw them.
   196   197   198   199   200   201   202   203   204   205   206