Code, Draw, and 3D-Print with Turtle Tina

Similar documents
Writing Simple Procedures Drawing a Pentagon Copying a Procedure Commanding PenUp and PenDown Drawing a Broken Line...

Recursion with Turtles

Fractal. Fractals. L- Systems 1/17/12

1 Turtle Graphics Concepts

In this project you will use loops to create a racing turtle game and draw a race track.

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

Package TurtleGraphics

Python 3 Turtle graphics. Lecture 24 COMPSCI111/111G SS 2017

Lecture 1: Turtle Graphics. the turtle and the crane and the swallow observe the time of their coming; Jeremiah 8:7

Turtle Ballet: Simulating Parallel Turtles in a Nonparallel LOGO Version. Erich Neuwirth

A Guide to the TurtleGraphics Package for R

Recursion with Turtles

Reference Guide Playful Invention Company

StarLogo Complete Command List (Edited and reformatted by Nicholas Gessler, 6 June 2001.)

Finch Robot: snap level 4

Scratch Lesson Plan. Part One: Structure. Part Two: Movement

6. 1 Leaping Lizards!

Activity 1: Changes in beak size populations in low precipitation

Math 290: L A TEXSeminar Week 10

The City School. Learn Create Program

Workbook. Version 3. Created by G. Mullin and D. Carty

GEOG 490/590 SPATIAL MODELING SPRING 2015 ASSIGNMENT 3: PATTERN-ORIENTED MODELING WITH AGENTS

Lab 7. Evolution Lab. Name: General Introduction:

Biol 160: Lab 7. Modeling Evolution

~~~***~~~ A Book For Young Programmers On Scratch. ~~~***~~~

Hello Scratch! by Gabriel Ford, Sadie Ford, and Melissa Ford. Sample Chapter 3. Copyright 2018 Manning Publications

Geometry from Scratch

Biology 164 Laboratory

Sketch Out the Design

Scratch. To do this, you re going to need to have Scratch!

Half-Lives of Antibiotics

A few other notes that may be of use.

RUBBER NINJAS MODDING TUTORIAL

Body Parts and Products (Sessions I and II) BROWARD COUNTY ELEMENTARY SCIENCE BENCHMARK PLAN

Integrated Math 1 Honors Module 2 Honors Systems of Equations and Inequalities

5 State of the Turtles

by Jennifer Oxley and Billy Aronson

$2.85 $4.10 $1.00 $25.00 $2.25 $2.25 4X4 CUSTOM QUOTE: $55.00 PLUS $1.00 PER LINE 8X10 CUSTOM QUOTE: $75.00 PLUS $1.00 PER LINE

CS108L Computer Science for All Module 7: Algorithms

Dasher Web Service USER/DEVELOPER DOCUMENTATION June 2010 Version 1.1

Getting Started! Searching for dog of a specific breed:

Characteristics of the Text Genre Fantasy Text Structure Simple fi rst-person narrative, with story carried by pictures Content

Lab 7: Experimenting with Life and Death

Grooming the Kerry Blue Terrier

Teaching Assessment Lessons

Lab 5: Bumper Turtles

Clicker Books: How to Make a Clicker Book Using Clicker Books App v

Introduction to phylogenetic trees and tree-thinking Copyright 2005, D. A. Baum (Free use for non-commercial educational pruposes)

SUGGESTED LEARNING STRATEGIES:

Kentucky Academic Standards

1 - Black 2 Gold (Light) 3 - Gold. 4 - Gold (Rich Red) 5 - Black and Tan (Light gold) 6 - Black and Tan

Math 506 SN. Competency Two Uses Mathematical Reasoning. Mathematics Science Option. Secondary 5. Student Booklet

THE PIGEONHOLE PRINCIPLE AND ITS APPLICATIONS

Cane toads and Australian snakes

Finch Robot: snap levels 1-3

Lab 6: Energizer Turtles

PROBLEM SOLVING JUNIOR CIRCLE 01/09/2011

Human Genetics: Create-a-Person

Characteristics of the Text Genre Realistic fi ction Text Structure

Mandatory Assignment 07

Grade 5 English Language Arts

Bluefang. All-In-One Smart Phone Controlled Super Collar. Instruction Manual. US and International Patents Pending

b. vulnerablebreeds.csv Statistics on vulnerable breeds for the years 2003 through 2015 [1].

Code Documentation MFA (Movable Finite Automata) Eric Klemchak CS391/CS392

P M. Also Available! K J - H - C U U H C U F E.

Lesson 1.3. One Way Use compatible numbers. Estimate Sums Essential Question How can you use compatible numbers and rounding to estimate sums?

Lab 10: Color Sort Turtles not yet sorted by color

Microsoft Dexterity. Comprehensive Index Release 12

Scratch Jigsaw Method Feelings and Variables

Chapter 18: Categorical data

Genotype to Phenotype Simulation Booklet

Genotype to Phenotype Simulation Booklet

Genotype to Phenotype Simulation Booklet

Introduction and methods will follow the same guidelines as for the draft

Human Impact on Sea Turtle Nesting Patterns

PupDate. Lacey Rahmani UX

Call of the Wild. Investigating Predator/Prey Relationships

Grade 2 English Language Arts

CAT UNDERCARRIAGE SELECTION GUIDE. Helping you select the right undercarriage

Getting Started with Java Using Alice. 1 Copyright 2013, Oracle and/or its affiliates. All rights reserved.

Study Buddy. Based on the book by Louise Yates. Table of Contents

COYOTES and FOXES. Final Report. - Chantilly Fulgham, - Gracie Sanchez,

Teacher Edition. Lizard s Tail. alphakids. Written by Mark Gagiero Illustrated by Kelvin Hucker

Graphics libraries, PCS Symbols, Animations and Clicker 5

DIY POST MORTEM TECHNIQUE FOR CATTLEMEN

Collars, Harnesses & Leashes

FCI LT LM UNDERGROUND

Phenotypic and Genetic Variation in Rapid Cycling Brassica Parts III & IV

09/17/18 1 Turtlesim Cheat Sheet pages (PG) in book ROS Robotics By Example 2nd, Fairchild and Harman

Econometric Analysis Dr. Sobel

288 Seymour River Place North Vancouver, BC V7H 1W6

Check the box after reviewing with your staff. DNA Collection Kit (Cheek Swab) Mailing a DNA Cheek Swab to BioPet. Waste Sample Collection

Required and Recommended Supporting Information for IUCN Red List Assessments

Functional Skills ICT. Mark Scheme for A : Level 1. Oxford Cambridge and RSA Examinations

The ALife Zoo: cross-browser, platform-agnostic hosting of Artificial Life simulations

tucker turtle puppet D4F4F186CFFB4B124D1E8F6FE09AB085 Tucker Turtle Puppet

Trapped in a Sea Turtle Nest

Dog Project Training Curriculum

Genotype to Phenotype Simulation Booklet

Level 11. Book g. Level 11. Word Count 210 Text Type Information report High Frequency Word/s Introduced. The Snail Race Outside Games

Transcription:

Code, Draw, and 3D-Print with Turtle Tina

Code, Draw, and 3D-Print with Turtle Tina Pavel Solin Revision July 14, 2016

About the Author Dr. Pavel Solin is Professor of Applied and Computational Mathematics at the University of Nevada, Reno. He loves computers, computing, and open source software. He works on advanced computer simulation projects and is the author of several books and many scientific articles in international journals. He also wrote the complete Turtle functionality in Python. Acknowledgment We would like to thank many teachers for class-testing the course, and for providing useful feedback that helps us improve the textbook, the self-paced course, and the Turtle language itself. Graphics Design: TR-Design http://tr-design.cz Copyright: Copyright 2016 NCLab. All Rights Reserved.

Preface Turtle Tina 1 is an intermediate computer programming course that teaches basic programming logic and the syntax of the Python programming language. This course is a step up from Karel Coding that uses both simple logic and simple syntax, but it is simpler than the full Python programming course with full logic and full syntax. Using loops, nested loops, functions, and other programming concepts, the Turtle can draw beautiful patterns which in turn can be extruded for 3D printing: 1 This document was prepared using the LATEX module in NCLab

The Turtle can also draw contours in the XY plane and revolve them about the Y axis, creating rotational solids, shells, and surfaces:

In the 3D mode, the Turtle can swim in all three spatial directions and create awesome wireframe models. Of course they can be 3D-printed as well. This is super fun! Besides teaching you Python coding, the Turtle will allow you to develop strong spatial reasoning skills, unleash your creativity, build awesome 3D-printable designs, and participate in weekly NCLab competitions. Good luck! Pavel

Contents 1 Introduction 2 2 Drawing the first line 2 3 Default settings 2 4 Basic Turtle commands 3 4.1 NCLabTurtle()............................................ 3 4.2 show()................................................. 3 4.3 go(), forward(), fd()......................................... 3 4.4 left(), lt()............................................... 3 4.5 right(), rt().............................................. 3 4.6 penup(), pu()............................................. 3 4.7 pendown(), pd()........................................... 4 4.8 back(), backward(), bk()....................................... 4 4.9 color()................................................. 4 4.10 width()................................................ 4 4.11 height()................................................ 4 4.12 angle()................................................. 4 4.13 goto()................................................. 5 4.14 home()................................................. 5 4.15 hide(), invisible()........................................... 5 4.16 reveal(), visible()........................................... 5 4.17 arc().................................................. 5 5 Basic Python commands 6 5.1 The for-loop.............................................. 6 5.2 The if-else condition......................................... 6 5.3 The while-loop............................................ 7 5.4 Custom functions and the command def.............................. 7 6 Advanced Turtle commands 7 6.1 isdown()................................................ 7 6.2 setpos(), setposition()........................................ 7 6.3 setx()................................................. 8 6.4 sety()................................................. 8 6.5 getx()................................................. 8 6.6 gety()................................................. 8 6.7 getcolor()............................................... 8 6.8 getwidth()............................................... 8 6.9 getheight().............................................. 8 6.10 getangle()............................................... 9 7 3D modeling commands 9 7.1 extrude()............................................... 9 7.2 rosol()................................................. 9 7.3 rosurf()................................................ 9 7.4 roshell()................................................ 9 7.5 spiral()................................................. 10 7.6 export()................................................ 10

8 The 3D Turtle 10 8.1 NCLabTurtle3D().......................................... 10 8.2 Initial position............................................ 10 8.3 The bellyplane............................................ 11 8.4 left(), right()............................................. 11 8.5 up(), down()............................................. 12 8.6 roll().................................................. 13 8.7 Flight analogy - commands yaw(), pitch() and roll()....................... 14 8.8 Line types and the edges() command................................ 14 8.9 Difference in the goto() command.................................. 15 8.10 angles()................................................ 16 8.11 getangles().............................................. 16 8.12 printlines().............................................. 16 1

1 Introduction The Turtle Programming module in NCLab can be accessed via Free Apps Programming, and the selfpaced course Turtle Tina via Courses. This document does not replace the course - it is meant to serve as a reference material for those who already know Turtle Programming. The NCLab Turtle is a descendant of Logo - an educational programming language designed in 1967 by Daniel G. Bobrow, Wally Feurzeig, Seymour Papert and Cynthia Solomon. Due to its simplicity, many different implementations can be found on the web. The NCLab version is mostly compatible with the other ones out there, but it is unique for its ability to produce 3D pendants and even rotational 3D objects, and export them as STL files for 3D printing. Both the Turtle program files and the resulting STL files can be saved in your NCLab account, and you can publish them on the web. 2 Drawing the first line The following program creates a new NCLabTurtle named t at position (0, 0), moves it 60 steps forward, and shows the line that it has drawn. To run the program, press the green Play button. This is the corresponding output: 3 Default settings The default position of the Turtle is at the origin (0, 0) and its default angle is 0 (facing East). The default line width is 1, default line height is 0 (the trace is 2D in the XY plane) and default line color is blue. The red and green lines with arrows are the axes of the coordinate system (red = x, green = y). The size of the grid square is 10 10 steps. This size can be customized in Settings. Both the axes and the grid can be turned off in the menu. The turtle s pen is down by default. 2

4 Basic Turtle commands 4.1 NCLabTurtle() t = NCLabTurtle() creates a new NCLabTurtle named t at position (0, 0). Defaults from Section 3 apply. t = NCLabTurtle(40, 30) creates a new NCLabTurtle named t at position (40, 30). 4.2 show() t.show() displays the trace of turtle t as well as the turtle itself if visible (it is visible by default). 4.3 go(), forward(), fd() t.go(30) will move turtle t 30 steps forward. When the pen is down, the turtle will draw a line. 4.4 left(), lt() t.left(45) will turn turtle t left 45 degrees. 4.5 right(), rt() t.right(90) will turn turtle t right 90 degrees. 4.6 penup(), pu() t.penup() will lift pen of turtle t up. When turtle t moves after that, she will not be drawing a line. 3

4.7 pendown(), pd() t.pendown() will put pen of turtle t down. When turtle t moves after that, she will be drawing a line. When a new turtle is created, the pen is down by default. 4.8 back(), backward(), bk() t.back(50) will move turtle t back 50 steps. NOTE: The turtle is not drawing while backing. 4.9 color() t.color(magenta) will set the color of turtle t to magenta. There are many predefined colors like this - if you can name a color, chances are that it will be available. Use all capital letters for colors. In addition, it is possible to define custom colors as triplets of RGB values. For example, [0, 0, 0] is the same as BLACK, [180, 0, 0] is the same as RED, [255, 0, 0] is the same as LIGHTRED, etc. When a new turtle is created, its default color is BLUE. 4.10 width() t.width(2) will set the line width of turtle t to 2. When a new turtle is created, its default line width is 1. 4.11 height() t.height(5) will set the (vertical) line height of turtle t to 5. When a new turtle is created, its default line height is 0. 4.12 angle() t.angle(30) will set the angle of turtle t to 45 degrees. When a new turtle is created, its default angle is 0 (facing East). Angle 90 faces North, angle 180 faces West, angle -90 faces South, etc. 4

4.13 goto() t.goto(50, 20) will move turtle t to coordinates (50, 20). If pen is down, then the turtle will draw a line. The turtle s final angle will depend on where she came from. 4.14 home() t.home() will move turtle t to coordinates (0, 0) and set angle to 0 degrees (facing East). The turtle is not drawing while going home. 4.15 hide(), invisible() t.hide() will make sure that turtle t is not displayed when t.show() is called. 4.16 reveal(), visible() t.reveal() will make sure that turtle t is displayed when t.show() is called. 4.17 arc() t.arc(120, 50, l ) will draw a left arc of angle 120 degrees and radius 50. t.arc(60, 10, r ) will draw a right arc of angle 60 degrees and radius 10. 5

5 Basic Python commands 5.1 The for-loop The for-loop is part of the Python programming language. The program for i in range(5): print(i) will print the numbers 0, 1, 2, 3, 4 one number per line: 0 1 2 3 4 Here i is the counting index. In the first cycle i = 0, in the second cycle, i = 1 etc. Now let s apply it to the Turtle. The program for j in range(4): tina.go(50) tina.left(90) makes the Turtle tina draw a square of edge length 10 steps. In other words, the turtle will go 10 steps forward, then turn left 90 degrees, and after that the same is repeated three more times. 5.2 The if-else condition The if-else condition is another basic element of the Python programming language. The program a = 7 if a > 0: print("the number a is positive.") else: print("the number a is negative or zero.") will print The number a is positive. And the program a = -2 if a > 0: print("the number a is positive.") else: print("the number a is negative or zero.") will print The number a is negative or zero. 6

5.3 The while-loop The while-loop is suitable for situations when it is not known how many repetitions will be needed. Such as, for example, when we want to add numbers 1, 2, 3, 4, 5,... etc. until their sum exceeds 1000: sum = 0 counter = 1 while sum < 1000: sum += counter counter += 1 print("sum =", sum) On line 1, a variable named sum is initialized with zero. On line 2, variable counter is initialized with 1. Line 3 checks if sum is less than 1000. If so, lines 4 and 5 and executed, and the program returns to line 3. If not, then the loop ends and the program continues to line 6 where the sum is printed. On line 4, the current value of the counter is added to the sum, and on line 5 the counter is increased by one. 5.4 Custom functions and the command def The command def is used to define custom functions. What is a custom function? Sometimes there is a group of actions that you need to do more than once. And perhaps in different parts of the main program. Then it makes sense to create a custom function for that group of commands. For example, the following custom function hexagon(t, s) will make Turtle T draw a haxagon of side length s: def hexagon(t, s): for i in range(6): T.go(s) T.left(60) As always, do not forget the colons at the end of lines 1 and 2, and pay good attention to the indentation. 6 Advanced Turtle commands 6.1 isdown() t.isdown() will return True is turtle t s pen is down, and False otherwise. 6.2 setpos(), setposition() t.setpos(10, 20) is the same as t.goto(10, 20) 7

6.3 setx() t.setx(20) will move turtle t to x-coordinate 20 while leaving its y-coordinate unchanged. 6.4 sety() t.sety(30) will move turtle t to y-coordinate 30 while leaving its x-coordinate unchanged. 6.5 getx() t.getx() will return the turtle s x-coordinate. 6.6 gety() t.gety() will return the turtle s y-coordinate. 6.7 getcolor() t.getcolor() will return the turtle s color. 6.8 getwidth() t.getwidth() will return the turtle s line width. 6.9 getheight() t.getheight() will return the turtle s (vertical) line height. 8

6.10 getangle() t.getangle() will return the turtle s angle. 7 3D modeling commands 7.1 extrude() t.extrude(5) will extrude the turtle s trace to 3D, making it a 3D object of thickness 5. 7.2 rosol() t.rosol() will revolve the turtle s trace about the y-axis, creating a rotationally-symmetric 3D solid. NOTE: The turtle s trace should be in the first quadrant for this command to work properly. Optional parameters are angle (default value 360 degrees) and angular subdivision (default value 36). 7.3 rosurf() t.rosurf() will revolve the turtle s trace about the y-axis, creating a rotationally-symmetric 3D surface. 3D surfaces are paper-thin and so they cannot be 3D-printed. NOTE: The turtle s trace should be in the first quadrant for this command to work properly. Optional parameters are angle (default value 360 degrees) and angular subdivision (default value 36). 7.4 roshell() Formerly this command was called revolve(). t.roshell() will revolve the turtle s trace about the y-axis, creating a rotationally-symmetric 3D shell. NOTE: The turtle s trace should be in the first quadrant for this command to work properly. Be careful: Rotational shells have more than twice the number of facets than rotational solids or surfaces. Therefore, this operation may take a longer computing time. Optional parameters are angle (default value 360 degrees) and angular subdivision (default value 36). 9

7.5 spiral() t.spiral(720, 30, 48) will spiral the turtle s trace about the y-axis. 720 means two full revolutions, 30 means an elevation gain 30 per revolution, and 48 is the angular subdivision per revolution. 7.6 export() With the program out = t.export() SHOW(out) Turtle t will export its 2D or 3D geometry to an object named out. This object can then be used as part of a more complex 2D or 3D model. In the above example, it is just displayed. 8 The 3D Turtle The 3D Turtle (in the following we ll call it just Turtle) allows you to move and draw in all three spatial directions X, Y and Z. Most commands are the same, so let s just focus on the new ones. 8.1 NCLabTurtle3D() To create a new 3D Turtle t, type t = NCLabTurtle3D() 8.2 Initial position When the Turtle is created, she stands at the origin (0, 0, 0) and faces East (positive X direction). Her belly lies in the XY plane. This is the same in 3D as it was in 2D: Recall that the X axis is red, Y axis green, and Z axis blue. 10

8.3 The bellyplane The Turtle can turn left and right in the 3D space exactly in the same way as she did in the XY plane. She is turning in the plane that is associated with her belly let s call it the bellyplane. In the image above, her bellyplane is the XY plane. That s the same as it was in 2D, and therefore turning in that plane is exactly the same as turning in 2D. But in the image below, the Turtle faces the positive Z direction and her bellyplane is the YZ plane. 8.4 left(), right() Consider again the last image above the Turtle faces the positive Z direction and her bellyplane is the YZ plane. Now when she turns left 90 degrees, t.left(90) she will turn in her bellyplane, and face the positive Y direction: We are using 90 degree angles here for simplicity, of course the Turtle can use arbitrary angles. If she turns 90 degrees right instead, t.right(90) she will be facing the negative Y direction: 11

8.5 up(), down() The up() and down() commands are again relative to the Turtle s bellyplane. Consider the last image above. Now when she turns up 90 degrees, t.left(90) she will be facing the negative X direction and her bellyplane will be the XZ plane: 12

When she turns down 90 degrees instead, t.down(90) she will be facing the positive X direction, her bellyplane will be the XZ plane again, but her body will be on the opposite side of the XZ plane: 8.6 roll() The Turtle uses the roll() command to roll about her axis exactly in the same way airplanes do. Consider the last image above. With the command t.roll(90, l ) she will be facing positive X direction and her bellyplane will be the XY plane: The l in the roll() command stands for to the left. She can also roll to the right using r as the second parameter of the roll() command. When she rolls 90 degrees to the right instead, 13

t.roll(90, r ) she will be facing positive X direction, her bellyplane will be the XY plane, but her body will be on the opposite side of the XY plane: 8.7 Flight analogy - commands yaw(), pitch() and roll() An airplane can turn in three different ways using yaw, pitch and roll. You already know how the roll() command works. The yaw() command is doing the same thing as left() and right(). When used with a positive angle, such as yaw(45), the Turtle will turn to the left. When used with a negative angle, such as yaw(-30), the Turtle turns to the right. The pitch() command is doing the same thing as up() and down(). When used with a positive angle, such as pitch(10), the Turtle will lift her nose up. When used with a negative angle, such as pitch(20), the Turtle turns her nose down. 8.8 Line types and the edges() command The default cross-section of the Turtle s 3D line is an octagon: This can be changed using the edges() command. For example, with t.edges(6) the cross-section will be a hexagon: 14

And with t.edges(4) it will be a square: and so on. The lowest number of edges is three. 8.9 Difference in the goto() command The goto() command accepts three coordinates of the end point, as expected. But there is one more difference: When the Turtle arrives at the target point, she will reset all her angles to zero. In other words, her angles will be as if she was just created. To illustrate this, the code t = NCLabTurtle3D() t.color(yellow) t.goto(0, 0, 30) t.show() will produce: 15

8.10 angles() The command angles(leftangle, upangle, rollangle) will set the Turtle s three angles. Here leftangle is the angle between her axis and the XZ plane, upangle is the angle between her axis and the XY plane, and rollangle is her roll angle about her axis. Initially all three are zero. For example, the following line will set these angles to 45, 30 and 20, respectively: t.angles(45, 30, 20) 8.11 getangles() This command can be used to find the Turtle s three angles in degrees. Sample use: aleft, aup, aroll = t.getangles() 8.12 printlines() This command is useful for debugging. It will print in text form all the lines where the Turtle went. For example, the code t = NCLabTurtle3D() t.go(30) t.up(90) t.go(20) t.left(90) t.go(10) t.printlines() will produce the output: --- Start: 0 0 0 End: 30 0 0 X: 1 0 0 Y: 0 1 0 Z: 0 0 1 --- Start: 30 0 0 End: 30.0 0.0 20.0 X: 0.0 0.0 1.0 Y: 0 1 0 16

Z: -1.0 0.0 0.0 --- Start: 30.0 0.0 20.0 End: 30.0 10.0 20.0 X: 0.0 1.0 0.0 Y: -0.0 0.0-1.0 Z: -1.0 0.0 0.0 Here Start and End are the starting and end points of each line, and the vectors X, Y, Z are three unit vectors that define the local coordinate system associated with the Turtle. 17