locked
How to print star in diamond shape with power shell like this RRS feed

  • Question

  • Hi friends,

    I have just started to learning power shell script, Please help me out on this

    How to print star in diamond shape with power shell like this.

            *

       * * * **

             *

    Tuesday, December 19, 2017 4:11 PM

All replies

  • We do not write scripts, we help with issues you are having on your script. What do you have so far?

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Tuesday, December 19, 2017 4:37 PM
  • Could use something similar to the below if your just using write-host or something similar.

    Write-Host @"

              *

       * * * **

             *

    "@

    PowerShell is not a very graphical language as are most coding languages. What is this going to be used for?

    Tuesday, December 19, 2017 4:41 PM
  • Thanks for your suggestion, actually i am doing like this

    for( $i = 1; $i -lt 10; $i++)
    {
    for( $j = 0; $j -lt 9-$i/2; $j++)
    {
      write-host -NoNewline ""
      }
      for( $j = 0; $j -lt $i; $j++)
      {
     write-host -NoNewline "*"
     }
     for( $i = 7; $i -gt 0; $i--)
     {
     for( $j = 0; $j -lt 9-$i/2; $j++)
     {
     write-host -NoNewline ""
     }
     for( $j = 0; $j -lt $i; $j++)
     {
     write-host -NoNewline "*"
     }
     }
     }

    If possible can you figure it out?

    Wednesday, December 20, 2017 8:07 AM
  • I am doing like this,

    can you figure it out?

    for( $i = 1; $i -lt 10; $i++)
    {
    for( $j = 0; $j -lt 9-$i/2; $j++)
    {
      write-host -NoNewline ""
      }
      for( $j = 0; $j -lt $i; $j++)
      {
     write-host -NoNewline "*"
     }
     for( $i = 7; $i -gt 0; $i--)
     {
     for( $j = 0; $j -lt 9-$i/2; $j++)
     {
     write-host -NoNewline ""
     }
     for( $j = 0; $j -lt $i; $j++)
     {
     write-host -NoNewline "*"
     }
     }
     }

    Wednesday, December 20, 2017 8:08 AM
  • What is it you are calling a star in a diamond shape?  A star is not a diamond shape.

    There is no simple mathematical formula for generating a star.

    You can superimpose two triangles to make a start and triangles can be generated from a formula.

    What you hat posted is just some lines with apteryx in them.

    You can also use the screen buffer to assign values to individual cells.


    \_(ツ)_/


    • Edited by jrv Wednesday, December 20, 2017 8:26 AM
    Wednesday, December 20, 2017 8:25 AM
  • You could do this:

    # http://www.techotopia.com/index.php/Drawing_Graphics_using_PowerShell_1.0_and_GDI%2B[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
    [reflection.assembly]::LoadWithPartialName("System.Drawing")
    
    # Create pen and brush objects 
    $myBrush = new-object Drawing.SolidBrush green
    $mypen = new-object Drawing.Pen black
    
    # Create a Rectangle object for use when drawing rectangle
    $rect = new-object Drawing.Rectangle 10, 10, 180, 180
    
    # Create a Form
    $form = New-Object Windows.Forms.Form
    # Get the form's graphics object
    $formGraphics = $form.createGraphics()
    
    # Define the paint handler
    $form.add_paint({
            
            $formGraphics.FillEllipse($myBrush, $rect) # draw an ellipse using rectangle object
            
            $mypen.color = "red" # Set the pen color
            $mypen.width = 5 # ste the pen line width
            
            $formGraphics.DrawLine($mypen, 10, 10, 190, 190) # draw a line
            
            $formGraphics.DrawLine($mypen, 190, 10, 10, 190) # draw a line
            
    })
    
    $form.ShowDialog() # display the dialog



    \_(ツ)_/


    • Edited by jrv Wednesday, December 20, 2017 8:30 AM
    Wednesday, December 20, 2017 8:28 AM
  • Could you please superimpose of two triangle to make it diamond shape.

    I am sending you the pyramid power shell script

    for( $i = 1; $i -le 5; $i++)
    {
    for( $j = 5; $j -ge $i; $j--)
      {
      write-host -NoNewline " "
     } 
     for($k = 1; $k -le $i; $k++)
     {
     write-host -NoNewline "* "
     }
     write-host
     }

    Please superimpose it if you can.

     
    Wednesday, December 20, 2017 12:00 PM
  • Hi,

    Do you mean this shape?
    Write-Host @"
        *
       * *
    * * * * *
     * * * *
    * * * * *
       * *
        *
    "@

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, December 21, 2017 12:18 PM
  • Is this what you are trying to do?  ( The following demonstrates how to address the screen)

    function MakeIt{
        # Thank you Sean 
        # https://ye110wbeard.wordpress.com/2009/12/18/powershell-%E2%80%93-oh-christmas-tree-oh-christmas-tree-%E2%80%93-one-more-change/
        clear-host
        #Move it all down the line 
        write-host
        # Assign two special characters into an array for potential "LIGHTS" 
        $starchar = [char][byte]15, "*"
        # Number of rows Deep for the Tree- from 2 to whatever fits on the screen, after 30 it gets funky 
        $Rows = 30
        # These variables are for the Bouncing Marquee at the bottom
        # Column, number of Columns to move (relative to size of tree)
        # and Direction it will move 
        $BottomRow = $Rows + 4
        $BottomColumn = 0
        $BottomMaxCol = ($Rows)
        $Direction = 1
    
        # Standard console Colours
        # Just for fun I added in all the possible Console Foreground Colors
        # Delete whichever ones you don't like 
        $colors = "DarkRed", "DarkBlue", "DarkCyan", "DarkMagenta", "DarkYellow", "Gray", "DarkGray", "Blue", "Green", "Cyan", "Red", "Magenta", "Yellow", "White"
        # Get where the Cursor was 
        $oldpos = $host.ui.RawUI.CursorPosition
        # BsponPosh's ORIGINAL Tree building Algorithm 🙂
        # None of this would be possible if it weren't for him 
        Foreach ($r in ($rows .. 1)) {
            write-host $(" " * $r) -NoNewline
            1 .. ((($rows - $r) * 2) + 1) | ForEach-Object{
                write-Host "*" -ForegroundColor Darkgreen -nonewline
            }
            write-host ""
        }
    
        # trunk 
        # A slight change, an extra row on the stump of the tree
        # and Red (Trying to make it look like a brown trunk 
    
        write-host $("{0}***" -f (' ' * ($Rows - 1))) -ForegroundColor DarkRed
        write-host $("{0}***" -f (' ' * ($Rows - 1))) -ForegroundColor DarkRed
        write-host $("{0}***" -f (' ' * ($Rows - 1))) -ForegroundColor DarkRed
    
        $host.ui.RawUI.CursorPosition = $oldpos
    
        # New Addins by Sean "The Energized Tech" Kearney 
        # Compute the possible number of stars in tree (Number of Rows Squared) 
        $numberstars = [math]::pow($Rows, 2)
    
        # Number of lights to give to tree.  %25 percent of the number of green stars.  You pick 
        $numberlights = $numberstars * .35
        # Initialize an array to remember all the "Star Locations" 
        for ($i = 0; $i -lt $numberlights; $i++) {
            $Starlocation += @($host.ui.Rawui.CursorPosition)
        }
    
        # Probably redundant, but just in case, remember where the  heck I am! 
        $oldpos = $host.ui.RawUI.CursorPosition
    
        # New change.  Create an Array of positions to place lights on and off 
        foreach ($light in ($numberlights .. 1)) {
            # Pick a Random Row 
            
            $row = (get-random -Minimum 1 -Maximum (($Rows) + 1))
            
            # Pick a Random Column – Note The Column Position is
            # Relative to the Row vs Number of Rows 
            
            $column = ($Rows - $row) + (get-random -Minimum 1 -Maximum ($row * 2))
            
            #Grab the current position and store that away in a $Temp Variable
            $temppos = $host.ui.rawui.CursorPosition
            
            # Now Build new location of X,Y into $HOST
            $temppos.x = $column
            $temppos.y = $row
            
            # Store this away for later
            $Starlocation[(($light) - 1)] = $temppos
            
            # Now update that "STAR" with a Colour
        }
        # Repeat this OVER and OVER and OVER and OVER 
    
        while ($true) {
            
            # Now we just pull all those stars up and blank em back
            # on or off randomly 7 at a time 
            
            for ($light = 1; $light -lt 7; $light++) {
                # Set cursor to random location within Predefined "Star Location Array" 
                
                $host.ui.RawUI.CursorPosition = ($Starlocation | get-random)
                # Pick a random number between 1 and 1000
                # if 500 or higher, turn it to a light
                # Else turn it off 
                
                $flip = get-random -Minimum 1 -Maximum 1000
                if ($flip -gt 500) {
                    
                    # Write a Random "star character" on the screen
                    # in a Random Foreground Color from defined sets 
                    
                    write-Host ($starchar | get-random) -ForegroundColor ($colors | get-random) -nonewline
                } else {
                    write-host "*" -Foregroundcolor DarkGreen -nonewline
                }
            }
            
            # Remember where we are 
            
            $temppos = $oldpos
            
            # Set a position for the row and Column 
            
            $oldpos.X = $BottomColumn
            $oldpos.Y = $BottomRow
            
            # update the console 
            
            $host.ui.Rawui.CursorPosition = $oldpos
            
            # Bump up the column position based upon direction 
            
            $BottomColumn = $BottomColumn + $Direction
            
            # Ok this was a BAD way to do it but it works for 
            # Christmas.   If we hit the right side change 
            # Direction to backwards.  If we hit the left side
            # Change direction forwards 
            
            If ($BottomColumn -gt $Rows) { $Direction = -1 }
            
            If ($BottomColumn -lt 1) { $Direction = 1 }
            
            # Print greeting.  Space must be before and after to avoid
            # Trails.  Output in Random Colour 
            
            write-host " Happy Holidays Powershell " -ForegroundColor ($colors | get-random)
            
            # End of the loop, keep doin' in and go "loopy!" 
            
        }
    
    }
    MakeIt
    


    \_(ツ)_/


    • Edited by jrv Thursday, December 21, 2017 12:28 PM
    Thursday, December 21, 2017 12:26 PM