algorithm - Trying to create an Htree using Recursion in Java, unable to get it to do more than one corner -
I am currently taking a class in Java and our professor has given us a "HTRI" which is the first In H is the H with the small H, and the lower the level the user specifies. So far, I have made it the first corner in the sequel, but I can not seem to know how to change it, so that it will complete the four corners. Here is my code:
Package assignment 3; Public class HTRI {public static zero main (string [] args) {int size = 512; // output and entry takes place in the console window Turtle.create (SIZE, SIZE); Int xCenter = SIZE / 2; Int yCenter = SIZE / 2; Int x = 4; Recursive (xCenter, yCenter, x); } Private static zero recursive (int xCenter, int yCenter, int x) {int x2 = x - 1; If (x2 & lt; 0) {return; } Int left = xCenter - xCenter / 2; Int correct = xCenter + xCenter / 2; Int top = yCenter - yCenter / 2; Int bottom = yCenter + yCenter / 2; Int middle = yCenter; Tortoise. Flight (right, middle); Turtle.go (left, middle); Tortoise. Flight (right, up); Turtle.go (right, bottom); Tortoise. Flight (left, above); Turtle.go (left, bottom); Recursive (left, top, x2); Println ("full"); }} And it looks like this:
Any suggestion would be appreciated.
Your code like
int left = xCenter - xCenter / 2 ; Int correct = xCenter + xCenter / 2; Is problematic Suppose second level H's X center should be X = 2 and x = 6. For Lefthand H you calculate the left = 1, right = 3, then it will be 2 units wide but for righthand h, you calculate left = 3, right = 9, then this is 6 units wide conclusions: to specify the width according to your recurring routine Another parameter is required, and you will use sources such as left = xcent-wide / 2, right = xcent + wide / 2.
Here's an easy way << Code> recursive (left, top, x2); The four different recursive calls used in place of the function ( drawH (xCenter, yCenter, wide, deep) / P> p = q = 1 I {1.}}: drawH (xcenter + p * size / xratio, ycenter + q * size / yratio, wide / 2, deep-1) Q = p * q; P = -p; Corrections and notes, May 2016: the last line of the pseudocode, q = p * q; p = -p; , pairs for four bars (1, 1), (-1, 1), (1, -1), (-1, -1) produces H of
Comments
Post a Comment