Skip to content

Commit fca9911

Browse files
PaddyKeleios
andauthored
Added Iterated function system in PowerShell (#895)
* implemented IFS in PowerShell * fixed typo Co-authored-by: James Schloss <jrs.schloss@gmail.com>
1 parent 3bbc549 commit fca9911

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

contents/IFS/IFS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ Here, instead of tracking children of children, we track a single individual tha
146146
[import:4-16, lang:"coconut"](code/coconut/IFS.coco)
147147
{% sample lang="java" %}
148148
[import:16-39, lang:"java"](code/java/IFS.java)
149+
{% sample lang="ps1" %}
150+
[import:2-19, lang:"powershell"](code/powershell/IFS.ps1)
149151
{% endmethod %}
150152

151153
If we set the initial point to the on the equilateral triangle we saw before, we can see the Sierpinski triangle again after a few thousand iterations, as shown below:
@@ -232,6 +234,8 @@ In addition, we have written the chaos game code to take in a set of points so t
232234
[import, lang:"coconut"](code/coconut/IFS.coco)
233235
{%sample lang="java" %}
234236
[import, lang:"java"](code/java/IFS.java)
237+
{% sample lang="ps1" %}
238+
[import, lang:"powershell"](code/powershell/IFS.ps1)
235239
{% endmethod %}
236240

237241
### Bibliography

contents/IFS/code/powershell/IFS.ps1

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This function simulates a "chaos game"
2+
function Simulate-ChaosGame($n, $shapePoints) {
3+
$outputPoints = New-Object System.Collections.ArrayList
4+
5+
# Initialize the starting point
6+
$point = @($(Get-Random -Minimum 0.0 -Maximum 1.0), $(Get-Random -Minimum 0.0 -Maximum 1.0))
7+
8+
for ($i = 0; $i -lt $n; $i++) {
9+
$outputPoints.add($point) | Out-Null
10+
$temp = $shapePoints[$(Get-Random -Maximum $shapePoints.Count)]
11+
12+
$point = @(
13+
0.5 * ($point[0] + $temp[0])
14+
0.5 * ($point[1] + $temp[1])
15+
)
16+
}
17+
18+
return $outputPoints
19+
}
20+
21+
22+
# This will generate a Sierpinski triangle with a chaos game of n points for an
23+
# initial triangle with three points on the vertices of an equilateral triangle:
24+
# A = (0.0, 0.0)
25+
# B = (0.5, sqrt(0.75))
26+
# C = (1.0, 0.0)
27+
# It will output the file sierpinski.dat, which can be plotted after
28+
$shapePoints = @(
29+
@(0.0, 0.0),
30+
@(0.5, [math]::sqrt(0.75)),
31+
@(1.0, 0.0)
32+
)
33+
34+
Simulate-ChaosGame -n 10000 -shapePoints $shapePoints | % { "$($_[0])`t$($_[1])" } | Out-File -Path "sierpinski.dat"

0 commit comments

Comments
 (0)