Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default Drawing out triangles

    Hello. For an assignment I have to ask for input of a character and a number. And then the program will draw out a triangle based on the character and the number as the number of rows. So if the input was "@" and "5" it would draw out this:
    |||||@||||
    |||@||@||
    ||@||||@||
    |@|||||||@|
    @|@|@|@|@

    The "|" is just there so that the triangle actually shows up. First row should have 4 spaces on either side of the @. Last row should have five @ with one space separating them.
    I can't think of a code efficient way of doing this. If anyone could help with this, it would be greatly appreciated.
    Last edited by leiferouis; 01-12-2009 at 02:32 AM.

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    there are 3 conditions for printing @
    first 2 are suitable for all lines
    last one used to handle base line of Triangle

    you can see, symmetry line in the center of Triangle
    so the first 2 conditions are "center position -/+ offset" while offset change by line number

    last conditions... do you know how to find a number is odd or even?

  3. #3
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    No I don't know how to find whether a number is odd or even, would you do a multiplication test? Also, I don't understand what you mean by center position -/+ offset.

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    for example input number is "5"
    expected result in line 1
    @ will appear in the center, so is center(which is 5) + or - offset (which is 0 in line 1)
    in line 2, @ will appears at center (5) + or - offset (1)
    @s appear => 4 and 6 pos

    test odd/even, suggested method is num % 2 = 0 or 1

  5. #5
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    How would I do that in a loop? Is there a built in offset function?

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    2 for(or while) loop needed, i think you know that,
    for
    Java Code:
    for (i=0; i < triangleWidth; i++) // row
    {
    	for (j=0; j < xxxx ; j++) // column
    	{
    		// process
    	}
    }
    1) consider what xxxx should be,

    2) by the previous post
    first line, offset is +/-0
    second line, offset is +/- 1
    third line, offset is +/- 2 and so on,
    can you see any relation between the offset and value of i, j

    you can create a method intake value of i,j or simply use nested if statement to check

    3) do you know how to find out center position of Triangle by user input?
    Last edited by mtyoung; 01-14-2009 at 04:07 AM.

  7. #7
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    1) I think that xxxx would have to be the inputed number times 2 because that equals the number of columns every triangle should have.
    2)Wouldn't the offset have to equal the current i value?
    3)center position = user input - ((user input-1)/2) for odd numbers, don't know what to do for even numbers.

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    It's not difficult, just really boring.

    WARNING!! The following is a spoiler.

    Java Code:
    public class DrawTri{
    	public static void main(String[] args){
    		
    		// add features to allow user input
    		int n = 2;		// user input
    		char c = '@';	// user input
    		
    		final int spacer = n * 2;	
    		
    		
    		// print first section
    		for(int line=0; line<(n-1); line++){
    			char[] charArray = new char[spacer];
    			int mid = charArray.length / 2;
    			
    			charArray[mid - line] = c;
    			charArray[mid + line] = c;
    			
    			System.out.println(charArray);
    		}
    		
    		// print last line
    		while(n != 0){
    			System.out.print(" " + c);
    			n--;
    		}
    		System.out.println();
    	}
    }

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    wow I got some really nice graphic when I changed n=500. Its like typing "yes" on gnu/linux, except a more interesting response.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default very interesting...

    Neat solution, but I would suggest not throwing out complete solutions before the OP presents his solution. It really doesn't help giving a complete solution... too much temptation to cut & paste & forget... no learning in the process.
    I liked your use of arrays... hadn't thought of that.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Sorry, I thought this was a help-solutions forum.

  12. #12
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    me, c++ type thinking...
    use of arrays... hadn't thought of that
    not thought can cout whole array...

  13. #13
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    umm... you forgot to set all the array as ' ' before the loop. But other than that, whenever I run the program, it sets the center array space as the char as well.
    Also what does final int mean?

  14. #14
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    final means constant

    leiferouis, can you check the output of first post is correct or not
    some lines have 9 characters and some have 10 characters?
    Last edited by mtyoung; 01-15-2009 at 06:11 AM.

  15. #15
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    I didn't really want to dwell too deep into that, because its your program. haha. But I think the jvm auto sets the array. I forgot, i'll have to look that up again. final is like const in c/c++.

  16. #16
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default It is a help forum...

    AB: It is a help forum... help people with their problems with Java programs. Giving complete solutions doesn't help them because there is no learning (they can take it and run :-). Now, what is helpful is pseudo code, hints, code snippets, explainations, etc. That kind of help is very useful to the OP.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  17. #17
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    For the code that angryboy offered, if you put charArray[4] and charArray[6] instead of charArray[mid-line] and charArray[mid+line] it works perfectly. However, its obvious that numbers cannot be used in the loop so I tried putting mid-line and mid+line in two different variables, however that did not work either. It always has the center space changed into the character as well.

    mtyoung:All lines have 10 spaces.
    Another few things i wanted to ask: 1. Why did you use charArray.length to set the value of mid instead of just spacer/2? 2.Why is the mid integer created within the loop?
    Last edited by leiferouis; 01-15-2009 at 03:18 PM.

  18. #18
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    There's many ways to code, just as there are many colors to paint with. I used charArray.length cause I wanted it to depend on charArray, so if something breaks in the chain, it'll still work. But I realize that adds an extra char at the console because of the zero-based array.

  19. #19
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Well basically, the whole thing is ascii art. I use the charArray as a canvas filled by spaces. Then set the correct place in the array with '@'. And have that put inside a loop to shift its position.

  20. #20
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    I got it working. I can't believe how I never noticed this before. I didn't reset the array in the loop therefore the characters that were changed before were still changed. Thanks for the help angryboy.

Page 1 of 2 12 LastLast

Similar Threads

  1. Drawing a map
    By Karp in forum AWT / Swing
    Replies: 4
    Last Post: 11-07-2008, 12:26 PM
  2. Triangles
    By CodeDog in forum New To Java
    Replies: 9
    Last Post: 10-14-2008, 09:18 PM
  3. Demonstration of drawing an Arc in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:24 PM
  4. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 02:23 AM
  5. asterisks triangles
    By Dan121 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •