Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Demonstration for turtle. It is not finished yet.
- // The final version will be at https://github.com/Stone-Age-Sculptor/StoneAgeLib
- LEFT = 100;
- RIGHT = 101;
- HOME = 102;
- FORWARD = 103;
- BACKWARD = 104;
- CIRCLE = 105;
- GOTO = 106;
- $fn=80;
- // Create a turtle pattern as a wave.
- turtle1 = [ [LEFT,110], each for(i=[0:10]) [[CIRCLE,5,-220],[CIRCLE,5,220]] ];
- path1 = TurtleToPath(turtle1);
- DrawTurtlePath(path1,0.5);
- module DrawTurtlePath(path,width=1)
- {
- if(len(path) >= 2)
- {
- for(i=[0:len(path)-2])
- {
- hull()
- {
- DrawDot(path[i]);
- DrawDot(path[i+1]);
- }
- }
- }
- module DrawDot(pos)
- {
- translate(pos)
- circle(width);
- }
- }
- function TurtleToPath(turtlelist,accuracy=$fn) =
- concat([[0,0], each WalkTheTurtle(turtlelist=turtlelist,accuracy=accuracy)]);
- // This function is recursive, going through all
- // the commands in the list.
- // The position and the angle of the turtle is kept
- // in the parameters "angle" and "lastpos".
- function WalkTheTurtle(turtlelist,accuracy=$fn,index=0,angle=0,lastpos=[0,0]) =
- index < len(turtlelist) ?
- // LEFT turns the angle left with the specified degrees.
- // The turtle itself stays in the same position.
- // No coordinates are added to the returning list.
- turtlelist[index][0] == LEFT ?
- WalkTheTurtle(turtlelist,accuracy,index+1,angle+turtlelist[index][1],lastpos) :
- // RIGHT turns the angle left with the specified degrees.
- // The turtle itself stays in the same position.
- // No coordinates are added to the returning list.
- turtlelist[index][0] == RIGHT ?
- WalkTheTurtle(turtlelist,accuracy,index+1,angle-turtlelist[index][1],lastpos) :
- // FORWARD moves the turtle forward, in the direction
- // of the angle of the turtle.
- turtlelist[index][0] == FORWARD ?
- let(x = lastpos.x + turtlelist[index][1] * cos(angle))
- let(y = lastpos.y + turtlelist[index][1] * sin(angle))
- concat([[x,y]], WalkTheTurtle(turtlelist,accuracy,index+1,angle,[x,y])) :
- // BACKWARD moves the turtle backward, according to the direction
- // of the angle of the turtle.
- turtlelist[index][0] == BACKWARD ?
- let(x = lastpos.x - turtlelist[index][1] * cos(angle))
- let(y = lastpos.y - turtlelist[index][1] * sin(angle))
- concat([[x,y]], WalkTheTurtle(turtlelist,accuracy,index+1,angle,[x,y])) :
- // CIRCLE has two parameters:
- // first parameter:
- // The radius of the circle.
- // second parameter:
- // The part of a circle.
- // 360 is a full circle, 90 is a quarter of a circle.
- // A positive angle is anti-clockwise.
- // A negative angle is clockwise.
- turtlelist[index][0] == CIRCLE ?
- // The radius for the arc.
- let(radius = turtlelist[index][1])
- // The extend is a part of a circle, 360 is a full circle.
- let(extend = turtlelist[index][2])
- // Put the center of the circle on the left or on the right.
- let(left_right = extend < 0 ? -1 : 1)
- // Calculate the center position of the circle.
- let(center_x = lastpos.x + radius*cos(angle + left_right*90))
- let(center_y = lastpos.y + radius*sin(angle + left_right*90))
- // Calcuate the start and end angle.
- let(angle_start = angle - left_right*90)
- let(angle_end = angle_start + extend)
- // Calculate the number of steps, according to the $fn settings.
- let(steps = floor(accuracy * abs(angle_end - angle_start)/360))
- let(angle_step = (angle_end - angle_start) / steps)
- // Calculate a list with arc coordinates.
- // Keep the list empty, if there are no steps.
- let(arc = steps > 0 ? [for(i=[0:steps])
- [center_x + radius*cos(angle_start+angle_step*i),
- center_y + radius*sin(angle_start+angle_step*i)]] : [])
- // Calculate the new angle for the turtle.
- let(new_angle = angle + extend)
- // Calculate the new location of the turtle.
- let(x = center_x + radius*cos(angle_end))
- let(y = center_y + radius*sin(angle_end))
- concat(arc, WalkTheTurtle(turtlelist=turtlelist,accuracy=accuracy,index=index+1,angle=new_angle,lastpos=[x,y])) :
- // GOTO goes straight to an absolute location.
- turtlelist[index][0] == GOTO ?
- let(x = turtlelist[index][1])
- let(y = turtlelist[index][2])
- concat([[x,y]], WalkTheTurtle(turtlelist=turtlelist,accuracy=accuracy,index=index+1,angle=angle,lastpos=[x,y])) :
- // HOME goes to (0,0)
- // The angle is also reset to zero.
- // A zero angle is in the direction of the postive x-axis.
- turtlelist[index][0] == HOME ?
- concat([[0,0]], WalkTheTurtle(turtlelist=turtlelist,accuracy=accuracy,index=index+1,angle=0,lastpos=[0,0])) : [] : [];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement