//// //// Routines for the support of LOGO-style graphics programming //// //// Warren Sack //// January 2003 //// //// //// CONSTANTS //// // // CurrentTurtleXPosition // // X position of the "Turtle" // set CurrentTurtleXPosition 50 // // CurrentTurtleYPosition // // Y position of the "Turtle" // set CurrentTurtleYPosition 50 // // Angle // // Angle of the turn underway by the "Turtle" // set Angle 0 // // LastAngle // // Angle of the last turn acomplished by the "Turtle" // set LastAngle 0 // // IsPenUp // // IsPenUp is 1 if the "Turtle" is to move, but not leave a trace. // IsPenUp is 0 otherwise. // set IsPenUp 0 //// //// UTILITIES //// // // mod // // Note that this makes use of the fact that division in DBN returns // an integer. Thus, for example, 115/100 returns 1 and not 1.15; // similarly 95/100 returns 0 and not 0.95 // number mod X Y { value (X-(Y*(X/Y))) } // // cos // // Blinn's cosine, from the book modified by Golan Levin // and then by Ben Fry. // This COS takes a number from 0 to 1000 for an angle // returns a value from -100 to 100. // Remember that, conventionally, cosine takes a number // between 0 and 2*PI and returns a number // between -1 and 1. // number cos a { set a smaller? a 0 { set a (a + 1000) } set a (4*a) set b 50 smaller? a 2000 { smaller? a 1000 { set t (a/10) set t2 (t*t) set t3 (t2*t) set b (100-134*t2/10000+34*t3/1000000) set b (b/2+50) } notsmaller? a 1000 { set t ((2000-a)/10) set t2 (t*t) set t3 (t2*t) set b (100-134*t2/10000+34*t3/1000000) set b (50-b/2) } } notsmaller? a 2000 { smaller? a 3000 { set t ((a-2000)/10) set t2 (t*t) set t3 (t2*t) set b (100-134*t2/10000+34*t3/1000000) set b (50-b/2) } notsmaller? a 3000 { set t ((4000-a)/10) set t2 (t*t) set t3 (t2*t) set b (100-134*t2/10000+34*t3/1000000) set b (b/2+50) } } value ((b - 50)*2) // return -100 to 100 } // // sin // number sin a { set a smaller? a 0 { set a (a + 1000) } //value value } // // sqrt // // Goran's sqrt number designed to approximate a square root function. // number sqrt In { set Out (In/2) repeat I 1 6 { set Out ((Out + (In/Out))/2) // line 0 Out (I*10) Out } value Out } // // abs // number abs X { set Result X smaller? X 0 { set Result (0 - X) } value Result } //// //// BASE COMMANDS: //// The LOGO commands are built on top of the following base commands //// // // moveThePen // command moveThePen DeltaH DeltaV { // No line is drawn if the pen is up; i.e., if IsPenUp is 1 same? IsPenUp 0 { line CurrentTurtleXPosition CurrentTurtleYPosition (CurrentTurtleXPosition + DeltaH) (CurrentTurtleYPosition + DeltaV) } } // // updateCurrentTurtlePositionAndZeroAngle // command updateCurrentTurtlePositionAndZeroAngle DeltaH DeltaV { set CurrentTurtleXPosition (CurrentTurtleXPosition + DeltaH) set CurrentTurtleYPosition (CurrentTurtleYPosition + DeltaV) set LastAngle (LastAngle + Angle) set Angle 0 } // // moveTheTurtle // command moveTheTurtle D { set DeltaH ( (D * ) / 100 ) set DeltaV ( (D * ) / 100 ) moveThePen DeltaH DeltaV updateCurrentTurtlePositionAndZeroAngle DeltaH DeltaV } //// //// LOGO COMMANDS: left, right, forward, back, penUp, penDown //// // // left // command left N { set Angle (Angle + N) } // // right // command right N { set Angle (Angle - N) } // // forward // command forward N { moveTheTurtle N } // // back // command back N { moveTheTurtle (0 - N) } // // penUp // command penUp { set IsPenUp 1 } // // penDown // command penDown { set IsPenUp 0 } //// //// INITIALIZATION //// // // initializeLogo // // Run this command before running a sequence of LOGO commands. // command initializeLogo { pen 0 paper 100 set CurrentTurtleXPosition 50 set CurrentTurtleYPosition 50 set Angle 0 set LastAngle 0 set IsPenUp 0 } //// //// USER PROGRAM: write your Logo commands here //// initializeLogo command polygon NumberOfSides LengthOfSide { set A (1000 / NumberOfSides) notSmaller? NumberOfSides 1 { repeat X 1 NumberOfSides { forward LengthOfSide right A } } } // triangle // polygon 3 20 // hexagon // polygon 6 20 // square // polygon 4 20 // almost a circle // polygon 20 4 command partialPolygon NumberOfSides LengthOfSide VisibleSides { set A (1000 / NumberOfSides) notSmaller? VisibleSides 1 { repeat X 1 VisibleSides { forward LengthOfSide right A } } } // half a circle // partialPolygon 20 4 10 command petal Size { partialPolygon 30 Size 5 right 333 partialPolygon 30 Size 5 right 333 } // petal 10 command flower Size { repeat X 1 6 { petal Size right 166 } } // flower 10 // flower 5