# Java Zelda Triangle Problem

• 10-10-2008, 03:39 PM
Hevonen
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

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

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?
• 10-12-2008, 05:01 PM
Paul Richards
Is a 'Zelda triangle' like this:

?

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.
• 10-12-2008, 08:32 PM
Hevonen
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?
• 10-12-2008, 09:21 PM
Paul Richards
Quote:

Originally Posted by Hevonen
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:

Code:

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