|
Author |
Topic: Turtle Tool (Read 1635 times) |
|
Tom Fuerstner Guest
|
Turtle Tool
« on: Nov 25th, 2002, 1:18pm » |
|
i thought it would be interesting to also use the turtle metapher inside the proce55ing IDE. to accomblish this task i've implemented a basic turtle-class. it works quite well and offers new possibilities especially for teaching. but check it out for yourself ! by, tom // Turtle 01 // by Tom Fuerstner // This is a basic implementation // of a graphic turtle similar to the one // inside the logo-language // Created: 20 Nov 2002 class Turtle { int xcor = width/2; int ycor = height/2; int angle = -90; int heading = 0; int bg = 151; boolean penStateUp = true; boolean visibility = true; boolean wrapMode = false; boolean windowMode = false; boolean fenceMode = false; color penColor = color(100,100,100); int penAlpha; int penWidth; void forward(int distance) { int xcor_1, ycor_1; xcor_1 = xcor; ycor_1 = ycor; xcor += distance*cos(radians(angle)); ycor += distance*sin(radians(angle)); if (penStateUp == true) { translate (xcor,ycor); } else { line (xcor_1, ycor_1, xcor, ycor); } } void back(int distance) { int xcor_1, ycor_1; xcor_1 = xcor; ycor_1 = ycor; xcor -= distance*cos(radians(angle)); ycor -= distance*sin(radians(angle)); if (penStateUp == true) { translate (xcor,ycor); } else { line (xcor_1, ycor_1, xcor, ycor); } } void right(int degree) { angle += degree; if (angle >= 360) { angle -= 360; } } void left(int degree) { angle -= degree; if (angle <= 0) { angle += 360; } } void penup() { penStateUp = true; } void pendown() { penStateUp = false; } void penerase() { //has to be implemented; //lower the pen with an eraser tip; } void penreverse() { //has to be implemented; //lower the pen with reversing ink; } void fence() { //has to be implemented; //sending the turtle beyond a screen edge; //will be considered an error; } void wrap() { //has to be implemented; //make turtle wrap aroung to opposite edge; //when it goes beyond; } void window() { //has to be implemented; //make turtle cease to wrap; } void home() { if (penStateUp == true) { translate (width/2,height/2); } else { line (xcor, ycor, width/2, height/2); } angle = 180; heading = 180; } void setxy(int x1, int y1) { if (penStateUp == true) { translate (x1,y1); } else { line (xcor, ycor, x1, y1); } xcor = x1; ycor = y1; } void pos() { println(xcor+" : "+ycor); } void setpos(int spx, int spy) { xcor = spx; ycor = spy; } void setx(int sx) { xcor = sx; } void sety(int sy) { ycor = sy; } void xcor() { println(xcor); } void ycor() { println(ycor); } void setheading(int sh) { angle = sh; } void towards(int tox,int toy) { //has to be implemented; } void pen() { //outputs a list of pen type and color; //also this command makes no sense in the context; // of the proce55ing environment; } void pencolor (int pc) { //outputs the number of the color; } void getbg (int gbg) { // outputs the colornumber of the background; } void setbg (int sbg) { // sets the background to a certain color; } void showturtle(boolean st) { visibility = true; } void hideturtle(boolean ht) { visibility = false; } void clearscreen() { home(); } void clean() { //don't know right now wheter this command is useful or not; } void square(int sq) { for (int i= 1;i<128; ++i) { forward(sq); right(90); } } } void setup() { int turtleNum = 0; size(600, 200); colorMode(HSB, 100); background(255); } void loop() { Turtle tom = new Turtle(); tom.pendown();tom = new Turtle(); for (int i= 1;i<128; ++i) { tom.pendown(); tom.square(30); tom.forward(100); tom.back(50); tom.right(i*10); } }
|
|
|
|
REAS
|
Re: Turtle Tool
« Reply #1 on: Nov 25th, 2002, 5:48pm » |
|
oh wow! this is fantastic. it makes my day to see this. thank you, tom.
|
|
|
|
|