diff --git a/contents/IFS/IFS.md b/contents/IFS/IFS.md index cd503ae46..f7d017e36 100644 --- a/contents/IFS/IFS.md +++ b/contents/IFS/IFS.md @@ -146,6 +146,8 @@ Here, instead of tracking children of children, we track a single individual tha [import:4-16, lang:"coconut"](code/coconut/IFS.coco) {% sample lang="java" %} [import:16-39, lang:"java"](code/java/IFS.java) +{% sample lang="ps1" %} +[import:2-19, lang:"powershell"](code/powershell/IFS.ps1) {% endmethod %} 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 [import, lang:"coconut"](code/coconut/IFS.coco) {%sample lang="java" %} [import, lang:"java"](code/java/IFS.java) +{% sample lang="ps1" %} +[import, lang:"powershell"](code/powershell/IFS.ps1) {% endmethod %} ### Bibliography diff --git a/contents/IFS/code/powershell/IFS.ps1 b/contents/IFS/code/powershell/IFS.ps1 new file mode 100644 index 000000000..086c80072 --- /dev/null +++ b/contents/IFS/code/powershell/IFS.ps1 @@ -0,0 +1,34 @@ +# This function simulates a "chaos game" +function Simulate-ChaosGame($n, $shapePoints) { + $outputPoints = New-Object System.Collections.ArrayList + + # Initialize the starting point + $point = @($(Get-Random -Minimum 0.0 -Maximum 1.0), $(Get-Random -Minimum 0.0 -Maximum 1.0)) + + for ($i = 0; $i -lt $n; $i++) { + $outputPoints.add($point) | Out-Null + $temp = $shapePoints[$(Get-Random -Maximum $shapePoints.Count)] + + $point = @( + 0.5 * ($point[0] + $temp[0]) + 0.5 * ($point[1] + $temp[1]) + ) + } + + return $outputPoints +} + + +# This will generate a Sierpinski triangle with a chaos game of n points for an +# initial triangle with three points on the vertices of an equilateral triangle: +# A = (0.0, 0.0) +# B = (0.5, sqrt(0.75)) +# C = (1.0, 0.0) +# It will output the file sierpinski.dat, which can be plotted after +$shapePoints = @( + @(0.0, 0.0), + @(0.5, [math]::sqrt(0.75)), + @(1.0, 0.0) +) + +Simulate-ChaosGame -n 10000 -shapePoints $shapePoints | % { "$($_[0])`t$($_[1])" } | Out-File -Path "sierpinski.dat"