Recursive method to draw 4 Heighway Dragons

Page 1 of 1

1 Replies - 566 Views - Last Post: 01 November 2011 - 08:44 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=253889&amp;s=715f710007e0641c2c15b493d7785f4e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 junsugal

Reputation: 0
• 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.
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

• D.I.C Lover

Reputation: 447
• Posts: 1,032
• 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.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }