Advertisement
Guest User

OpenSCAD: Demonstration of turtle

a guest
Jan 29th, 2025
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | Source Code | 0 0
  1. // Demonstration for turtle. It is not finished yet.
  2. // The final version will be at https://github.com/Stone-Age-Sculptor/StoneAgeLib
  3.  
  4. LEFT = 100;
  5. RIGHT = 101;
  6. HOME = 102;
  7. FORWARD = 103;
  8. BACKWARD = 104;
  9. CIRCLE = 105;
  10. GOTO = 106;
  11.  
  12. $fn=80;
  13.  
  14. // Create a turtle pattern as a wave.
  15. turtle1 = [ [LEFT,110], each for(i=[0:10]) [[CIRCLE,5,-220],[CIRCLE,5,220]] ];
  16. path1 = TurtleToPath(turtle1);
  17. DrawTurtlePath(path1,0.5);
  18.  
  19. module DrawTurtlePath(path,width=1)
  20. {
  21. if(len(path) >= 2)
  22. {
  23. for(i=[0:len(path)-2])
  24. {
  25. hull()
  26. {
  27. DrawDot(path[i]);
  28. DrawDot(path[i+1]);
  29. }
  30. }
  31. }
  32.  
  33. module DrawDot(pos)
  34. {
  35. translate(pos)
  36. circle(width);
  37. }
  38. }
  39.  
  40. function TurtleToPath(turtlelist,accuracy=$fn) =
  41. concat([[0,0], each WalkTheTurtle(turtlelist=turtlelist,accuracy=accuracy)]);
  42.  
  43. // This function is recursive, going through all
  44. // the commands in the list.
  45. // The position and the angle of the turtle is kept
  46. // in the parameters "angle" and "lastpos".
  47. function WalkTheTurtle(turtlelist,accuracy=$fn,index=0,angle=0,lastpos=[0,0]) =
  48. index < len(turtlelist) ?
  49.  
  50. // LEFT turns the angle left with the specified degrees.
  51. // The turtle itself stays in the same position.
  52. // No coordinates are added to the returning list.
  53. turtlelist[index][0] == LEFT ?
  54. WalkTheTurtle(turtlelist,accuracy,index+1,angle+turtlelist[index][1],lastpos) :
  55.  
  56. // RIGHT turns the angle left with the specified degrees.
  57. // The turtle itself stays in the same position.
  58. // No coordinates are added to the returning list.
  59. turtlelist[index][0] == RIGHT ?
  60. WalkTheTurtle(turtlelist,accuracy,index+1,angle-turtlelist[index][1],lastpos) :
  61.  
  62. // FORWARD moves the turtle forward, in the direction
  63. // of the angle of the turtle.
  64. turtlelist[index][0] == FORWARD ?
  65. let(x = lastpos.x + turtlelist[index][1] * cos(angle))
  66. let(y = lastpos.y + turtlelist[index][1] * sin(angle))
  67. concat([[x,y]], WalkTheTurtle(turtlelist,accuracy,index+1,angle,[x,y])) :
  68.  
  69. // BACKWARD moves the turtle backward, according to the direction
  70. // of the angle of the turtle.
  71. turtlelist[index][0] == BACKWARD ?
  72. let(x = lastpos.x - turtlelist[index][1] * cos(angle))
  73. let(y = lastpos.y - turtlelist[index][1] * sin(angle))
  74. concat([[x,y]], WalkTheTurtle(turtlelist,accuracy,index+1,angle,[x,y])) :
  75.  
  76. // CIRCLE has two parameters:
  77. // first parameter:
  78. // The radius of the circle.
  79. // second parameter:
  80. // The part of a circle.
  81. // 360 is a full circle, 90 is a quarter of a circle.
  82. // A positive angle is anti-clockwise.
  83. // A negative angle is clockwise.
  84. turtlelist[index][0] == CIRCLE ?
  85. // The radius for the arc.
  86. let(radius = turtlelist[index][1])
  87. // The extend is a part of a circle, 360 is a full circle.
  88. let(extend = turtlelist[index][2])
  89. // Put the center of the circle on the left or on the right.
  90. let(left_right = extend < 0 ? -1 : 1)
  91. // Calculate the center position of the circle.
  92. let(center_x = lastpos.x + radius*cos(angle + left_right*90))
  93. let(center_y = lastpos.y + radius*sin(angle + left_right*90))
  94. // Calcuate the start and end angle.
  95. let(angle_start = angle - left_right*90)
  96. let(angle_end = angle_start + extend)
  97. // Calculate the number of steps, according to the $fn settings.
  98. let(steps = floor(accuracy * abs(angle_end - angle_start)/360))
  99. let(angle_step = (angle_end - angle_start) / steps)
  100. // Calculate a list with arc coordinates.
  101. // Keep the list empty, if there are no steps.
  102. let(arc = steps > 0 ? [for(i=[0:steps])
  103. [center_x + radius*cos(angle_start+angle_step*i),
  104. center_y + radius*sin(angle_start+angle_step*i)]] : [])
  105. // Calculate the new angle for the turtle.
  106. let(new_angle = angle + extend)
  107. // Calculate the new location of the turtle.
  108. let(x = center_x + radius*cos(angle_end))
  109. let(y = center_y + radius*sin(angle_end))
  110. concat(arc, WalkTheTurtle(turtlelist=turtlelist,accuracy=accuracy,index=index+1,angle=new_angle,lastpos=[x,y])) :
  111.  
  112. // GOTO goes straight to an absolute location.
  113. turtlelist[index][0] == GOTO ?
  114. let(x = turtlelist[index][1])
  115. let(y = turtlelist[index][2])
  116. concat([[x,y]], WalkTheTurtle(turtlelist=turtlelist,accuracy=accuracy,index=index+1,angle=angle,lastpos=[x,y])) :
  117.  
  118. // HOME goes to (0,0)
  119. // The angle is also reset to zero.
  120. // A zero angle is in the direction of the postive x-axis.
  121. turtlelist[index][0] == HOME ?
  122. concat([[0,0]], WalkTheTurtle(turtlelist=turtlelist,accuracy=accuracy,index=index+1,angle=0,lastpos=[0,0])) : [] : [];
  123.  
  124.  
Tags: OpenSCAD
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement