Search code examples
arrayspowershellimagemagick

How do I format ImageMagick argument for Powershell script (array of points for drawing intersecting lines connecting hexagonal vertices)?


I'm trying to set up a project to interest nephew in design/coding (while learning myself). This snippet appears to work, but it's suboptimal and I can't figure out how to set $draw as array and call thrice like -draw $draw[i] that works (or otherwise approach). I've tried iterations of single- and/or double-quotes. (Idk how to initialize $hex properly either.)

# Hexagon
$size = '482'
$half = $size/2
$hex = @(0,1,2,3,4,5)
for ($i = 0; $i -lt 6; $i++)
{
    $x = $half * [math]::cos(2 * [math]::pi * $i/6+[math]::pi) + $half
    $y = $half * [math]::sin(2 * [math]::pi * $i/6+[math]::pi) + $half
    $hex[$i] = "$x,$y"
}
$draw1 = 'line ' + $hex[0] + ' ' + $hex[3]
$draw2 = 'line ' + $hex[1] + ' ' + $hex[4]
$draw3 = 'line ' + $hex[2] + ' ' + $hex[5]
Write-Output $draw1
magick -size '482x482' xc:none -stroke 'black' -fill 'white' -draw "polygon $hex" `
-stroke 'black' -fill 'none' `
-draw $draw1 -draw $draw2 -draw $draw3 test_hex.png

Hexagon


Solution

  • # Hexagon
    $size = 482
    $half = $size/2
    for ($i = 0; $i -lt 6; $i++){
        $x = $half * [math]::cos(2 * [math]::pi * $i/6 + [math]::pi) + $half
        $y = $half * [math]::sin(2 * [math]::pi * $i/6 + [math]::pi) + $half
        $hex[$i] = "$x,$y"
    }
    $draw = @(
            '-draw', ('line', $hex[0], $hex[3] -join ' ')
            '-draw', ('line', $hex[1], $hex[4] -join ' ')
            '-draw', ('line', $hex[2], $hex[5] -join ' '))
    Write-Output $draw
    magick -size $size'x'$size xc:none -stroke black -fill white -draw "polygon $hex" `
    -fill none `
    $draw test_hex.png