Skip to content

Commit 9fc3954

Browse files
feat: Turtle.Spiderweb ( Fixes #290 )
1 parent 2619c83 commit 9fc3954

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

Types/Turtle/Spiderweb.ps1

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<#
2+
.SYNOPSIS
3+
Draws a spiderweb with a Turtle
4+
.DESCRIPTION
5+
Tells our Turtle to draw a spiderweb.
6+
7+
This will draw any number of spokes, and draw a polygon between each spoke at regular intervals
8+
.EXAMPLE
9+
# Draw a spiderweb
10+
turtle spiderweb
11+
.EXAMPLE
12+
# Draw a spider web with a radius six
13+
# containing six rings
14+
# along six spokes
15+
turtle spiderweb 6 6 6 show
16+
.EXAMPLE
17+
# Draw a random spiderweb
18+
turtle rotate (
19+
Get-Random -Max 360
20+
) web 42 (
21+
Get-Random -Min 3 -Max 13
22+
) (
23+
Get-Random -Min 3 -Max 13
24+
) save ./RandomWeb.svg show
25+
.EXAMPLE
26+
turtle rotate (
27+
Get-Random -Max 360
28+
) web 42 (
29+
Get-Random -Min 3 -Max 13
30+
) (
31+
Get-Random -Min 3 -Max (13 * 3)
32+
) pathAnimation (
33+
[Ordered]@{
34+
type = 'rotate' ; values = 0, 360 ;repeatCount = 'indefinite'; dur = "41s"
35+
}
36+
) save ./RandomWebRotate.svg show
37+
.EXAMPLE
38+
turtle rotate (
39+
Get-Random -Max 360
40+
) web 42 (
41+
Get-Random -Min 3 -Max 13
42+
) (
43+
Get-Random -Min 3 -Max (13 * 3)
44+
) pathAnimation (
45+
[Ordered]@{
46+
type = 'rotate' ; values = 0, 360 ;repeatCount = 'indefinite'; dur = "41s"
47+
}
48+
) backgroundcolor 'black' stroke 'yellow' pathclass 'yellow-stroke' save ./RandomWebRotateColor.svg show
49+
.EXAMPLE
50+
turtle rotate (
51+
Get-Random -Max 360
52+
) web 42 (
53+
Get-Random -Min 3 -Max 13
54+
) (
55+
Get-Random -Min 3 -Max (13 * 3)
56+
) morph save ./RandomWebStepMorph.svg
57+
.EXAMPLE
58+
$spokes = Get-Random -Min 3 -Max 13
59+
$rings = Get-Random -Min 3 -Max (13 * 3)
60+
turtle web 42 $spokes $rings morph @(
61+
turtle web 42 $spokes $rings
62+
turtle rotate (
63+
Get-Random -Max 360
64+
) web 42 $spokes $rings
65+
turtle web 42 $spokes $rings
66+
) save ./RandomWebMorph.svg
67+
.EXAMPLE
68+
$spokes = Get-Random -Min 3 -Max 13
69+
$rings = Get-Random -Min 3 -Max (13 * 3)
70+
turtle web 42 $spokes $rings morph @(
71+
turtle web 42 $spokes $rings
72+
turtle rotate (
73+
Get-Random -Max 360
74+
) web 42 $spokes $rings
75+
turtle web 42 $spokes $rings
76+
) backgroundcolor 'black' stroke 'yellow' pathclass 'yellow-stroke' save ./RandomWebMorphColor.svg
77+
78+
#>
79+
param(
80+
# The radius of the web
81+
[double]
82+
$Radius = 42,
83+
84+
# The number of spokes in the web.
85+
[int]
86+
$SpokeCount = 6,
87+
88+
# The number of rings in the web.
89+
[int]
90+
$RingCount = 6
91+
)
92+
93+
94+
# If there were no spokes or rings, return this
95+
if ($spokeCount -eq 0 ) { return $this }
96+
if ($RingCount -eq 0 ) { return $this }
97+
98+
99+
# Determine the angle of the spokes
100+
$spokeAngle = 360 / $SpokeCount
101+
102+
# And draw each spoke.
103+
foreach ($n in 1..$([Math]::Abs($SpokeCount))) {
104+
$this = $this.Forward($radius).Backward($radius).Rotate($spokeAngle)
105+
}
106+
107+
# Now we have the structure of our web, and we are at the center.
108+
$center =
109+
[Numerics.Vector2]::new($this.X, $this.Y)
110+
111+
112+
# Each ring we want to grow the in radius
113+
$radiusStep = $radius / $RingCount
114+
$inRadius = 0
115+
116+
# Starting from the center, we want to try to make a series of rings to each next point in the web
117+
foreach ($ringNumber in 1..$([Math]::Abs($RingCount))) {
118+
$inRadius+=$radiusStep
119+
# First, move along our spoke
120+
$null = $this.Forward($radiusStep)
121+
122+
# Then get our bearings
123+
$heading = $this.Heading
124+
125+
# and imagine points around a circle, along each of our spokes
126+
$webPoints = @(
127+
foreach ($spokeNumber in 1..$SpokeCount) {
128+
$heading += $spokeAngle
129+
[Numerics.Vector2]::new(
130+
$center.X + $inRadius * [math]::cos($heading * [Math]::PI / 180),
131+
$center.Y + $inRadius * [math]::sin($heading * [Math]::PI / 180)
132+
)
133+
}
134+
)
135+
136+
# Now that we have the points,
137+
foreach ($point in $webPoints) {
138+
# our turtle spider can
139+
$this = $this.Rotate(
140+
# rotate towards the point
141+
$this.Towards($point.X, $point.Y)
142+
).Forward(
143+
# and close the distance.
144+
$this.Distance($point.X, $point.Y)
145+
)
146+
}
147+
148+
# Reset our bearings and head up to the next ring.
149+
$this.Heading = $heading
150+
}
151+
152+
# Now that we've drawn our web, return ourself.
153+
return $this

0 commit comments

Comments
 (0)