Installing

Install-Module Turtle

Updating

Install-Module Turtle -Force

Importing

Import-Module Turtle

Basics

turtle polygon 42 6

More Examples


Turtle

Get-Turtle

Get-Turtle

Gets Turtles

Gets turtles in a PowerShell.

Turtle Graphics are pretty groovy.

They have been kicking it since 1966, and they are how computers first learned to draw.

They kicked off the first computer-aided design boom and inspired generations of artists, engineers, mathematicians, and physicists.

They are also incredibly easy to build.

A Turtle graphic is described with a series of moves.

Let's start with the core three moves:

Imagine you are a Turtle holding a pen.

These are the three basic moves a turtle can make.

We can describe more complex moves by combining these steps.

Each argument can be the name of a move of the turtle object.

After a member name is encountered, subsequent arguments will be passed to the member as parameters.

Any parameter that begins with whitespace will be split into multiple words.

We can write shapes as a series of steps

turtle "
    rotate 120
    forward 42 
    rotate 120 
    forward 42 
    rotate 120 
    forward 42
"

We can also use a method. Polygon will draw an an N-sided polygon.

turtle polygon 10 5

A simple case of this is a square

turtle square 42

If we rotate 45 degrees first, our square becomes a rhombus

turtle rotate 45 square 42

We can draw a circle

turtle circle 10

Or a pair of half-circles

turtle circle 10 0.5 rotate 90 circle 10 0.5

We can multiply arrays in PowerShell this can make composing complex shapes easier. Let's take the previous example and repeat it 8 times.

turtle @('circle',42,0.5,'rotate',90 * 8)

Let's make a triangle by multiplying steps

turtle ('forward', 10, 'rotate', 120 * 3)

We can also write this with a polygon

turtle polygon 10 3

Let's make a series of polygons, decreasing in size

turtle polygon 10 6 polygon 10 5 polygon 10 4

We can also use a loop to produce a series of steps Let's extend our previous example and make 9 polygons

turtle @(
    foreach ($n in 12..3) {
        'polygon'
        42
        $n
    }
)

We can use the same trick to make successively larger polygons

turtle @(
    $sideCount = 3..8 | Get-Random 
    foreach ($n in 1..5) {
        'polygon'
        $n * 10
        $sideCount
    }
)

We can reflect a shape by drawing it with a negative number

turtle polygon 42 3 polygon -42 3

We can change the angle of reflection by rotating first

turtle rotate 60 polygon 42 3 polygon -42 3

We can morph any N shapes with the same number of points.

turtle square 42 morph @(
    turtle square 42
    turtle rotate 45 square 42
    turtle square 42
)

Reflections always have the same number of points.

Morphing a shape into its reflection will zoom out, flip, and zoom back in.

turtle polygon 42 6 morph @(
    turtle polygon -42 6
    turtle polygon 42 6
    turtle polygon -42 6
)

If we want to morph a smaller shape into a bigger shape,

we can duplicate lines

turtle polygon 21 6 morph @(
    turtle @('forward', 21,'backward', 21 * 3)
    turtle polygon 21 6
    turtle @('forward', 21,'backward', 21 * 3)
)

We can repeat steps by multiplying arrays. Lets repeat a hexagon three times with a rotation

turtle ('polygon', 23, 6, 'rotate', -120 * 3)

Let's change the angle a bit and see how they overlap

turtle ('polygon', 23, 6, 'rotate', -60 * 6)

Let's do the same thing, but with a smaller angle

turtle ('polygon', 23, 6, 'rotate', -40 * 9)

A flower is a series of repeated polygons and rotations

turtle Flower

Flowers look pretty with any number of polygons

turtle Flower 50 10 (3..12 | Get-Random) 36

Flowers get less dense as we increase the angle and decrease the repetitions

turtle Flower 50 15 (3..12 | Get-Random) 24

Flowers get more dense as we decrease the angle and increase the repetitions.

turtle Flower 50 5 (3..12 | Get-Random) 72

Flowers look especially beautiful as they morph

$sideCount = (3..12 | Get-Random)        
turtle Flower 50 15 $sideCount 36 morph @(
    turtle Flower 50 10 $sideCount 72
    turtle rotate (                
        Get-Random -Max 360 -Min -360
    ) Flower 50 5 $sideCount 72
    turtle Flower 50 10 $sideCount 72
)

We can draw a pair of arcs and turn back after each one.

We call this a 'petal'.

turtle rotate -30 Petal 42 60

We can construct a flower out of petals

turtle FlowerPetal

Adjusting the angle of the petal makes our petal wider or thinner

turtle FlowerPetal 42 15 (20..60 | Get-Random) 24

Flower Petals get more dense as we decrease the angle and increase repetitions

turtle FlowerPetal 42 10 (10..50 | Get-Random) 36

Flower Petals get less dense as we increase the angle and decrease repetitions

turtle FlowerPetal 50 20 (20..72 | Get-Random) 18

Flower Petals look amazing when morphed

$Radius = 23..42 | Get-Random
$flowerAngle = 30..60 | Get-Random
$AngleFactor = 2..6 | Get-Random
$StepCount = 36
$flowerPetals =
    turtle rotate (
        (Get-Random -Max 180) * -1
    ) flowerPetal $radius 10 $flowerAngle $stepCount
$flowerPetals2 =
    turtle rotate (
        (Get-Random -Max 180)
    ) flowerPetal $radius (
        10 * $AngleFactor
    ) $flowerAngle $stepCount
turtle flowerPetal $radius 10 $flowerAngle $stepCount morph (
    $flowerPetals, 
    $flowerPetals2,
    $flowerPetals
)

We can create a Star with N points

turtle star 42 5
turtle star 42 6

turtle star 42 7

turtle star 42 8

Stars look spectacular when we rotate and repeat them

turtle @('star',42,5,'rotate',72 * 5)
turtle @('star',42,6,'rotate',60 * 6)

turtle @('star',42,7,'rotate',(360/7) * 7)

turtle @('star',42,8,'rotate',45 * 8)

When we do this, we call it a Star Flower

turtle StarFlower 42

turtle StarFlower 42 30 6 12

turtle StarFlower 42 (360/7) 7 7

turtle StarFlower 42 45 8 8

StarFlowers look spectacular when morphed

turtle StarFlower 42 45 8 24 morph @(
    turtle StarFlower 42 45 8 24
    turtle StarFlower 42 15 8 24
    turtle StarFlower 42 45 8 24
)

We can rotate the points we morph into.

turtle StarFlower 42 45 8 24 morph @(
    turtle StarFlower 42 45 8 24
    turtle rotate (Get-Random -Max 360) StarFlower 42 15 8 24
    turtle StarFlower 42 45 8 24
)

We can mix the number of points in a star flower morph

(as long as we're drawing the same number of points)

turtle StarFlower 42 12 5 30 morph @(
    turtle StarFlower 42 12 5 30
    turtle rotate (
        Get-Random -Max 360 -Min -360
    ) StarFlower 42 14.4 6 25
    turtle StarFlower 42 12 5 30
)

We can construct a 'scissor' by drawing two lines at an angle

turtle Scissor 42 60

Drawing a scissor does not change the heading

We can create a zig-zag pattern by multiplying scissors

turtle @('Scissor',42,60 * 4)

Getting a bit more interesting, we can create a polygon out of scissors

We will continually rotate until we have turned a multiple of 360 degrees.

Turtle ScissorPoly 23 90 120

Turtle ScissorPoly 23 60 72

This can get very chaotic, if it takes a while to reach a multiple of 360

Let's build a dozen scissor polygons.

foreach ($n in 60..72) {
    Turtle ScissorPoly 16 $n $n
}

We can draw an outward spiral by growing a bit each step

turtle StepSpiral

turtle StepSpiral 42 120 4 18

Because Step Spirals are a fixed number of steps,
they are easy to morph.

turtle StepSpiral 42 120 4 18 morph @(
    turtle StepSpiral 42 90 4 24
    turtle StepSpiral 42 120 4 24
    turtle StepSpiral 42 90 4 24            
)

turtle @('StepSpiral',3, 120, 'rotate',60 * 6)

turtle @('StepSpiral',3, 90, 'rotate',90 * 4)

Step spirals look lovely when morphed

(especially when reversing angles)

turtle @('StepSpiral',3, 120, 'rotate',60 * 6) morph @(
    turtle @('StepSpiral',3, 120, 'rotate',60 * 6)
    turtle @('StepSpiral',6, -120, 'rotate',120 * 6)
    turtle @('StepSpiral',3, 120, 'rotate',60 * 6)
)

When we reverse the spiral angle, the step spiral curve flips

turtle @('StepSpiral',3, 90, 'rotate',90 * 4) morph @(
    turtle @('StepSpiral',3, 90, 'rotate',90 * 4)
    turtle @('StepSpiral',3, -90, 'rotate',90 * 4)
    turtle @('StepSpiral',3, 90, 'rotate',90 * 4)
)

When we reverse the rotation, the step spiral curve slides

turtle @('StepSpiral',3, 90, 'rotate',90 * 4) morph @(
    turtle @('StepSpiral',3, 90, 'rotate',90 * 4)
    turtle @('StepSpiral',3, 90, 'rotate',-90 * 4)
    turtle @('StepSpiral',3, 90, 'rotate',90 * 4)
)

We we alternate, it looks amazing

turtle @('StepSpiral',3, 90, 'rotate',90 * 4) morph @(
    turtle @('StepSpiral',3, 90, 'rotate',90 * 4)
    turtle @('StepSpiral',3, 90, 'rotate',-90 * 4)
    turtle @('StepSpiral',3, 90, 'rotate',90 * 4)
    turtle @('StepSpiral',3, -90, 'rotate',90 * 4)
    turtle @('StepSpiral',3, 90, 'rotate',90 * 4)            
)

turtle @('StepSpiral',3, 120, 'rotate',60 * 6) morph @(
    turtle @('StepSpiral',3, 120, 'rotate',60 * 6)
    turtle @('StepSpiral',6, -120, 'rotate',120 * 6)
    turtle @('StepSpiral',3, 120, 'rotate',60 * 6)
    turtle @('StepSpiral',6, 120, 'rotate',-120 * 6)
    turtle @('StepSpiral',3, 120, 'rotate',60 * 6)
)

turtle spirolateral

turtle spirolateral 50 60 10

turtle spirolateral 50 120 6 @(1,3)

turtle spirolateral 23 144 8

turtle spirolateral 23 72 8

Lets get practical. Turtle can easily make a bar graph.

turtle BarGraph 200 300 (1..10)

Want a vertical bar graph? Rotate first.

turtle rotate 90 BarGraph 200 300 (1..10)

Let's provide more random points:

turtle rotate 90 BarGraph 200 300 (1..20 | Get-Random -Count 20)

We can draw pretty pictures by connecting and rotating graphs

turtle @(
    'BarGraph', 200, 300, (1..10),
    'BarGraph', 200, 300, (10..1),
    'rotate',180 * 2
)

Turtle can draw a number of fractals

turtle BoxFractal 42 4

We can make a Board Fractal

turtle BoardFractal 42 4

We can make a Crystal Fractal

turtle CrystalFractal 42 4

We can make ring fractals

turtle RingFractal 42 4

We can make a Pentaplexity

turtle Pentaplexity 42 3

We can make a Triplexity

turtle Triplexity 42 4

We can draw the Koch Island

turtle KochIsland 42 4

Or we can draw the Koch Curve

turtle KochCurve 42

We can make a Koch Snowflake

turtle KochSnowflake 42

We can draw the Levy Curve

turtle LevyCurve 42 6

We can use a Hilbert Curve to fill a space

Turtle HilbertCurve 42 4

We can use a Moore Curve to fill a space with a bit more density.

turtle MooreCurve 42 4

We can rotate and repeat moore curves, giving us even Moore.

turtle @('MooreCurve', 42, 4, 'Rotate', 90 * 4)

We can show a binary tree

turtle BinaryTree 42 4

We can also mimic plant growth

turtle FractalPlant 42 4

The SierpinskiArrowHead Curve is pretty

turtle SierpinskiArrowheadCurve 42 4

The SierpinskiTriangle is a Fractal classic

turtle SierpinskiTriangle 42 4

We can morph with no parameters to try to draw step by step

This will result in large files.

This may not work in all browsers for all graphics.

turtle SierpinskiTriangle 42 3 morph

Let's draw two reflected Sierpinski Triangles

turtle @(
    'rotate', 60
    'SierpinskiTriangle', 42, 4
    'SierpinskiTriangle', -42, 4
)

Now let's draw a dozen reflected Sierpinski Triangles

turtle @(
    'rotate', 60,
    'SierpinskiTriangle', 42, 4,
    'SierpinskiTriangle', -42, 4,
    'rotate', 30 *
        12
)

We can draw a 'Sierpinski Snowflake' with multiple Sierpinski Triangles.

turtle @('rotate', 30, 'SierpinskiTriangle',42,4 * 12)

turtle @('rotate', 45, 'SierpinskiTriangle',42,4 * 24)