Advertisement
abdulfatirs

RecursionTree.java | Fractal Basics, Recursion Trees

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