2014-02-17 92 views
1

我有一個家庭作業任務,我已準備好接受任務,我不得不使用遞歸繪製10級深度的嵌套圓圈,在幾個小時後,我的頭撞這對我終於完成它。我唯一的問題是,我正在繪製10層深的圖像還是實際上11?遞歸級別和循環計數

這個問題來自於這樣一個事實,即我已經具體說明了遞歸在經歷了10個級別後就結束了,但我確實告訴了繪製原始圓圈然後它自己調用它的方法。這讓我覺得它已經達到了第一級,然後下降了10級,總共達到了11級。它創建的圖像到目前爲止,我無法計數的圓圈:/

任何澄清將不勝感激,謝謝!

// import statements 
    import java.awt.*; 
    import javax.swing.*; 

    public class RecursiveCircles 
    { 

public static void main(String[] args) 
{ 
    // create a new canvas 
    RCanvas myCanvas = new RCanvas(); 

    // create JFrame 
    JFrame myJframe = new JFrame(); 
    myJframe.setTitle("Recursive Circles"); 

    // set JFrame size, location and close operation 
    myJframe.setSize(1500, 500); 
    myJframe.setLocation(100, 100); 
    myJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    // and canvas to JFrame and make it visble 
    myJframe.getContentPane().add(myCanvas); 
    myJframe.setVisible(true); 

    } // end main   
    } // end class RecursiveCircles 

    /* 
    * this class will draw the main circle of the image and will have the recursive 
    * method that draws the two outer circles down 10 levels 
    */ 

    class RCanvas extends Canvas 
    { 
    // defualt constructor 
    public RCanvas() 
    {} 

    public void paint (Graphics graphics) 
    { 
     // declare variables 
     String title = "Recursive Circles"; 

     int n = 300; // diamerter of the circle 
     int xOrigin = 600; // x location of start of circle (makes circle around the origin I wanted 
     int yOrigin = 100; // y location of start of circle (makes circle around the origin i wanted 
     int radius = n/2; // radius of circle (half the diamerter 

     int level = 10; 

     // make canvas background color black 
     graphics.setColor(Color.black); // make the background color black 
     graphics.fillRect(0, 0, 1500, 500); // rectangle that fills the JFrame 

     // put title on canvas 
     graphics.setColor(Color.BLUE); 
     graphics.drawString(title, 725, 50); 

     // draw main circle 
     graphics.setColor(Color.WHITE); 
     graphics.drawOval(xOrigin, yOrigin, n, n); 

     drawRCircles(graphics,xOrigin,yOrigin,radius,level); // call recrusrive method 
     System.out.println(level); 

    } // end paint 

    /* 
    * This is the recursive method that will draw the two circles on the outer sides of the 
    * main circle. it will then call itself and repate the process till it is 10 levels deep 
    */ 
    public void drawRCircles(Graphics graphics,int xOrigin,int yOrigin, int radius, int level) 
    { 

    int newRadius = (radius/2); // radius of smaller circle 
    int newXOrigin = xOrigin - (newRadius); // xOrigin of circle on left of the main circle 
    int newYOrigin = yOrigin + (newRadius); // yOrigin of circle on the right of the main circle 
    int newXOrigin2 = xOrigin + (newRadius*3); // xOrigin of circle on the right of the main circle 
    int newYOrigin2 = yOrigin + (newRadius); // yOrigin of circle on the right of the main circle 
     if (level > 0) // counts down from 10 to make the recursive image 10 levels deep 
     { 
      graphics.drawOval(newXOrigin, newYOrigin, newRadius*2, newRadius*2); // draw recursive circle on the left of main circle 
      graphics.drawOval(newXOrigin2, newYOrigin2, newRadius*2, newRadius*2); // draw recursive circle on the right of main circle 
      drawRCircles(graphics, newXOrigin, newYOrigin , newRadius, (level-1)); // make recursion of left circle 
      drawRCircles(graphics, newXOrigin2, newYOrigin2,newRadius,(level-1)); // make recursion of right circle 

     }// end if 
    } // end drawRCircles 
    }// end class 

回答

0

您的代碼繪製11圈,但是你的電話(S)到drawRCircles本身只負責10人。

更具體地說,這些線條畫了1個圓。

// draw main circle 
    graphics.setColor(Color.WHITE); 
    graphics.drawOval(xOrigin, yOrigin, n, n); 

無論您是否擁有drawRCircles函數,該圓圈都會被繪製。 (嘗試取出它並運行你的代碼,看看會發生什麼。)

然後,你的代碼電話drawRCircles功能的11倍,但只有吸引 10圈,因爲它的最後一次通話,與水平= 0失敗測試if(level > 0)

它創建的圖像下降,到目前爲止,我不能指望圓:/

一個快速提示:既然你想知道,鑑於N最高水平,將它畫NN+1級別的圈子,您也可以嘗試將level變量更改爲更易於管理的項目(如2)並以可視方式進行檢查。

再回到你的代碼上面,而忽略了draw main circle部分(因爲它是獨立於你的遞歸循環繪圖的),你有

drawRCircles(graphics,xOrigin,yOrigin,radius,level); // call recursive method 
    System.out.println(level); 

和你public void drawRCircles(…)函數中,

drawRCircles(…,level-1); 

讓我們檢查它與級別= 2而不是10:

drawRCircles(…, 2) 
     --> Check if 1 > 0. 
      --> Yes, 1 > 0 so drawRCircles(…, 1) 
       --> Check if 0 > 0. 
        --> No, 0 = 0, so stop. 

層數= 2 =繪製的遞歸圓的數量。

+0

感謝您的信息!都發布瞭解決方案的工作,但我打算將您的標記爲正確的答案,因爲您還深入瞭解了一些內容並給出了一些提示,謝謝:D –