PowerShell/Docs/write-fractal.md

83 lines
2.3 KiB
Markdown
Raw Normal View History

2023-07-29 10:34:04 +02:00
*write-fractal.ps1*
================
2022-11-17 20:02:26 +01:00
write-fractal.ps1
2022-11-17 19:46:02 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2022-11-17 19:46:02 +01:00
```powershell
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Writes a fractal
.DESCRIPTION
This PowerShell script writes an animated Mandelbrot fractal.
.EXAMPLE
2023-09-01 17:53:03 +02:00
PS> ./write-fractal.ps1
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
function CalculateFractal([float]$left, [float]$top, [float]$xside, [float]$yside, [float]$zoom) {
[int]$maxx = $rui.MaxWindowSize.Width
[int]$maxy = $rui.MaxWindowSize.Height
[float]$xscale = $xside / $maxx
[float]$yscale = $yside / $maxy
for ([int]$y = 0; $y -lt $maxy; $y++) {
for ([int]$x = 0; $x -lt $maxx; $x++) {
[float]$cx = $x * $xscale + $left
[float]$cy = $y * $yscale + $top
[float]$zx = 0
[float]$zy = 0
for ([int]$count = 0; ($zx * $zx + $zy * $zy -lt 4) -and ($count -lt $MAXCOUNT); $count++) {
[float]$tempx = $zx * $zx - $zy * $zy + $cx
$zy = $zoom * $zx * $zy + $cy
$zx = $tempx
}
$global:buf[$y * $maxx + $x] = $([char](65 + $count))
}
}
}
$MAXCOUNT = 30
$ui = (Get-Host).ui
$rui = $ui.rawui
[float]$left = -1.75
[float]$top = -0.25
[float]$xside = 0.25
[float]$yside = 0.45
$buffer0 = ""
1..($rui.MaxWindowSize.Width * $rui.MaxWindowSize.Height) | ForEach-Object { $buffer0 += " " }
$global:buf = $buffer0.ToCharArray()
while ($true) {
for ([float]$zoom = 4.0; $zoom -gt 1.1; $zoom -= 0.02) {
CalculateFractal $left $top $xside $yside $zoom
[console]::SetCursorPosition(0,0)
[string]$Screen = New-Object system.string($global:buf, 0, $global:buf.Length)
Write-Host -foreground green $Screen -noNewline
}
for ([float]$zoom = 1.1; $zoom -lt 4.0; $zoom += 0.02) {
CalculateFractal $left $top $xside $yside $zoom
[console]::SetCursorPosition(0,0)
[string]$Screen = New-Object system.string($global:buf, 0, $global:buf.Length)
Write-Host -foreground green $Screen -noNewline
}
}
exit 0 # success
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2023-09-01 17:53:03 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of write-fractal.ps1 as of 09/01/2023 17:51:57)*