1 Replies - 276 Views - Last Post: 01 November 2011 - 08:44 PM Rate Topic: -----

#1 junsugal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-March 10

Recursive method to draw 4 Heighway Dragons

Posted 01 November 2011 - 08:33 PM

I am new to Java and I don't really understand about recursion.
Please lead me on how to write a recursive method.
As for the base class, I think I am on the right track.
My teacher said the base class should be 0 and 1.
Where level 0 is only drawing 4 lines facing north south east west
The output for level 0 and 1 are right.
But, I don't know how to write recursive method for level 3 onwards.
 /**
     * Draw the dragon curve by calling the private method of draw
     * 
     * @param g The graphics context where the drawing will take place
     */
    public void drawDragon(Graphics g){
        draw(level,g);
    }
    
    /**
     * Private and recursive method to draw the Dragon
     *
     * @param level The interation of the dragon
     * @param g The graphic contexts the drawing take place
     */
    private void draw(int level, Graphics g){
        int peakXNorth = (int)((center.getX() + ( north.getX()-center.getX())*0.5 -(center.getY()-north.getY())*0.5));
        int peakYNorth= (int)((center.getX() - ( north.getX()-center.getX())*0.5 -(center.getY()-north.getY())*0.5));
        int peakXSouth = (int)((center.getX() + ( south.getX()-center.getX())*0.5 -(center.getY()-south.getY())*0.5));
        int peakYSouth= (int)((center.getX() - ( south.getX()-center.getX())*0.5 -(center.getY()-south.getY())*0.5));
        int peakXWest = (int)((center.getX() + ( west.getX()-center.getX())*0.5 -(center.getY()-west.getY())*0.5));
        int peakYWest = (int)((center.getX() - ( west.getX()-center.getX())*0.5 -(center.getY()-west.getY())*0.5));
        int peakXEast = (int)((center.getX() + ( east.getX()-center.getX())*0.5 -(center.getY()-east.getY())*0.5));
        int peakYEast = (int)((center.getX() - ( east.getX()-center.getX())*0.5 -(center.getY()-east.getY())*0.5));

       if(level < 0){
            throw new IllegalArgumentException();
       }
       
       // base class 
       if(level == 0){
            g.setColor(Color.RED);
            g.drawLine((int)center.getX(), (int)center.getY(), (int)north.getX(), (int)north.getY());
            g.setColor(Color.BLUE);
            g.drawLine((int)center.getX(), (int)center.getY(), (int)south.getX(), (int)south.getY());
            g.setColor(Color.BLACK);
            g.drawLine((int)center.getX(), (int)center.getY(), (int)west.getX(), (int)west.getY());
            g.setColor(Color.GREEN);
            g.drawLine((int)center.getX(), (int)center.getY(), (int)east.getX(), (int)east.getY());
        }
       // base class 
        if(level == 1){    
           g.setColor(Color.RED);
           g.drawLine((int)center.getX(), (int)center.getY(), peakXNorth, peakYNorth);
           g.drawLine(peakXNorth,peakYNorth, (int)north.getX(), (int)north.getY());
           g.setColor(Color.BLUE);
           g.drawLine((int)center.getX(), (int)center.getY(), peakXSouth, peakYSouth);
           g.drawLine(peakXSouth,peakYSouth, (int)south.getX(), (int)south.getY());
           g.setColor(Color.BLACK);
           g.drawLine((int)center.getX(), (int)center.getY(), peakXWest, peakYWest);
           g.drawLine(peakXWest,peakYWest, (int)west.getX(), (int)west.getY());
           g.setColor(Color.GREEN);
           g.drawLine((int)center.getX(), (int)center.getY(), peakXEast, peakYEast);
           g.drawLine(peakXEast,peakYEast, (int)east.getX(), (int)east.getY());
         
       }else if (level > 1){
                draw(level-1,g);
        }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Recursive method to draw 4 Heighway Dragons

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Recursive method to draw 4 Heighway Dragons

Posted 01 November 2011 - 08:44 PM

Since the only thing changing from method to method here is the level, you should use the level as some sort of guide as to what to draw. Just doing If statements would make it kinda pointless as you can just do drawLevel3() if you wanted to do that. I'm not sure how to use recursion to draw a dragon, but the method should perform an operation that is unique to each level.

You call dragon.draw(3,g); and it will call draw(2,g) and draw(1,g) on it's own. so 3,g should draw the third layer of the dragon or something based on the level variable. 2,g should draw the second layer, based on layer, etc.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1