How to Draw a square within a square

This is a discussion on How to Draw a square within a square within the C# Programming forums, part of the General Programming Boards category; i know how to draw a square hollow or filled but i need to draw an alternating pattern ex iPatternHeight ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    4

    How to Draw a square within a square

    i know how to draw a square hollow or filled

    but i need to draw an alternating pattern

    ex
    iPatternHeight = 1
    X

    iPatternHeight = 3
    OOO
    OXO
    OOO

    iPatternHeight = 5
    XXXXX
    XOOOX
    XOXOX
    XOOOX
    XXXXX

    iPatternHeight = 7
    OOOOOOO
    OXXXXXO
    OXOOOXO
    OXOXOXO
    OXOOOXO
    OXXXXXO
    OOOOOOO

    anybody can help?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    I would say quit focusing exactly on the squareness of the shape. I will use the size 7 square, since I think it will clearly illustrate my thinking -- but it applies equally to the size 5 and size 3 ones. The 1 input is a special case, regardless.

    OOOOOOO

    OXXXXXO

    OXOOOXO

    OXOXOXO

    What do you notice about these lines? For each line, there is a specific number of X's and O's to draw. First it was all O's. Notice that as the X's are filled in, the line composed of the same characters on the inside is getting shorter by two. So one way to program it is to keep track of what row you are on, alternate characters to the start of the inner line, and then fill the inner line, and then alternate characters until you are done with the whole row. When you get down to the inner line length of 1, you know to alternate like OXOXOXO.

    The pattern below is the same as that pattern, done in reverse.

    OXOOOXO

    OXXXXXO

    OOOOOOO

    Once you can do it for this length, extend the concept for longer lines.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. draw an empty square of asterisks
    By rob90 in forum C Programming
    Replies: 3
    Last Post: 11-01-2009, 05:03 PM
  2. line-square and circle-square intersections
    By tomast in forum Game Programming
    Replies: 1
    Last Post: 11-16-2008, 07:12 AM
  3. Red Square? :(
    By Kai_Legends in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-14-2004, 08:13 PM
  4. for loops using square roots, square, Cube
    By lotf in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 03:29 AM
  5. Square
    By steerpike100 in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2002, 12:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21