Я пытаюсь создать «кривую дракона», которая использует левый и правый повороты для создания фигуры на панели рисования. Направления генерируются рекурсивно, где за определенное количество итераций или «уровней» направление добавляет предыдущую строку поворотов к одному повороту вправо плюс обратное дополнение предыдущей строки: предыдущая строка + «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 = '\0';
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...
Большое спасибо!