This script uses a mathematical algorithm to define the X and Y coordinates for each pixel in the image. The result is a complex pattern which is repeated like tiles on a floor for an interesting final image. These patterns are fractal like insomuch as they have repeating patterns but lack a fractal dimension and are static.
 
I will credit the author of the original algorithm when I figure out who it was. I got the outline of the process from a QuickBasic program I wrote long, long ago. I didn’t have the credit in the code but I think it was from some fractal books I have and hope to find again one day.
 
Here is an example Tiled Pattern image –
 
TiledPatternExample
 
And a closeup of the pattern –
TiledPatternExampleCloseUp
 
Here are a few other examples from this Tiled Patterns script-
 
TiledPatterns_20131120_192034
 
TiledPatterns_20131127_213942
 
TiledPatterns30131121_184757
 
The are five variables which are used to control the pattern, so in a WinForms Form I defined five TrackBar controls for Alpha, Beta1, Beta2, Gamma and Modulus. I also added the Labels for each TrackBar and set the initial variable values and gathered the script path.
 

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# Main Form
$mainForm = New-Object Windows.Forms.Form
$mainForm.BackColor = "White"
$mainForm.Font = “Comic Sans MS,8.25"
$mainForm.Text = "Tiled Pattern"
$mainForm.size = "500,350"

# Global Values
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$global:alphaValue = 2
$global:beta1Value = 4
$global:beta2Value = 4
$global:gammaValue = 134
$global:modulusValue = 2

# Alpha TrackBar
$alphaTrackBar = New-Object Windows.Forms.TrackBar
$alphaTrackBar.Location = "100,20"
$alphaTrackBar.Orientation = "Horizontal"
$alphaTrackBar.Width = 350
$alphaTrackBar.Height = 40
$alphaTrackBar.LargeChange = 5
$alphaTrackBar.SmallChange = 1
$alphaTrackBar.TickFrequency = 1
$alphaTrackBar.TickStyle = "TopLeft"
$alphaTrackBar.SetRange(1, 10)
$alphaTrackBar.Value = 4
$alphaTrackBarValue = 4
#Alpha TrackBar Event Handler
$alphaTrackBar.add_ValueChanged({
    $alphaTrackBarValue = $alphaTrackBar.Value
    $alphaLabel.Text = "Alpha ($alphaTrackBarValue)"
    $global:alphaValue = $alphaTrackBarValue
})
$mainForm.Controls.add($alphaTrackBar)

# Alpha Label
$alphaLabel = New-Object System.Windows.Forms.Label 
$alphaLabel.Location = "10,20"
$alphaLabel.Size = "120,23"
$alphaLabel.Text = "Alpha ($alphaTrackBarValue)"
$mainForm.Controls.Add($alphaLabel)

 
Next was added the two Buttons. One to call the DrawIt function to plot the pattern and one to close the Form.
 

# Draw Button
$DrawButton = New-Object System.Windows.Forms.Button 
$DrawButton.Location = "160,270"
$DrawButton.Size = "75,23"
$DrawButton.Text = "Draw"
$DrawButton.add_Click({DrawIt})
$mainForm.Controls.Add($DrawButton)

# Exit Button
$exitButton = New-Object System.Windows.Forms.Button
$exitButton.Location = "280,270"
$exitButton.Size = "75,23"
$exitButton.Text = "Exit"
$exitButton.add_Click({$mainForm.close()})
$mainForm.Controls.Add($exitButton)

 
In the main plotting function I first output a line on the host console to show processing has begun. Next I setup the output image file with a black background. I then get the values of the variable from the TrackBar selection (or initial values if not selected from TrackBar). The loop counters are initialized. The first processing steps percentage is set and a random color selected. During this function for each X position a Y is calculated. So in a X loop I first check for percent of processing done by looking for every one 100 pixels completed(this assumes the 1023 width so each 100 is close to 10% complete) and output to the host console. I then start a loop to plot the Y position. I make sure the pixel is positioned within the image and then apply the mathematical algorithm to get the Y position. Many values are generated but only the ones which have the modulus selected are used. Once Y is determined the pixel is written to the image using the previously selected random color. Lastly, after the image is fully created it is written to disk in the folder where the script resides. To keep the file names unique I concatenate the file name with the date and time. The newly written file is then displayed with the system default image viewer for that file type and a completion line is written to the host console.
 

# Draw the Pattern
Function DrawIt {
    Write-Host "Running . . ."

    # Output Bitmap Image
    $bitmap = New-Object System.Drawing.Bitmap(1024, 768)
    $bitmapGraphics = [System.Drawing.Graphics]::FromImage($bitmap) 
    $bitmapGraphics.Clear("black")

    $alpha = $global:alphaValue
    $beta1 = $global:beta1Value
    $beta2 = $global:beta2Value
    $gamma = $global:gammaValue
    $modulus  = $global:modulusValue

    $xPixel = 0; $yPixel = 0
    $percent = 10
    $totalPixels = 1023
    $red =   (Get-Random -minimum 0 -maximum 255)
    $green = (Get-Random -minimum 0 -maximum 255)
    $blue =  (Get-Random -minimum 0 -maximum 255)

    while ($xPixel -le $totalPixels) {
        $xPixel++ ; $yPixel = 1
        if (($xPixel % 100) -eq 0) {
            Write-Host "Processing" $percent"%"
            $percent = $percent + 10
        }
        while ($yPixel -le $totalPixels) { 
            $yPixel++
            if ($xPixel -ge 0) {
                if ($yPixel -ge 0) {
                    if ($xPixel -le 1023) {       
                        if ($yPixel -le 767) { 
                            $x=$beta1+($gamma*$xPixel)
                            $y=$beta2+($gamma*$yPixel)
                            $z=($alpha*([math]::sin($x))+[math]::sin($y))
                            $c=[math]::truncate($z)
                            if ($c % $modulus -eq 0) {
	                            $bitmap.SetPixel($xPixel, $yPixel, [System.Drawing.Color]::FromArgb($red, $green, $blue))
                            }
                        }
                    }
                }
            }
        }
    }

    $outFile = $scriptPath + "\"  + "TiledPatterns_" + (Get-Date -UFormat %Y%m%d_%H%M%S) + ".bmp"
    $bitmap.Save($outFile)
    Invoke-Item $outFile
    $bitmap.Dispose()
    $bitmapGraphics.Dispose()
    Write-Host "Complete"
}

 
TitledPatterns
 
Download – TiledPatterns.ps1

Advertisements