Results 1 to 7 of 7
  1. #1
    bap2 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default Sierpisnki Triagle

    When I run this code it only works for the base case.
    Java Code:
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    
    public class Sierpinski extends JPanel
    {
    	// set window size here
    	public static final int WINDOWSIZE = 500;
    
    	// some static variables
    	static Polygon[] polyList;
    	static int count = 0;
    
    	public static void drawTriangles(int splits, int ax, int ay, int bx, int by, int cx, int cy)
    	{
    		int[] p1 = {ax,ay};
    		int[] p2 = {bx,by};
    		int[] p3 = {cx,cy};
    
    		if(splits == 1){
    			//create basic triangle
    			int[]xs={ax,bx,cx};
    			int[]ys={ay,by,cy};
    			Polygon p = new Polygon(xs,ys,xs.length);
    
    			polyList[count] = p;
    			count++;
    		}
    		else
    		{
    			//get midpoints for each side
    			int[] p4 = getMidpoint(ax, ay, bx, by);
    			int[] p5 = getMidpoint(bx, by, cx, cy);
    			int[] p6 = getMidpoint(ax, ay, cx, cy);
    
    			// 3 new triangles
    			drawTriangles(splits - 1,p1[0],p1[1],p4[0],p4[1],p6[0],p6[1]);
    			drawTriangles(splits - 1,p4[0],p4[1],p2[0],p2[1],p5[0],p5[1]);
    			drawTriangles(splits - 1,p6[0],p6[1],p5[0],p5[1],p3[0],p3[1]);
    		}
    	}
    
    	// returns the midpoint as an array [x,y] of any line given the coordinates
    	public static int[] getMidpoint(int ax, int ay, int bx, int by)
    	{
    		int[] mid = new int[2];
    
    		mid[0] = (ax + bx)/2;
    		mid[1] = (ay + by)/2;
    
    		return mid;
    	}
    
    	public void paint(Graphics g)
    	{
    		for(int i = 0; i < polyList.length; i++)
    		{
    			g.fillPolygon(polyList[i]);
    		}
    	}
    
    	public static void main (String[] args)
    	{
    		// make the base triangle based on the window size
    		//point 1 -- top
    		int p1x = WINDOWSIZE/2;
    		int p1y = WINDOWSIZE/10;
    		//point 2 -- bottom right
    		int p2x = WINDOWSIZE-p1y;
    		int p2y = WINDOWSIZE-p1y;
    		//point 3 -- bottom left
    		int p3x = WINDOWSIZE/10;
    		int p3y = WINDOWSIZE-p1y;
    
    		// ask user how many splits
    		Scanner keyboard = new Scanner(System.in);
    		System.out.print("How many splits? ");
    		int splits = keyboard.nextInt();
    		polyList = new Polygon[(int)Math.pow(3,splits-1)];
    
    
    		// new window
    		JFrame f = new JFrame("Sierpinski's Triangle!");
    		f.setSize(WINDOWSIZE,WINDOWSIZE);
    		f.setBackground(Color.white);
    		f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		f.setVisible(true);
    
    		drawTriangles(splits,p1x,p1y,p2x,p2y,p3x,p3y);
    
    		Sierpinski s = new Sierpinski();
    		f.add(s);
    	}
    }

  2. #2
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Hi,

    And you question is...

    BTW On my system your code works like a charm (as long as I do not enter too much splits because otherwise the triangles get too small... ;))
    I'm new to Java but I like to help where ever I can. :)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't forget to override JPanel's paintComponent method, not the paint method. Also you'll want to allow for antialiasing by setting the Graphics2D's rendering hints within the paintComponent method.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Myself, I like the random version of the Sierpinski:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.Point2D;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class SierpinskiB extends JPanel {
       private static final int BI_HEIGHT = 600;
       private static final int BI_WIDTH = 600;
       private static final Point2D[] VERTICES = {new Point2D.Double(300, 20), new Point2D.Double(20, 580), new Point2D.Double(580, 580)};
       private static final int TIMER_DELAY = 5;
       private static final int FOREGROUND_COLOR_RGB = Color.red.getRGB();
       private BufferedImage bImage = new BufferedImage(BI_WIDTH, BI_HEIGHT, BufferedImage.TYPE_INT_RGB);
       private Point2D currentPoint = new Point2D.Double(300, 300);
       private Random random = new Random();
       
       public SierpinskiB() {
          setPreferredSize(new Dimension(BI_WIDTH, BI_HEIGHT));
    
          new Timer(TIMER_DELAY, new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                timerActionPerformed();
             }
          }).start();
       }
       
       private void timerActionPerformed() {
          Point2D vertex = VERTICES[random.nextInt(VERTICES.length)];
          double x = (currentPoint.getX() + vertex.getX()) /2;
          double y = (currentPoint.getY() + vertex.getY()) /2;
          currentPoint = new Point2D.Double(x, y);
          bImage.setRGB((int)x, (int)y, FOREGROUND_COLOR_RGB);
          repaint();
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          g.drawImage(bImage, 0, 0, null);
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("Sierpinski");
          frame.getContentPane().add(new SierpinskiB());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Myself, I like the random version of the Sierpinski:
    Bah, that's an IFS (Iterated Function System); those IFSs were ever so promising in the early 90s for image compression but now they're only here for generating fancy pictures ;-) Did you buy Barnsley's book "Fractals Everywhere"? What a waste that was ...

    kind regards,

    Jos
    Last edited by JosAH; 11-06-2010 at 01:59 PM. Reason: typo ...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    Bah, that's an IFS (Iterated Function System); those IFSs were ever so promising in the early 90s for image compression but now they're only here for generating fancy pictures ;-) Did you buy Barnsley's book "Fractals Everywhere"? What a waste that was ...
    Not sure as I've got a bunch of fractal books scattered all over the darn place. I used to think I could somehow apply fractals and chaos to my work, but found that we have nothing but chaos, and not the mathematical kind.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Not sure as I've got a bunch of fractal books scattered all over the darn place. I used to think I could somehow apply fractals and chaos to my work, but found that we have nothing but chaos, and not the mathematical kind.
    That's nothing a bit of data fitting won't solve ;-)

    kind regards,

    Jos

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •