# Recursive method to draw 4 Heighway Dragons

Page 1 of 1

## 2 Replies - 1925 Views - Last Post: 01 November 2011 - 08:45 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=253886&amp;s=24e2cecaabb7703d098c02c4b0f95e1b&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:28 PM

``` /**
* 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 junsugal

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

## Re: Recursive method to draw 4 Heighway Dragons

Posted 01 November 2011 - 08:35 PM

I accidentally post twice. I didn't mean to post this one. Because i forgot to write down the description.
please excuse me as this is my first time here.

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Recursive method to draw 4 Heighway Dragons

Posted 01 November 2011 - 08:45 PM

No problem. Admitted sins are half forgiven