Hello all,
I used a mighty sketch from asimes and made a Turtle program like in LOGO but as 3D.
Imagine a turtle that draws with a pen. When you tell it to go forward, it heads forward and paints a line.
When you tell it to turn right 90° and then go forward, you got an right angle painted. Repeat that four times a you have rect.
Now imagine you got a water turtle and can tell it to turn upwards and downwards with its nose as well and thus paint 3D or paint lines that go in an angle upward or downward from the classical 2D canvas of the Turtle (or imagine first it's only paddling on the surface and then suddenly dips down and swims towards the ground).
I tried to draw a cube by first paint a rect (that works) (points abcda), then I am at point "a" again. Then go forward once to point "b". Then turn downward (that's the 3D part) 90° and paint again a rect. Now you would have to rects in a 90° angle.
That four times should give you a cube made of four rects (doesn't work).
Anyway, I failed utterly, probabaly due to a permanent lack of intelligence I encounter regularly.
Maybe the problem lies there that when I calculate the new line to draw (the new turtle pos) I assume to start from the 0,0,0 which is true only at the start.
Maybe someone can track down my bug.
Here's what I got so far:
Copy code
- // Turtle from LOGO but in 3D
- // approach:
- // reset canvas and all values at beginning from draw, paint a rect and then a cube.
- //
- // issue:
- // works only 2D, not 3D
- //
- // to do:
- // use another approach and push to an arrayList and paint this instead
- // make Turtle a real class
- //
- float theta = 0; // degree / 2D angle of Turtle (left / right)
- float phi = 0; // degree / 3D angle of Turtle (up / down)
- boolean penUp = false; // turtle pulls a pen; when the pen moved up, it's not drawing (pen not on the canvas then)
- //
- int numMoves=0; // just to debug and show, where turtle was going
- //
- PVector posTurtle = new PVector ();
- //
- void setup() {
- size(1444, 1000, P3D);
- posTurtle.set( width/2, height/2, 0 );
- }
- //
- void draw() {
- // Init
- background(255);
- theta = 22;
- phi = 0;
- numMoves=0;
- posTurtle.set( width/2, height/2, 0 );
- //
- quadRight() ;
- forward (130);
- noseDown(90);
- quadRight() ;
- }
- // --------------------------------------------------------------------------
- void quadRight() {
- showTurtle ();
- for (int i = 0 ; i<4; i++) {
- forward (130);
- RI (90);
- //
- } // for
- } // func
- void quadLeft() {
- showTurtle ();
- for (int i = 0 ; i<4; i++) {
- forward (130);
- LE (90);
- //
- } // for
- } // func
- // ==========================================================================
- // the Turtle tools - make that a class some times
- void RI (int angle1) {
- // turn RIGHT
- theta-=angle1;
- }
- //
- void LE (int angle1) {
- // turn left
- theta+=angle1;
- }
- //
- void noseDown (int angle1) {
- phi-=angle1;
- }
- //
- void nosUp (int angle1) {
- phi+=angle1;
- }
- //
- void forward (int rad) {
- // that's drawing with line
- float x = cos(radians(phi))*sin(radians(theta))*(rad);
- float y = cos(radians(theta))*(rad);
- float z = sin(radians(phi))*sin(radians(theta))*(rad);
- if (penUp==false) {
- line (posTurtle.x, posTurtle.y, posTurtle.z,
- posTurtle.x+x, posTurtle.y+y, posTurtle.z+z );
- }
- posTurtle.x = posTurtle.x+x ;
- posTurtle.y = posTurtle.y+y ;
- posTurtle.z = posTurtle.z+z ;
- numMoves++;
- showTurtle () ;
- }
- void showTurtle () {
- // small sphere
- pushMatrix();
- translate ( posTurtle.x, posTurtle.y, posTurtle.z );
- fill(255, 0, 0);
- sphere(11);
- text( numMoves, 20, 0 );
- popMatrix();
- }
Thank you all very much!
Greetings, Chrisir
1