Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

mturtle.cc

Go to the documentation of this file.
00001 
00025 #include <stdio.h>
00026 #include "magic/mlsystem.h"
00027 #include "magic/mturtle.h"
00028 
00029 
00031 //                                                                           //
00032 //           -----               |        ----                               //
00033 //             |              |  |  ___  (      |   ___   |   ___            //
00034 //             |   |   | |/\ -+- | /   )  ---  -+-  ___| -+- /   )           //
00035 //             |   |   | |    |  | |---      )  |  (   |  |  |---            //
00036 //             |    \__! |     \ |  \__  ___/    \  \__|   \  \__            //
00037 //                                                                           //
00039 
00040 void TurtleState::copy (const TurtleState& o) {
00041     mCoord = o.mCoord;
00042     mRot = o.mRot;
00043 }
00044 
00045 
00046 
00048 //                                                                           //
00049 //                        -----               |                              //
00050 //                          |              |  |  ___                         //
00051 //                          |   |   | |/\ -+- | /   )                        //
00052 //                          |   |   | |    |  | |---                         //
00053 //                          |    \__! |     \ |  \__                         //
00054 //                                                                           //
00056 
00057 void Turtle::forward () {
00058     double x1 = mCoord.x + mStepSize*cos(mRot);
00059     double y1 = mCoord.y + mStepSize*sin(mRot);
00060     mDevice.forwardLine (mCoord, Coord2D(x1,y1));
00061     jumpTo (x1, y1);
00062 }
00063 
00064 void Turtle::drawLSystem (const String& str) {
00065     Array<TurtleState> stack (1000);
00066     int stackp=0;
00067     bool goingForward=false; // Needed to detect tip points
00068 
00069     mDevice.start ();
00070 
00071     // Scan the string char by char
00072     for (uint i=0; i<str.length(); i++) {
00073         // TRACE1 ("%c", str[i]);
00074         // serr << *this;
00075         // serr.print("\n");
00076         switch (str[i]) {
00077           case 'F': {
00078               forward ();
00079           } break;
00080           case '[': { // PUSH
00081               ASSERTWITH (stackp < stack.size()-1,
00082                           format ("Stack (size %d) overflow", stack.size()));
00083               stack[stackp++].copy (*this);
00084               goingForward = true;
00085           } break;
00086           case ']': { // POP
00087               // Draw tip point
00088               if (goingForward) {
00089                   mDevice.tip (mCoord);
00090                   goingForward = false;
00091               }
00092               ASSERTWITH (stackp>0, "Stack underflow, too much ]'s");
00093               this->copy (stack[--stackp]);
00094           } break;
00095           case '+': {
00096               turnBy (mDeltaAngle);
00097           } break;
00098           case '-': {
00099               turnBy (-mDeltaAngle);
00100           } break;
00101           default:
00102               ;
00103         }
00104     }
00105 
00106     mDevice.end ();
00107 }
00108 
00109 OStream& Turtle::operator>> (OStream& out) const
00110 {
00111     out.printf ("{(%d,%d) rot=%f step=%d drot=%f}",
00112                 int(mCoord.x), int(mCoord.y), mRot,
00113                 mStepSize, mDeltaAngle);
00114     return out;
00115 }
00116 

Generated on Thu Feb 10 20:06:42 2005 for LibMagiC by doxygen1.2.18