2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
17
Task/Sierpinski-carpet/PowerShell/sierpinski-carpet-1.psh
Normal file
17
Task/Sierpinski-carpet/PowerShell/sierpinski-carpet-1.psh
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
function Draw-SierpinskiCarpet ( [int]$N )
|
||||
{
|
||||
$Carpet = @( '#' ) * [math]::Pow( 3, $N )
|
||||
ForEach ( $i in 1..$N )
|
||||
{
|
||||
$S = [math]::Pow( 3, $i - 1 )
|
||||
ForEach ( $Row in 0..($S-1) )
|
||||
{
|
||||
$Carpet[$Row+$S+$S] = $Carpet[$Row] * 3
|
||||
$Carpet[$Row+$S] = $Carpet[$Row] + ( " " * $Carpet[$Row].Length ) + $Carpet[$Row]
|
||||
$Carpet[$Row] = $Carpet[$Row] * 3
|
||||
}
|
||||
}
|
||||
$Carpet
|
||||
}
|
||||
|
||||
Draw-SierpinskiCarpet 3
|
||||
54
Task/Sierpinski-carpet/PowerShell/sierpinski-carpet-2.psh
Normal file
54
Task/Sierpinski-carpet/PowerShell/sierpinski-carpet-2.psh
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
Function Draw-SierpinskiCarpet ( [int]$N )
|
||||
{
|
||||
# Define form
|
||||
$Form = [System.Windows.Forms.Form]@{ Size = '300, 300' }
|
||||
$Form.Controls.Add(( $PictureBox = [System.Windows.Forms.PictureBox]@{ Size = $Form.ClientSize; Anchor = 'Top, Bottom, Left, Right' } ))
|
||||
|
||||
# Main code to draw Sierpinski carpet
|
||||
$Draw = {
|
||||
|
||||
# Create graphics objects to use
|
||||
$PictureBox.Image = ( $Canvas = New-Object System.Drawing.Bitmap ( $PictureBox.Size.Width, $PictureBox.Size.Height ) )
|
||||
$Graphics = [System.Drawing.Graphics]::FromImage( $Canvas )
|
||||
|
||||
# Draw single pixel
|
||||
$Graphics.FillRectangle( [System.Drawing.Brushes]::Black, 0, 0, 1, 1 )
|
||||
|
||||
# If N was not specified, use an N that will fill the form
|
||||
If ( -not $N ) { $N = [math]::Ceiling( [math]::Log( [math]::Max( $PictureBox.Size.Height, $PictureBox.Size.Width ) ) / [math]::Log( 3 ) ) }
|
||||
|
||||
# Define the shape of the fractal
|
||||
$P = @( @( 0, 0 ), @( 0, 1 ), @( 0, 2 ) )
|
||||
$P += @( @( 1, 0 ), @( 1, 2 ) )
|
||||
$P += @( @( 2, 0 ), @( 2, 1 ), @( 2, 2 ) )
|
||||
|
||||
# For each iteration
|
||||
ForEach ( $i in 0..$N )
|
||||
{
|
||||
# Copy the result of the previous iteration
|
||||
$Copy = New-Object System.Drawing.TextureBrush ( $Canvas )
|
||||
|
||||
# Calulate the size of the copy
|
||||
$S = [math]::Pow( 3, $i )
|
||||
|
||||
# For each position in the next layer of the fractal
|
||||
ForEach ( $i in 1..7 )
|
||||
{
|
||||
# Adjust the copy for the new location
|
||||
$Copy.TranslateTransform( - $P[$i-1][0] * $S + $P[$i][0] * $S, - $P[$i-1][1] * $S + $P[$i][1] * $S )
|
||||
|
||||
# Paste the copy of the previous iteration into the new location
|
||||
$Graphics.FillRectangle( $Copy, $P[$i][0] * $S, $P[$i][1] * $S, $S, $S )
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Add the main drawing code to the appropriate events to be drawn when the form is first shown and redrawn when the form size is changed
|
||||
$Form.Add_Shown( $Draw )
|
||||
$Form.Add_Resize( $Draw )
|
||||
|
||||
# Launch the form
|
||||
$Null = $Form.ShowDialog()
|
||||
}
|
||||
|
||||
Draw-SierpinskiCarpet 4
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
function inCarpet($x, $y) {
|
||||
while ($x -ne 0 -and $y -ne 0) {
|
||||
if ($x % 3 -eq 1 -and $y % 3 -eq 1) {
|
||||
return " "
|
||||
}
|
||||
$x = [Math]::Truncate($x / 3)
|
||||
$y = [Math]::Truncate($y / 3)
|
||||
}
|
||||
return "#"
|
||||
}
|
||||
|
||||
function carpet($n) {
|
||||
for ($y = 0; $y -lt [Math]::Pow(3, $n); $y++) {
|
||||
for ($x = 0; $x -lt [Math]::Pow(3, $n); $x++) {
|
||||
Write-Host -NoNewline (inCarpet $x $y)
|
||||
}
|
||||
Write-Host
|
||||
}
|
||||
}
|
||||
#Display the carpet for N=3...
|
||||
carpet 3
|
||||
Loading…
Add table
Add a link
Reference in a new issue