|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Turtle Graphics in PowerShell |
| 4 | +.EXAMPLE |
| 5 | + .\README.md.ps1 > .\README.md |
| 6 | +#> |
| 7 | +#requires -Module Turtle |
| 8 | +param() |
| 9 | + |
| 10 | + |
| 11 | +@" |
| 12 | +# Turtle |
| 13 | +
|
| 14 | +<div align='center'> |
| 15 | + <img src='./Examples/SierpinskiTriangle.svg' alt='SierpinskiTriangle' width='50%' /> |
| 16 | +</div> |
| 17 | +
|
| 18 | +
|
| 19 | +
|
| 20 | +## Turtles in a PowerShell |
| 21 | +
|
| 22 | +[Turtle Graphics](https://en.wikipedia.org/wiki/Turtle_graphics) are a great way to learn programming and describe shapes. |
| 23 | +
|
| 24 | +Turtle graphics start really simple. |
| 25 | +
|
| 26 | +Imagine we are a turtle dragging a pen. |
| 27 | +
|
| 28 | +We can draw almost any shape by moving. |
| 29 | +
|
| 30 | +We can only really move in two ways: |
| 31 | +
|
| 32 | +We can turn, and we can take a step forward. |
| 33 | +
|
| 34 | +Turtle graphics starts with these two operations: |
| 35 | +
|
| 36 | +* `Rotate()` rotates the turtle |
| 37 | +* `Forward()` moves forward |
| 38 | +
|
| 39 | +We can easily represent these steps in memory, and draw them within a webpage using SVG. |
| 40 | +
|
| 41 | +We can implement Turtle in any language. |
| 42 | +
|
| 43 | +This module implements Turtle in PowerShell. |
| 44 | +"@ |
| 45 | + |
| 46 | +@" |
| 47 | +### Installing and Importing |
| 48 | +
|
| 49 | +We can install Turtle from the PowerShell Gallery: |
| 50 | +
|
| 51 | +~~~PowerShell |
| 52 | +$({Install-Module Turtle -Scope CurrentUser -Force}) |
| 53 | +~~~ |
| 54 | +
|
| 55 | +Then we can import it like any other module |
| 56 | +
|
| 57 | +~~~PowerShell |
| 58 | +$({Import-Module Turtle -Force -PassThru}) |
| 59 | +~~~ |
| 60 | +
|
| 61 | +#### Cloning and Importing |
| 62 | +
|
| 63 | +You can also clone the repository and import the module |
| 64 | +
|
| 65 | +~~~PowerShell |
| 66 | +$({ |
| 67 | +git clone https://github.com/PowerShellWeb/Turtle |
| 68 | +cd ./Turtle |
| 69 | +Import-Module ./ -Force -PassThru |
| 70 | +}) |
| 71 | +~~~ |
| 72 | +"@ |
| 73 | + |
| 74 | +@" |
| 75 | +### Getting Started |
| 76 | +
|
| 77 | +Once we've imported Turtle, we can create any number of turtles, and control them with commands and methods. |
| 78 | +
|
| 79 | +#### Drawing Squares |
| 80 | +
|
| 81 | +<div align='center'> |
| 82 | + $( |
| 83 | + $null = Get-Turtle Square 10 | |
| 84 | + Set-Turtle -Property Stroke '#4488ff' | |
| 85 | + Save-Turtle -Path ./Examples/Square.svg |
| 86 | + ) |
| 87 | + <img src='./Examples/Square.svg' alt='Square' width='50%' /> |
| 88 | +</div> |
| 89 | +
|
| 90 | +
|
| 91 | +Let's start simple, by drawing a square. |
| 92 | +
|
| 93 | +~~~PowerShell |
| 94 | +$( |
| 95 | +$drawSquare1 = { |
| 96 | +New-Turtle | |
| 97 | + Move-Turtle Forward 10 | |
| 98 | + Move-Turtle Rotate 90 | |
| 99 | + Move-Turtle Forward 10 | |
| 100 | + Move-Turtle Rotate 90 | |
| 101 | + Move-Turtle Forward 10 | |
| 102 | + Move-Turtle Rotate 90 | |
| 103 | + Move-Turtle Forward 10 | |
| 104 | + Move-Turtle Rotate 90 | |
| 105 | + Save-Turtle "./Square.svg" |
| 106 | +} |
| 107 | +$drawSquare1 |
| 108 | +) |
| 109 | +~~~ |
| 110 | +
|
| 111 | +"@ |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +@' |
| 116 | +We can also write this using a method chain: |
| 117 | +
|
| 118 | +~~~PowerShell |
| 119 | +$turtle = New-Turtle |
| 120 | +$turtle. |
| 121 | + Forward(10).Rotate(90). |
| 122 | + Forward(10).Rotate(90). |
| 123 | + Forward(10).Rotate(90). |
| 124 | + Forward(10).Rotate(90). |
| 125 | + Symbol.Save("$pwd/Square.svg") |
| 126 | +~~~ |
| 127 | +
|
| 128 | +This just demonstrates how we can construct shapes out of these two simple primitive steps. |
| 129 | +'@ |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +@' |
| 134 | +We can also just say, make a square directly: |
| 135 | +
|
| 136 | +~~~PowerShell |
| 137 | +New-Turtle | Move-Turtle Square 10 | Save-Turtle ./Square.svg |
| 138 | +~~~ |
| 139 | +'@ |
| 140 | + |
| 141 | +@' |
| 142 | +We can use loops: |
| 143 | +
|
| 144 | +~~~PowerShell |
| 145 | +$turtle = New-Turtle |
| 146 | +
|
| 147 | +foreach ($n in 1..6) { |
| 148 | + $turtle = $turtle.Forward(10).Rotate(60) |
| 149 | +} |
| 150 | +
|
| 151 | +$turtle | |
| 152 | + Save-Turtle "./Hexagon.svg" |
| 153 | +~~~ |
| 154 | +'@ |
| 155 | + |
| 156 | +@" |
| 157 | +<div align='center'> |
| 158 | +$( |
| 159 | +$null = turtle ('Forward', 10, 'Rotate', 60 * 6) | |
| 160 | + Set-Turtle -Property Stroke '#4488ff' | |
| 161 | + Save-Turtle -Path ./Examples/Hexagon.svg |
| 162 | +) |
| 163 | +<img src='./Examples/Hexagon.svg' alt='Hexagon' width='50%' /> |
| 164 | +</div> |
| 165 | +"@ |
| 166 | + |
| 167 | + |
| 168 | +@" |
| 169 | +Because this Turtle generates SVG, we can also use it to create patterns. |
| 170 | +"@ |
| 171 | + |
| 172 | +$MakeHexagonPattern = { |
| 173 | + turtle ('Forward', 10, 'Rotate', 60 * 6) | |
| 174 | + Set-Turtle -Property Stroke '#4488ff' | |
| 175 | + Save-Turtle -Path ./Examples/HexagonPattern.svg -Property Pattern |
| 176 | +} |
| 177 | + |
| 178 | + |
| 179 | +@" |
| 180 | +
|
| 181 | +~~~PowerShell |
| 182 | +$MakeHexagonPattern |
| 183 | +~~~ |
| 184 | +"@ |
| 185 | + |
| 186 | +$HexPattern = . $MakeHexagonPattern |
| 187 | + |
| 188 | +@" |
| 189 | +<div align='center'> |
| 190 | +<img src='./Examples/$($HexPattern.Name)' alt='Hexagon Pattern' width='50%' /> |
| 191 | +</div> |
| 192 | +"@ |
| 193 | + |
| 194 | + |
| 195 | +@" |
| 196 | +Speaking of patterns, Turtle is often used to draw fractals. |
| 197 | +
|
| 198 | +This implementation of Turtle has quite a few built-in fractals. |
| 199 | +
|
| 200 | +For example, here is an example of a pattern comprised of Koch Snowflakes: |
| 201 | +"@ |
| 202 | + |
| 203 | +$MakeSnowflakePattern = { |
| 204 | + turtle KochSnowflake 2.5 4 | |
| 205 | + Set-Turtle -Property StrokeWidth '0.1%' | |
| 206 | + Set-Turtle -Property Stroke '#4488ff' | |
| 207 | + Set-Turtle -Property PatternTransform -Value @{scale = 0.5 } | |
| 208 | + Save-Turtle -Path ./Examples/KochSnowflakePattern.svg -Property Pattern |
| 209 | +} |
| 210 | + |
| 211 | + |
| 212 | +@" |
| 213 | +
|
| 214 | +~~~PowerShell |
| 215 | +$MakeSnowflakePattern |
| 216 | +~~~ |
| 217 | +"@ |
| 218 | + |
| 219 | +$SnowFlakePattern = . $MakeSnowflakePattern |
| 220 | + |
| 221 | +@" |
| 222 | +<div align='center'> |
| 223 | +<img src='./Examples/$($SnowFlakePattern.Name)' alt='Snowflake Pattern' width='50%' /> |
| 224 | +</div> |
| 225 | +"@ |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | + |
| 234 | +"" |
| 235 | + |
| 236 | + |
| 237 | +"" |
| 238 | + |
0 commit comments