// chaosGame.C // // Written 11 Feb 96 by Max Hailperin // // This program plays simple versions of the "chaos game" in which the orbit // of a point is plotted as it moves some specified fraction of the way towards // a randomly chosen vertex at each iteration, where the vertices are equally // spaced and there are a specified number of them. // // For a simple but startling demo, try using 3 vertices and a compression of .5, // i.e., each iteration the point moves half way towards a randomly chosen vertex // of an equilateral triangle. You can start with the initial point anywhere, // though if it is inside the triangle its behavior will more quickly become // "normal". Assuming you start inside the triangle, you can ask to not show the // first maybe 8 or 10 iterations and see the pattern clearly. // // This version of the program incorporates some rather crude user interface, // namely waiting for the user to press enter once the drawing window is // displayed properly before it starts to draw on it, as a precaution in case // the DrawingWindow class is implemented using a simplistic implementation // that can't deal with drawing "off screen" and copying that onto the // on-screen window when appropriate. // // A similar wait-for-enter-key at the end prevents the window from vanishing // before the user gets to see it, since windows would go away on program // exit in most obvious implementations of the DrawingWindow abstraction. // // For more information, see http://math.bu.edu/DYSYS/chaos-game/chaos-game.html #include #include #include "Vector.h" #include "DrawingWindow.h" #include "pseudoRandom.h" Vector makeVectorPolar(float radius, float angle); // declared here, needs defining Vector get_vec(int identifyingNumber); // snarf definition from Wang's book Vector fractionFromTo(float f, Vector p1, Vector p2) // returns the Vector for the point the fraction f of the way from p1 to p2 { return p2*f + p1*(1-f); } void readThroughNewline(){ char c; while(cin.get(c) && c != '\n') ; } int main(){ const int maxVertices = 20; int numVertices; cout << "How many vertices? "; cin >> numVertices; if(numVertices > maxVertices){ cerr << "Sorry, this program is set up for at most " << maxVertices << " vertices.\n"; exit(1); } Vector vertices[maxVertices]; for(int i = 0; i < numVertices; i++) vertices[i] = makeVectorPolar(1, 2*3.14159*i/numVertices); float compression; cout << "What compression? "; cin >> compression; int iterations; cout << "How many iterations? "; cin >> iterations; int hiddenIterations; cout << "How many initial iterations should not be displayed? "; cin >> hiddenIterations; cout << "Enter staring point.\n"; Vector point = get_vec(0); DrawingWindow win; readThroughNewline(); // to get rid of the newline at end of last input cout << "Press enter once window is visible.\n"; readThroughNewline(); // to wait for this, due to lack of redrawing support for(i = 1; i <= iterations; i++){ point = fractionFromTo(compression, vertices[pseudoRandom(numVertices)], point); if(i > hiddenIterations) point.drawAsPointOn(win); } cout << "Press enter when ready to exit program.\n"; readThroughNewline(); // to wait for this, since window goes away on exit return 0; }