Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions contents/IFS/IFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions contents/IFS/code/powershell/IFS.ps1
Original file line number Diff line number Diff line change
@@ -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"