Help with christmas tree shape

Help with christmas tree shape

Page 1 of 1

2 Replies - 9458 Views - Last Post: 11 December 2009 - 02:49 AM Rate Topic: -----

#1 Upheaval   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 09

Help with christmas tree shape

Posted 10 December 2009 - 07:34 AM

I'm writing a method to create an xmas tree with stars.

Allright so far i came up with this :


static void DrawSquare(int x, int y)
		{
			for (int v = 0; v < x; v++)
			{
				for (int h = 1; h <= y; h++)
				{
					Console.Write("*");
				}			

				Console.WriteLine();
			}			
		}

static void DrawTree(int x)
		{
			for (int v = 0; v < x; v++)
			{
				for (int s = v + 1; s < x; s++)
				{
					Console.Write(" ");
				}

				for (int star = 2 * v; star >= 0; star--)
				{
					Console.Write("*");
				}
				Console.WriteLine();
			}
		}




Now the Square isn't in the middle of the tree.
I'm thinking about using a third parameter in the square method and using Console.SetCursorPosition().
I also came up with a possible formula :

Value of spaces from the 1st star of the triangle - (square width / 2)

But i'm not shure how to implement it in my code. Can anyone help me out here ?

Thx.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with christmas tree shape

#2 Renagado   User is offline

  • D.I.C Regular
  • member icon

Reputation: 118
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Help with christmas tree shape

Posted 10 December 2009 - 03:46 PM

This should do the trick, it's not hard, just think carefully about each step the iteration needs to do. However it would be neater(I think) to make DrawTree make the call to DrawSquare in order to draw the stem of the tree, so you only need to make one call yourself. But I'll leave it up to you to implement that :D

		static void DrawSquare(int x, int y, int offset)
		{
			for (int v = 0; v < x; v++)
			{
				for (int h = 1; h <= (y+offset); h++)
				{
					if (h <= offset)
					{
						Console.Write(" ");
					}
					else
					{
						Console.Write("*");
					}
				}

				Console.WriteLine();
			}
		}

Was This Post Helpful? 0
  • +
  • -

#3 Upheaval   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 09

Re: Help with christmas tree shape

Posted 11 December 2009 - 02:49 AM

Thx Renagado for your hint ! Got it working now !

I feel kinda stupid i didn't figured that one out xD
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1