Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的递归三角形_Java_Recursion - Fatal编程技术网

Java中的递归三角形

Java中的递归三角形,java,recursion,Java,Recursion,我是新来的,如果我做错了什么,请原谅。我已经对我的主题做了研究,但我似乎找不到如何正确地准备递归三角形 我试着接受一个我能做的参数,并创建一个我也能用Java做的三角形。 当参数=1时,应创建一个三角形。 在参数=2中,总共应创建四个三角形。 在这之后,我的程序就搞砸了,我很难弄清楚如何使三个三角形出现在三个三角形的中点。我以前看到的一些建议建议我使用中点,但我找不到边应该有多长 代码如下: public class Sierpinski{ public static void main

我是新来的,如果我做错了什么,请原谅。我已经对我的主题做了研究,但我似乎找不到如何正确地准备递归三角形

我试着接受一个我能做的参数,并创建一个我也能用Java做的三角形。 当参数=1时,应创建一个三角形。 在参数=2中,总共应创建四个三角形。 在这之后,我的程序就搞砸了,我很难弄清楚如何使三个三角形出现在三个三角形的中点。我以前看到的一些建议建议我使用中点,但我找不到边应该有多长

代码如下:

public class Sierpinski{
    public static void main(String args[]) {

                    String firstArgument = args[0];

        int b = Integer.parseInt( firstArgument );
        int c = b - 1;
        double rt = Math.sqrt(3);

        double lol = rt / 4;


        double[] x = {.25, .5, .75};
        double[] y = {lol, 0, lol};

        double x1 = .25;
        double x2 = .5;
        double x3 = .75;
        double y1 = lol;
        double y2 = 0;
        double y3 = lol;

        StdDraw.filledPolygon(x,y);


//Small Triangles

    while ( c > 0 ){

        //Small Triangle 1

        double xb1 = (x1) / 2 / c;
        double yb1 = (lol) / 2 / c;
        double xb2 = (x1) / c;
        double yb2 = (y2) / 2;
        double xb3 = (x1 + x2) / 2 / c;
        //double yb30 = (.44301270189) / 2 * 2  * i;
        double xb4 = 1 - ((x1) / 2 / c);
        double yb4 = ((lol) / 2 / c);
        double xb5 = 1 - ((x1) / c);
        double yb5 = (0) / 2;
        double xb6 = 1 - ((x1 + x2) / 2 / c);

        double xb7 = x2 + ((x1) / 2 / c);
        double yb7 = ((lol) / 2 / c);
        double xb8 = x2 + ((x1) / c);
        double yb8 = (0) / 2;
        double xb9 = x2 + ((x1 + x2) / 2 / c);

        double xb10 = .125 + ((x1) / 2 / c);
        double yb10 = (lol) / 2 / c;
        double xb11 = (x1) / c;
        double yb11 = (0) / 2;
        double xb12 = (x1 + x2) / 2 / c;


        double[] xb0 = {xb1, xb2, xb3, xb4, xb5, xb6, xb7, xb8, xb9};
        double[] yb0 = {yb1, yb2, yb1, yb4, yb5, yb4, yb7, yb8, yb7};

        StdDraw.filledPolygon(xb0,yb0);


//Small Triangle 2

        double xc1 = (.25 + .5) / 2;
        double yc1 = 1 - ( 0.44301270189 - .27409407929) * 2 ;
        double xc2 = (.5); // 2;
        double yc2 = (.44301270189); // 2;
        double xc3 = (.5 + .75) / 2;
        //double yb30 = (.44301270189); //2  * b;
        double xc4 = ((.25 + .5) / 2);
        double yc4 = .5 + (1 - ( 0.44301270189 - .27409407929) * 2) ;
        double xc5 = (.5); // 2;
        double yc5 = .5 + (.44301270189); // 2;
        double xc6 = (.5 + .75) / 2;

        double xc7 = x2 + ((x1) / 2 / c);
        double yc7 = ((lol) / 2 / c);
        double xc8 = x2 + ((x1) / c);
        double yc8 = (0) / 2;
        double xc9 = x2 + ((x1 + x2) / 2 / c);

        double xc10 = .125 + ((x1) / 2 / c);
        double yc10 = (lol) / 2 / c;
        double xc11 = (x1) / c;
        double yc11 = (0) / 2;
        double x12 = (x1 + x2) / 2 / c;


        double[] xc0 = {xc1, xc2, xc3};
        double[] yc0 = {yc1, yc2, yc1};

        StdDraw.filledPolygon(xc0,yc0);


        //Small Triangle 3

        double xd1 = (.5 + .75) / 2;
        double yd1 = (.44301270189) / 2;
        double xd2 = (.75); // 2;
        double yd2 = (0) / 2;
        double xd3 = (.25 + .5) / 2 + .5;
        double yd30 = (.44301270189) /2;



        double[] xd0 = {xd1, xd2, xd3};
        double[] yd0 = {yd1, yd2, yd1};

        StdDraw.filledPolygon(xd0,yd0);

        c--;


        }
    }

}

现在当它在3形成时,前四个完全重叠,第五个重叠(我想这是因为我试着使用精确的数字,因为我见过其他没有重叠问题的数字,但这是另一个似乎超出我头脑的问题。)

我在这里猜了很多,但假设您希望找到原始三角形每条边的中点,并使用中点和初始三角形顶点在原始三角形中刻下4个三角形,以递归方式执行此操作,直到任意深度

为此,您的代码将是这样的

public class TestIt {

    public static void inscribeTriangles(double xa, double ya, 
                                         double xb, double yb, 
                                         double xc, double yc, int depth) {
        if (depth <= 1) {
            // We've reached the bottom of the recursion, so draw!
            // Don't fill it or you won't see the inscribed triangles. 
            StdDraw.polygon(new double[]{xa, xb, xc}, new double[]{ya, yb, yc});
        } else {
            // Else recur to draw 4 inscribed triangles.
            double xab = (xa + xb) / 2, yab = (ya + yb) / 2;
            double xbc = (xb + xc) / 2, ybc = (yb + yc) / 2;
            double xca = (xc + xa) / 2, yca = (yc + ya) / 2;
            inscribeTriangles(xa, ya, xab, yab, xca, yca, depth - 1);
            inscribeTriangles(xb, yb, xbc, ybc, xab, yab, depth - 1);
            inscribeTriangles(xc, yc, xca, yca, xbc, ybc, depth - 1);
            // For Sierpinksi, don't subdivide 4th triangle:
            // inscribeTriangles(xab, yab, xbc, ybc, xca, yca, depth - 1);
            // And in this case you can also fill the polygon if you like.
        }
    }

    public static void main(String [] args) {
        inscribeTriangles(0, 0, 1, 0, 0.5, 1, 5);
    }
}
公共类测试{
公共静态空白刻字(双xa,双ya,
双xb,双yb,
双xc、双yc、整数深度){

如果(深度这里是完整的工作代码。享受:)


递归应该在哪里?我看到的唯一方法是
main()
。看起来远不是递归。我假设while循环是递归?没有递归,但它显然应该是递归。没有人能理解你在这里试图实现什么。很抱歉,无法理解它应该是一个Sierpinski三角形递归。我想设置第一组新的三角形坐标除以二就足够了,但显然不行。我已经把它放进去了,但实际上它并没有打印出更小的三角形,我需要做些什么吗?比如画一个圆,你需要圆。画,是对的还是还需要其他什么?好的。我没有意识到你的库假设了一个圆域为0到1,而不是像素。因此
int
s应该是
double
s。它现在是一个可运行的代码,在我的机器上运行良好。啊!你以前从未说过Sierpinski!要得到它,只需删除对
Inscribetriangs
的最后一个调用。我是通过注释完成的。
public class voicehovich{

      int generations;
      double borderSideSize;

     public static void main(String args[]) {

         voicehovich v =new voicehovich();

         String firstArg = args[0];
         v.generations = Integer.parseInt( firstArg );


         double startX=0.25;
         double startY=0.5;
         double startSide=0.5;

         //stop basing on side size
         v.borderSideSize = startSide/ (Math.pow(2,v.generations-1));

         //start creating triangles
         v.drawTriangle(startX,startY, startSide);


        }


           public void drawTriangle(double topLeftX, double topLeftY, double sideSize){
             double[] x = {topLeftX, topLeftX+sideSize/2, topLeftX+sideSize};

             double lowY = Math.sqrt(sideSize*sideSize-((sideSize/2)*(sideSize/2)));
             double[] y = {topLeftY, topLeftY-lowY, topLeftY};

             StdDraw.filledPolygon(x,y); 


             if (sideSize>borderSideSize){
             findCoordinatesOfThree(topLeftX,topLeftY,sideSize);
            }

        }

        public void findCoordinatesOfThree(double topLeftX, double topLeftY, double sideSize){
            //clculate coordinate of 3 triangles
            double tLeftX = topLeftX+sideSize/4-sideSize/2;
            double tLeftY = topLeftY-Math.sqrt(sideSize*sideSize-((sideSize/2)*(sideSize/2)))/2 ;

            double tRightX = topLeftX+sideSize*3/4;
            double tRightY = topLeftY-Math.sqrt(sideSize*sideSize-((sideSize/2)*(sideSize/2)))/2 ;

            double tTopX =  topLeftX+sideSize/4;
            double tTopY = topLeftY+Math.sqrt(sideSize/2*sideSize/2-((sideSize/4)*(sideSize/4)));


            drawTriangle( tLeftX, tLeftY, sideSize/2);
            drawTriangle( tRightX, tRightY, sideSize/2);
            drawTriangle( tTopX, tTopY, sideSize/2);

        }

    }