/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package st;
import java.awt.Color;
import java.awt.Polygon;
import java.util.Scanner;
import javax.swing.JFrame;
/**
*
* @author aginta geniusa
*/
public class MAIN extends Sierpinski {
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/20;
//point 2 -- bottom right
int p2x = WINDOWSIZE-p1y;
int p2y = WINDOWSIZE-p1y;
//point 3 -- bottom left
int p3x = WINDOWSIZE/20;
int p3y = WINDOWSIZE-p1y;
// ask user berapa bagian ?
Scanner keyboard = new Scanner(System.in);
System.out.print("berapa bagian ? ");
int splits = keyboard.nextInt();
polyList = new Polygon[(int)Math.pow(3,splits-1)];
// new window
JFrame f = new JFrame("Sierpinski");
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);
}
}
--------------------------------------------------------------------------------------------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package st;
/**
*
* @author aginta geniusa
*/
import java.awt.*;
import javax.swing.*;
public class Sierpinski extends JPanel{
// set window size here
// 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;
}
@Override
public void paint(Graphics g)
{
for(int i = 0; i < polyList.length; i++)
{
g.fillPolygon(polyList[i]);
g.setColor(Color.red);
}
}
}
No comments:
Post a Comment