Sunday, September 20, 2015

Sierpinski Triangles

The Sierpinski triangle (also with the original orthography SierpiƄski), also called the Sierpinski gasket or the Sierpinski Sieve, is a fractal and attractive fixed set with the overall shape of an equilateral triangle, subdivided recursively into smaller equilateral triangles.

The concept of the Sierpinski triangle is very simple:
  • Take a triangle
  • Create four triangles out of that one by connecting the centers of each side
  • Cut out the middle triangle
  • Repeat the process with the remaining triangles
The Sierpinsky Triangle is a fractal created by taking a triangle, decreasing the height and width by 1/2, creating 3 copies of the resulting triangle, and place them such each triangle touches the other two on a corner. This process is repeated over and over again with the resulting triangles to produce the Sierpinski triangle, as illustrated below.



This is the examples out with 5 iteration
                               *                               
                              * *                              
                             *   *                             
                            * * * *                            
                           *       *                           
                          * *     * *                          
                         *   *   *   *                         
                        * * * * * * * *                        
                       *               *                       
                      * *             * *                      
                     *   *           *   *                     
                    * * * *         * * * *                    
                   *       *       *       *                   
                  * *     * *     * *     * *                  
                 *   *   *   *   *   *   *   *                 
                * * * * * * * * * * * * * * * *                
               *                               *               
              * *                             * *              
             *   *                           *   *             
            * * * *                         * * * *            
           *       *                       *       *           
          * *     * *                     * *     * *          
         *   *   *   *                   *   *   *   *         
        * * * * * * * *                 * * * * * * * *        
       *               *               *               *       
      * *             * *             * *             * *      
     *   *           *   *           *   *           *   *     
    * * * *         * * * *         * * * *         * * * *    
   *       *       *       *       *       *       *       *   
  * *     * *     * *     * *     * *     * *     * *     * *  
 *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   * 
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public static Vector < String > sierpinski(int n) {
    Vector < String > down = new Vector < String > (Arrays.asList("*"));
    String space = " ";
    for (int i = 0; i < n; i++) {
        Vector < String > newDown = new Vector < String > ();
        for (String x: down)
            newDown.add(space + x + space);
        for (String x: down)
            newDown.add(x + " " + x);

        down = newDown;
        space += space;
    }
    return down;
}

public static void main(String[] args) {
    for (String x: sierpinski(5))
        System.out.println(x);
}

No comments:

Post a Comment