0

我试图在简单的六边形晶格的有限区域中生成所有闭合曲线。这并不太重要,它只是一组彼此相距 1 的有限点。但是,我的代码会生成一段时间的闭合曲线,然后我的程序将停止工作并陷入无限循环?我曾尝试通过命令强制 java 进行垃圾收集,但相同的代码在不同的点停止。据我所知,它停在哪里是随机的。Sphere 是一个数组,用于存储所考虑区域中的所有点

private static Sphere Sphere1 = new Sphere();
private static double [][] vectors =  {{0, 0, 1},{0, 0, -1},{1, 0, 0},{-1,0,0},{.5, (Math.sqrt(3))/2, 0},{-.5, -(Math.sqrt(3))/2, 0},{.5, -(Math.sqrt(3))/2, 0},{-.5, (Math.sqrt(3))/2, 0}};
private static StringBuilder loopString = new StringBuilder("r");
private static String posPoints = "abcdefghijklmnopqrstuvwxyz1234567";
private static int garbage = 0;

private static boolean once = false;
private static PrintWriter output = null;

public static void main (String [] args){

    try {
        output = new PrintWriter(new FileOutputStream("closedLoops.txt"));
    } catch (FileNotFoundException e){
        System.out.println("error");
    }
    System.out.println("start");
    KnotPoint loopPoint = new KnotPoint(0,0,1);
    addVector(loopPoint, vectors);

}

public static void addVector(KnotPoint loopPoint, double [][] vectors){
    garbage ++;
    if (garbage == 200){
        System.gc();
        garbage = 0;
    }
    if (loopString.length() > 19
       && (loopString.charAt(loopString.length()-1) == 'z' ||
           loopString.charAt(loopString.length()-1) == '3' ||
           loopString.charAt(loopString.length()-1) == 'u' ||
           loopString.charAt(loopString.length()-1) == 'o' ||
           loopString.charAt(loopString.length()-1) == 'g' ||
           loopString.charAt(loopString.length()-1) == 'j' ||
           loopString.charAt(loopString.length()-1) == 'q')) 
    {
        System.out.println(loopString);
        output.println(loopString);
        once = true;

        return;
    }
    for (int i = 0; i < 8 ; i ++){
        if (validAdd(loopPoint, vectors[i], loopString, posPoints)){
            loopPoint.addNext(vectors[i]);

            addVector(loopPoint, vectors);
            //System.gc();
            loopString.deleteCharAt(loopString.length()-1);
            loopPoint.subtractLast(vectors[i]);
            if(loopString.toString().equals("r") ){
                output.println("you did it");
                output.close();
                System.out.println("you did it good job");
                System.exit(0);
            }
        }
    }

    return;
}

public static boolean validAdd(KnotPoint loopPoint, double [] vector, StringBuilder loopString, String posPoints){
    KnotPoint testAdd = new KnotPoint();
    testAdd.set(loopPoint);
    testAdd.addNext(vector);
    int pointIndex = 0;
    boolean pointCheck = false;
    char point = '.';
    for (int i = 0; i < 33; i ++){
        if( testAdd.equals(Sphere1.getKnotPoint(i))){
            pointIndex = i;
            pointCheck = true;
            point = posPoints.charAt(i);
        }
    }
    if (pointCheck && !loopString.toString().contains(posPoints.substring(pointIndex, pointIndex + 1))&& point != 'r'){// added not r check
        loopString.append(point);
        return true;
    } else {
        return false;
    }
}
4

0 回答 0