Chrisir
Full Member
Offline
Posts: 207
Germany
Postscriptum 3D Turtle
Jan 5th , 2009, 11:28pm
/* Prinzip : Turtle von LOGO, aber in 3D. Daher hat die Turtle nicht nur x und y, sondern auch z-Position und nicht nur einen Winkel, sondern zwei. */ float TurtleX; /* Turtledaten */ float TurtleY; float TurtleZ; int WinkelVertikal; int WinkelHorizontal; color TurtleColor; boolean StiftHoch = false; // Ende Turtledaten /* ===================== Inits & TOOLS ================================ */ void setup() { size ( screen.width, screen.height, P3D ); TurtleColor = color(255, 0, 0 ); color ( TurtleColor); background(110); TurtleX = width / 2; TurtleY = height / 2; TurtleZ = 10; WinkelVertikal = 0 ; WinkelHorizontal = 0 ; smooth(); } void CheckWinkel() { // Bereichsüberprüfung if (WinkelHorizontal > 359) { WinkelHorizontal = WinkelHorizontal - 360; } if (WinkelHorizontal > 359) { WinkelHorizontal = WinkelHorizontal - 360; } if (WinkelHorizontal > 359) { WinkelHorizontal = WinkelHorizontal - 360; } if (WinkelVertikal > 359) { WinkelVertikal = WinkelVertikal - 360; } if (WinkelVertikal > 359) { WinkelVertikal = WinkelVertikal - 360; } if (WinkelVertikal > 359) { WinkelVertikal = WinkelVertikal - 360; } if (WinkelHorizontal < 0) { WinkelHorizontal = 360 + WinkelHorizontal; } if (WinkelVertikal < 0) { WinkelVertikal = 360 + WinkelVertikal; } } // =========================================== // Ende Tools // Begin Turtle-Basics void Forward(float Lang) { // move forward /* Malt ggf. (Wenn not Stifthoch gilt) eine Linie und ändert in jedem Fall die Turtle-Position. Die Linie verbindet alte und neue Turtle-Position. */ float OldX, OldY, OldZ; float LangZ=0; float LangNeu=0; // Bereichsüberprüfung CheckWinkel(); // alte Werte sichern: OldX = TurtleX; OldY = TurtleY; OldZ = TurtleZ; // Matrix befüllen pushMatrix(); // translate and turn translate (OldX,OldY,OldZ); rotateX(WinkelVertikal); // xrot rotateZ(WinkelHorizontal); // zrot point (OldX,OldY,OldZ); // it was drawn at ( .., .., ..), store that location float x = modelX(OldX,OldY,OldZ); float y = modelY(OldX,OldY,OldZ); float z = modelZ(OldX,OldY,OldZ); // clear out all the transformations (read out Matrix) popMatrix(); // get the (x, y, z) coordinate TurtleX = x; // TurtleY = y; // TurtleZ = z; // if (!StiftHoch){ stroke ( TurtleColor); line ( OldX, OldY, OldZ, TurtleX, TurtleY, TurtleZ ); } } void Backward(int Length) { // move backward Forward (-Length); } void Right(int Winkel) { // Turn right WinkelHorizontal += Winkel; } void Left(int Winkel) { // Turn left Right(-Winkel); } void Up(int Winkel) { // Turn up WinkelVertikal += Winkel; } void Down(int Winkel) { // Turn down Up(-Winkel); } void JumpTo ( int a, int b, int c ) { // verwendet absolute Koordinaten TurtleX = a; TurtleY = b; TurtleZ = c; } // Ende Turtle Commands /* ------------------------------------ */ // Beginn Turtle-Programme 1 void Quadrat(int Laenge) { StiftHoch = false; for (int i = 0; i < 4; i = i+1) { Forward ( Laenge ); Right (90); } } // ================================================== // Beginn Turtle-Programme 2 void QuadratUp(int Laenge) { StiftHoch = false; for (int i = 0; i < 4; i = i+1) { Forward ( Laenge ); Up (90); } } void Wuerfel(int Laenge) { // paint cube println("Start Cube"); StiftHoch = false; WinkelVertikal = 0 ; WinkelHorizontal = 0 ; // Start Position TurtleX=560; TurtleY=340; TurtleZ=10; // for Number of Planes for (int j = 0; j < 4; j = j+1) { // Sides of a plane for (int i = 0; i < 4; i = i+1) { Forward ( Laenge ); Right (90); } // Ende for i Forward(Laenge); Up(90); } // Ende for j println("Ende Cube"); } void Igel3D(int Laenge) { StiftHoch = false; lights(); noStroke(); // color ( TurtleColor); for (int j = 0; j < 361; j = j+40) { for (int i = 0; i < 361; i = i+40) { JumpTo ( 600,400,400 ); WinkelVertikal = j; WinkelHorizontal = i; Forward ( Laenge ); } } } void Spirale3D() { JumpTo (600,400,10); Up(10); for (int j = 0; j < 360; j = j+2 ) { Right (10); Forward (14); } // Ende for } // Ende function // ================================================================================ ========== // Hauptprogramm void draw() { println ( "" ); println ( "Starting." ); JumpTo ( 300, 444, 30 ); Quadrat (30); Up(90); Quadrat (30); // Wuerfel(40); // Quadrat(40); // Spirale3D(); // Igel3D(90); noLoop(); println ( "End. "); println ( ""); }