gRecursion

In this project I implemented some graphical shapes using recursive functions. I just explain TREE (F8) codes.

to download this project use below link:

gRecursion v0.01

 

For example:   a Tree (F8)

// 
drawF8(g2, 350, 600, 350, 450);

// (x1,y1)-(x2,y2) is the base line of tree

//(newX1,new Y1)   (newX2,new Y2)
//         \       /     
//          \     / 
//           \   /
//            \ / 
//             |<--- (x2,y2)
//             |
//             |
//             |
//             |
//             |<--- (x1,y1)


private void drawF8(Graphics2D g2, int x1, int y1, int x2, int y2)
    {
		int newX1, newY1, newX2, newY2;
		
        // to draw in better mood
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		g2.setColor(Color.black);
		g2.drawLine(x1, y1, x2, y2);
		
		int degree=(int) Math.toDegrees(Math.atan2((y2-y1), (x2-x1)));
		int w=(int) Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
		
        // for any line (x1,y1)-(x2,y2),
        // rotate graphics to make that line horizontal and draw 2 tree branches
        
        //             (x2+(2*w)/3, y2-w/3)
        //                         /
        //                        /
        //                       /  
        //     (x1,y1) ---------  (x2,y2)
        //                       \   
        //                        \
        //                         \
        //              (x2+(2*w)/3, y2+w/3)
        
        // after that rerotate the graphics to first state        
        // this way is too easy !!!
        
		g2.rotate(Math.toRadians(degree), x2, y2);
		
		g2.drawLine(x2, y2, x2+(2*w)/3, y2-w/3);
		g2.drawLine(x2, y2, x2+(2*w)/3, y2+w/3);
        
		g2.rotate(-Math.toRadians(degree), x2, y2);
		       
        
        // but newX, newY (for example x2+(2*w)/3, y2-w/3) should be calculated in current graphics
		// calculate newX1, newY1
        double[] pt= { x2+(2*w)/3, y2-w/3 };
		AffineTransform.getRotateInstance(Math.toRadians(degree), x2, y2).transform(pt, 0, pt, 0, 1);
		newX1=(int) pt[0];
		newY1=(int) pt[1];
		
    	// calculate newX2, newY2
		pt[0]=x2+(2*w)/3;
		pt[1]=y2+w/3;
		AffineTransform.getRotateInstance(Math.toRadians(degree), x2, y2).transform(pt, 0, pt, 0, 1);
		newX2=(int) pt[0];
		newY2=(int) pt[1];
		
        // repeat this action for two branches
		if (w>5)
		{
			drawF8(g2, x2, y2, newX1, newY1);
			drawF8(g2, x2, y2, newX2, newY2);
		}
		
	}

Rec000

a Spiral Tree (F11)

Rec001

Reflective Desktop (F10)

Rec003

A Fractal (F16)

Rec002