Pergunta

I have a homework assignment that I am ready to turn in, in the assignment I had to use recursion to draw nested circles 10 levels deep, after banging my head against this for a few hours I finally completed it. My only question is, is the image I am drawing 10 levels deep or actually 11?

This question comes from the fact that I have specifically state the recursion to end when it has gone through 10 levels, but I do tell the method to draw the original circles then it calls itself. this is making me think that it draws the first level then goes down 10 to make a total of 11 levels. the Image it creates goes down so far that I can not count the circles :/

any clarification would be appreciated, thanks!

    // 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
Foi útil?

Solução

Your code is drawing 11 circles but your call(s) to the drawRCircles itself is responsible for only 10 of them.

More specifically, these lines are drawing 1 circle.

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

This circle gets drawn regardless of whether or not you even have a drawRCircles function. (Try removing it and running your code to see what happens.)

Then, your code calls the drawRCircles function 11 times, but only draws 10 circles since the last call to it, with level = 0 fails the test if(level > 0).

the Image it creates goes down so far that I can not count the circles :/

A quick tip: since you want to know if, given max level of N, will it draw N or N+1 levels of circles, you could also just try changing your level variable to something more manageable (like 2) and checking it visually.

Going back to your code above, and ignoring the draw main circle portion (since it's independent of your recursive circle drawing) you have

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

and within your public void drawRCircles(…) function,

    drawRCircles(…,level-1);

Let's check it with level = 2 instead of 10:

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

Number of levels = 2 = number of recursive circles drawn.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top