我正在尝试创建一个"龙曲线",它使用左右转动在绘图板上创建一个形状。方向是递归生成的,对于一定次数的迭代或"级别",方向将前一串匝数添加到一个右转加上前一串的反向补码:前一串字符串 + "R" + 前一串的反向补码。
根据我的结果,我的补码方法没有正确制作反向补码,我不确定为什么。这是我的代码。
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
intro();
int level = userlevel(scan);
int size = userSize(scan);
String fileName = BASE + level + TXT;
recursion(level, fileName);
drawDragon(fileName, size, level);
}
/**
* Prints the introductory message
**/
public static void intro()
{
System.out.println("This program will generate a fractal called the Dragon Curve");
System.out.println("first explored by John Heighway, Bruce Banks, and William Harter");
System.out.println("at Nasa in teh 1960's");
}
/**
* Inputs the user's desired level for the dragon curve
* @param the Scanner object for recieving user inputs
* @return the user's desired level
**/
public static int userlevel(Scanner scan)
{
String levelPrompt = "Enter the level of the fractcal you'd like to see (1-25): ";
int level = MyUtils.getNumber(scan, levelPrompt, MINLEVEL, MAXLEVEL);
return level;
}
/**
* Inputs the user's desired drawing panel width and height
* @param the Scanner object for recieving user inputs
* @return the user's desired drawing panel size
**/
public static int userSize(Scanner scan)
{
String panelPrompt = "Enter the size of your drawing panel, in pixels (100-2000): ";
int size = MyUtils.getNumber(scan, panelPrompt, MINSIZE, MAXSIZE);
return size;
}
/**
* Creates the reverse complement of the previous dragon curve string
* @param the previous string used the in the recursive loop
* @return the new reverse complement string
**/
public static String complement(String previousString)
{
StringBuilder newString = new StringBuilder();
char letter = ' ';
for (int i=previousString.length(); i<0; i--)
{
letter = previousString.charAt(i);
if (letter == 'L')
newString.append('R');
else if (letter == 'R')
newString.append('L');
}
return newString.toString();
}
/**
* Creates the string of directions for the dragon curve using recursion
* @param level - the user's desired level
* @param fileName - the file in which the dragon curve string is located
* @return the new set of directions for the curve after each loop
**/
public static String recursion(int level, String fileName)
{
PrintStream output = MyUtils.createOutputFile(fileName);
String newDirection = "";
String directions = "";
if (level==1)
{
directions = "R";
output.print(directions);
}
else
{
String previousString = recursion(level-1, fileName);
String nextComplement = complement(previousString);
newDirection = previousString + "R" + nextComplement;
output.print(newDirection);
System.out.println(newDirection);
//output.flush();
//output.close();
}
return newDirection;
}
/**
* Draws the dragon curve on the drawing panel
* @param fileName - the file where the dragon curve directions are located
* @param size - the width and height of the drawing panel
* @param level - the user's desired level
**/
public static void drawDragon(String fileName, int size, int level)
{
Scanner fileScan = MyUtils.getFileScanner(fileName);
System.out.println("Path generated, writing to file " + fileName + "...");
String direction = fileScan.nextLine();
System.out.println("Drawing Curve...");
DragonDraw dd = new DragonDraw(size);
dd.drawCurve(fileName, level);
}
}
这是错误:我只得到重复的"R"。
Enter the level of the fractcal you'd like to see (1-25): 20
Enter the size of your drawing panel, in pixels (100-2000): 500
R
RR
RRR
RRRR
RRRRR
RRRRRR
RRRRRRR
RRRRRRRR
RRRRRRRRR
RRRRRRRRRR
RRRRRRRRRRR
RRRRRRRRRRRR
RRRRRRRRRRRRR
RRRRRRRRRRRRRR
RRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRR
Path generated, writing to file dragon20.txt...
Drawing Curve...
非常感谢!
看看complement
方法中的for
循环。它在变量i
小于零时运行,这应该在此变量大于零时运行。因此,将此for (int i=previousString.length(); i<0; i--)
更改为for (int i=previousString.length() - 1; i >= 0; i--)
,它将正常工作。