2 Replies - 1154 Views - Last Post: 01 November 2011 - 08:45 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: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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8325
  • View blog
  • Posts: 31,857
  • 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 :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1