Results 1 to 4 of 4
Thread: Java Zelda Triangle Problem
- 10-10-2008, 02:39 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
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?
- 10-12-2008, 04:01 PM #2
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
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.
- 10-12-2008, 07:32 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
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, 08:21 PM #4
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
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
-
change the square to triangle java
By anotsu in forum New To JavaReplies: 3Last Post: 07-09-2009, 11:17 AM -
Triangle
By jkswebsite in forum New To JavaReplies: 8Last Post: 01-10-2009, 02:08 PM -
Is it a right triangle? (Code help)
By TheApostle in forum New To JavaReplies: 8Last Post: 10-07-2008, 08:39 PM -
Pascal Triangle help
By Magic101 in forum New To JavaReplies: 4Last Post: 05-01-2008, 07:51 PM -
Making triangle
By banie in forum New To JavaReplies: 4Last Post: 02-02-2008, 11:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks