abdulfatirs

RecursionTree.java | Fractal Basics, Recursion Trees

May 14th, 2014
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. // @author Abdul Fatir
  2. import java.io.*;
  3. import javax.imageio.*;
  4. import java.awt.*;
  5. import java.awt.image.*;
  6. import static java.lang.System.out;
  7.  
  8. public class RecursionTree
  9. {
  10.     private double ANGLE = Math.PI/8;
  11.     private float SHRINK_FACTOR = 1.5f;
  12.     private int MAX_RECURSIONS = 10;
  13.     public void drawBranch(Graphics g,int pointX, int pointY,double directionX,double directionY, float size, int recursion_number)
  14.     {
  15.         // Finding the End Point of the Vector(line) to draw it
  16.         int x2 = (int)(pointX + directionX*size), y2 = (int)(pointY + directionY*size);
  17.         // Setting the Colour of the line
  18.         java.util.Random random = new java.util.Random();
  19.         Color randomColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
  20.         g.setColor(randomColor);
  21.         // Drawing the line from initial to final point
  22.         g.drawLine(pointX,pointY,x2,y2);
  23.         // If the maximum recursions are reached we need to stop
  24.         if(recursion_number>=MAX_RECURSIONS)
  25.             {
  26.                 return;
  27.             }
  28.         // Finding the size of next branch by shrinking the current branch
  29.         float new_branch_size = size/SHRINK_FACTOR;
  30.         // Finding the Direction Vector of the next branch
  31.         double directionX2  =  Math.sin(ANGLE) * directionY + Math.cos(ANGLE) * directionX;
  32.         double directionY2 = -(Math.sin(ANGLE) * directionX) +  Math.cos(ANGLE) * directionY;
  33.         // Incrementing the recursion_number by 1
  34.         int n2 = recursion_number+1;
  35.         // Drawing the branch
  36.         drawBranch(g,x2,y2,directionX2,directionY2,new_branch_size,n2);
  37.         // Finding the Direction Vector of the corresponding branch on the other side i.e. with angle = -ANGLE
  38.         directionX2  = Math.cos(-ANGLE) * directionX + Math.sin(-ANGLE) * directionY;
  39.         directionY2 = -Math.sin(-ANGLE) * directionX + Math.cos(-ANGLE) * directionY;
  40.         // Drawing the twin branch
  41.         drawBranch(g,x2,y2,directionX2,directionY2,new_branch_size,n2);
  42.        
  43.     }
  44.    
  45.     public static void main(String args[])throws IOException
  46.     {
  47.         int IMG_WIDTH = 800;
  48.         int IMG_HEIGHT = 500;
  49.         // Creating a next 800x500 RGB Image
  50.         BufferedImage img = new BufferedImage(IMG_WIDTH,IMG_HEIGHT,BufferedImage.TYPE_3BYTE_BGR);
  51.         // Getting the graphics object to draw on
  52.         Graphics g = img.getGraphics();
  53.         // Calling the drawBranch(Graphics, IntialX, InitialY, DirectionVectorX, DirectionVectorY, Length_Of_First_Line, Recursion_Number)
  54.         new RecursionTree().drawBranch(g,IMG_WIDTH/2,IMG_HEIGHT,0,-1,IMG_HEIGHT/3,0);
  55.         // Saving the Image
  56.         ImageIO.write(img,"PNG",new File("tree.png"));
  57.     }
  58.    
  59. }
Advertisement
Add Comment
Please, Sign In to add comment