void drawStar(Turtle& turtle, double size, int sides) { for (int i = 0; i < sides; i++) { turtle.forward(size); turtle.right(360.0 / sides); turtle.forward(size); turtle.left(2 * 360.0 / sides); } }
// Recursive fractal "tree" structure void tree(Turtle& turtle, int level, double size, double angle, double scale) { if (level == 0) { turtle.forward(size); turtle.backward(size); } else { turtle.forward(size); turtle.left(angle); tree(turtle, level - 1, size * scale, angle, scale); turtle.right(angle); turtle.right(angle); tree(turtle, level - 1, size * scale, angle, scale); turtle.left(angle); turtle.backward(size); } }
void drawSpiral(Turtle& turtle, double size) { while (size < 1.25) { turtle.forward(size); turtle.right(15); size = size * 1.02; } }
// draw the given lsystem starting at the given position void drawSystem(LSystem* sys, vec4 startPoint, vec4 color, bool setColor) { Turtle* turtle = sys->getTurtleCopy(); stack<mat4> modelView; // move to start point and point the tree upwards modelView.push(Translate(startPoint) * RotateX(-90)); turtle->ctm = &modelView; string turtleString = sys->getTurtleString(); if(setColor) { GLuint colorLoc = glGetUniformLocationARB(program, "inColor"); glUniform4fv(colorLoc, 1, color); } for(string::iterator it = turtleString.begin(); it != turtleString.end(); ++it) { char currentChar = *it; if(currentChar == 'F') { drawTurtleComponent(turtle, sphere); drawTurtleComponent(turtle, cylinder); } switch(currentChar) { case 'F': case 'f': turtle->forward(); break; case '+': turtle->rotate(Turtle::X, true); break; case '-': turtle->rotate(Turtle::X, false); break; case '&': turtle->rotate(Turtle::Y, true); break; case '^': turtle->rotate(Turtle::Y, false); break; case '\\': turtle->rotate(Turtle::Z, true); break; case '/': turtle->rotate(Turtle::Z, false); break; case '|': turtle->turnAround(); break; case '[': turtle->push(); break; case ']': turtle->pop(); break; } } delete turtle; }
void drawRosette(Turtle& turtle, double size, int squares) { double angle = 360.0 / squares; for (int i = 0; i < squares; i++) { for (int j = 0; j < 4; j++) { turtle.forward(size); turtle.right(90); } turtle.right(angle); } }
void symetric_star(int number_of_sides, const int IMAGE_SIZE, std::string file_name){ Turtle turtle (IMAGE_SIZE/2,IMAGE_SIZE/2,IMAGE_SIZE); int angle = 180 - 180/number_of_sides; turtle.pendown(); for (int x = 0; x < number_of_sides; ++x) { turtle.left(angle); turtle.forward(5); } turtle.save(file_name); }
// Recursive Koch fractal void koch(Turtle& turtle, int level, double size, double angle) { if (level == 0) { turtle.forward(size); } else { koch(turtle, level - 1, size, angle); turtle.left(angle); koch(turtle, level - 1, size, angle); turtle.right(angle); turtle.right(angle); koch(turtle, level - 1, size, angle); turtle.left(angle); koch(turtle, level - 1, size, angle); } }
/** The main function begins execution of the program. */ int ccc_win_main() { Turtle myrtle; double size = 3; myrtle.moveTo(5 - size / 2, -9); // bottom of drawing area myrtle.setHeading(90); for (int i = 3; i < 10; i++) { drawPolygon(myrtle, size, i); } myrtle.moveTo(-4 + size / 2, 1); myrtle.setHeading(-18); drawStar(myrtle, size, 5); myrtle.moveTo(-5, -5); // center of drawing area myrtle.setHeading(0); drawRosette(myrtle, size, 40); myrtle.moveTo(5, 5); // center of drawing area myrtle.setHeading(90); drawSpiral(myrtle, .01); usleep(2000000); cwin.clear(); myrtle.moveTo(0, -9); // start of drawing area myrtle.setHeading(0); tree(myrtle, 10, 7, 60, .6); usleep(2000000); cwin.clear(); myrtle.moveTo(-9, -9); // start of drawing area myrtle.setHeading(90); koch(myrtle, 5, .25, 80); usleep(2000000); cwin.clear(); myrtle.moveTo(4, -8); // start of drawing area myrtle.setHeading(0); kochSnowflake(myrtle, 5, .05); myrtle.sleep(2000); myrtle.clear(); string input = myrtle.getString("Enter a message: "); int num1 = myrtle.getInt("Enter an integer number: "); double num2 = myrtle.getDouble("Enter a double number: "); myrtle.moveTo("Click mouse to display the message"); myrtle.print(input); myrtle.moveTo("Click mouse to display the integer number"); myrtle.print(num1); myrtle.moveTo("Click mouse to display the double number"); myrtle.print(num2); myrtle.moveTo("Click mouse to display a square"); myrtle.setHeading(0); myrtle.right(90); myrtle.forward(5); myrtle.right(90); myrtle.forward(5); myrtle.right(90); myrtle.forward(5); myrtle.right(90); myrtle.forward(5); }
int ccc_win_main() { Turtle t; bool running = true; while (running == true) { double sides = -1; while (sides < 1) { sides = t.getInt("Enter number of sides:"); } double length = t.getDouble("Enter length of sides:"); if (sides > 1) { int degrees = DEGREES_IN_CIRCLE / sides; t.setHeading(LEFT); /* Apothem is the distance of the normal of any side of the polygon to the centerpoint of the polygon. The following code will center the shape in the window. */ double apothem = length / (2 * tan(PI / sides)); t.moveTo(length / 2, -apothem); int i = 0; while (i < sides) { t.forward(length); t.right(degrees); i++; } } if (sides == 1) { t.setHeading(0); // Creates the upper case letter M. t.moveTo(NTHREE,NNINE); t.right(TEN); t.forward(FORWARD_INC); t.right(G_TURN2); t.forward(FORWARD_INC); t.left(G_TURN2); t.forward(FORWARD_INC); t.right(G_TURN2); t.forward(FORWARD_INC); // Creates the upper case letter G. t.moveTo(SEVEN_POINT_FIVE, N_ONE_POINT_FIVE); t.left(G_TURN); t.forward(2); t.backward(2); t.right(ONE_THIRTY); t.forward(2); t.left(G_TURN); t.forward(2); t.left(G_TURN); t.forward(2); t.left(G_TURN); t.forward(2); t.left(G_TURN); t.forward(2); t.left(G_TURN); t.forward(2); t.left(G_TURN); t.forward(2); t.left(SMALL_TURN); t.forward(1); t.left(SMALL_TURN); t.forward(1); t.left(SMALL_TURN); t.forward(1); t.left(SMALL_TURN); t.forward(1); t.backward(2); // Creates the upper case letter J. t.setHeading(RIGHT_TURN); t.moveTo(NSEVEN, FIVE); t.forward(2); t.right(STRAIGHT); t.forward(1); t.left(RIGHT_TURN); t.forward(2); t.right(RIGHT_TURN); t.forward(1); // Creates the upper case letter W. t.moveTo(-FOUR, FIVE); t.right(TWO_THIRTY); t.forward(W_INC); t.left(RIGHT_TURN); t.forward(2); t.right(RIGHT_TURN); t.forward(2); t.left(RIGHT_TURN); t.forward(W_INC); // Creates the upper case letter C. t.moveTo(FOUR, FIVE); t.right(C_TURN); t.forward(2); t.right(STRAIGHT); t.forward(2); t.left(RIGHT_TURN); t.forward(2); t.left(RIGHT_TURN); t.forward(2); } string repeat = t.getString("Do you wish to continue (y/n)? "); if (repeat != "y") running = false; else t.clear(); } cout << "Exiting Program.\n"; exit(0); return 0; }
void drawPolygon(Turtle& turtle, double size, int sides) { double angle = 360.0 / sides; for (int count = 0; count < sides; count++) { turtle.forward(size); turtle.left(angle); } }