Results 1 to 4 of 4
  1. #1
    Hevonen is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Question Java Zelda Triangle Problem

    Problem to make a Zelda triangle by using Java.
    I have the code for a normal triangle in dpaste.com/83617

    Zelda triangle contains three such triangles.

    My approach to the problem:

    A. the row of 1st triangle
    1. The indent for the 1st triangle can be by a simple for loop
    2. Then I would add the triangleSmall method to code

    B. the row of 2nd triangle

    1. Add the triangleSmall method
    2. Add a new for loop which makes the inner, upside-down triangle
    3. Add the triangleSmall method

    Problem in joining the triangleSmall method:

    The parameters set in the main method do not work in the method triangleSmall. This problem is perhaps caused by the fact that the parameters set in the main method do not apply to the method triangleSmall. I have considered that the adding of method in Java works like the include command in php: this is not true in Java.

    How would you make a Zelda triangle by using Java? Should I make just one method which contains all code?

  2. #2
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    Is a 'Zelda triangle' like this:
    gilbyonline.com/wp-content/uploads/2006/11/siepinskiTriangle.JPG

    ?

    If so, I would personally draw this using one method which calls itself recursively and takes two parameters: a point (e.g. the bottom left) and the side length. When do you want the recursion to terminate, though? You could do it when the size of the triangle reaches some (small) value.

  3. #3
    Hevonen is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default Partly

    You have the hardest Zelda triangle :) I meant the one which has only 4 triangles. However, I will look forward to getting your triangle done once I have got the triangle which is easier ready.

    How would you make the "easier" Zelda triangle?

  4. #4
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by Hevonen View Post
    You have the hardest Zelda triangle :) I meant the one which has only 4 triangles. However, I will look forward to getting your triangle done once I have got the triangle which is easier ready.

    How would you make the "easier" Zelda triangle?
    Personally, if I knew it was only going to have 4 triangles (well 5 actually because there is one large triangle :)), then I would not use recursion. I would probably start at the bottom-middle of the triangle and draw 7 lines. You can do this using the Line2D.Double (or Line2D.Float) classes. Theres lots of examples out there if you google it.

    It may be better to use the Polygon class. You can create a Polygon for the large triangle, using trigonometry (the Math class) to work out the location of one of the points (I am assuming you know the location of one and the side length). You then use basic math to work out the mid-points of the lines and create a second Polygon for the smaller triangle.

    I tend to override paintComponent, like this:

    Java Code:
    protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.draw(largetriangle);
            g2.draw(smalltriangle);
    }

Similar Threads

  1. change the square to triangle java
    By anotsu in forum New To Java
    Replies: 3
    Last Post: 07-09-2009, 11:17 AM
  2. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 02:08 PM
  3. Is it a right triangle? (Code help)
    By TheApostle in forum New To Java
    Replies: 8
    Last Post: 10-07-2008, 08:39 PM
  4. Pascal Triangle help
    By Magic101 in forum New To Java
    Replies: 4
    Last Post: 05-01-2008, 07:51 PM
  5. Making triangle
    By banie in forum New To Java
    Replies: 4
    Last Post: 02-02-2008, 11:23 AM

Posting Permissions

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