Sierpinski Triangle A sierpinski is composed of an infinite number of sub-triang
ID: 3907305 • Letter: S
Question
Sierpinski Triangle A sierpinski is composed of an infinite number of sub-triangles, so it is almost impossible to draw. However, we can produce an algorithm that can produce various levels that approximate the actual fractal. A sierpinski triangle is created as follows: 1. Draw an equilateral triangle. This is considered to be a Sierpinski fractal of level 1, as seen in fig.01. This initial triangle has three points (p1, p2, p3) set in proportions to the frame size. Fig.01: Sierpinski triangle level: 1 2. At level 2, draw three smaller triangles that are contained within the original triangle. Fig.02: Sierpinski triangle level: 2 3. Apply this process in a recursive manner. Just as we replaced the original triangle with three inner triangles, we replace each of the three triangles in level 2 with three inner triangles toExplanation / Answer
here is your program : ------------->>>>>>>>>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Sierpinski extends JFrame implements ActionListener{
private Point p1;
private Point p2;
private Point p3;
private JPanel pan1;
private int level;
private JTextField lab;
private SierpinskiPanel pan;
private JButton bplus,bminus;
public Sierpinski(){
super("Sierpinski");
setSize(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
pan = new SierpinskiPanel();
add(pan,BorderLayout.CENTER);
bplus = new JButton(" + ");
bplus.addActionListener(this);
bminus = new JButton(" - ");
bminus.addActionListener(this);
pan1 = new JPanel();
lab = new JTextField(" n = "+level);
lab.setEditable(false);
pan1.setLayout(new FlowLayout());
pan1.add(bplus);
pan1.add(bminus);
pan1.add(lab);
add(pan1,BorderLayout.SOUTH);
level = 3;
p1 = new Point();
p2 = new Point();
p3 = new Point();
p1.setLocation(100,490);
p2.setLocation(300,90);
p3.setLocation(500,490);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == bplus){
level++;
if(level > 1){
bminus.setEnabled(true);
}
lab.setText(" n = "+level);
repaint();
}else if(e.getSource() == bminus && level != 1){
level--;
lab.setText(" n = "+level);
if(level == 1){
bminus.setEnabled(false);
}
repaint();
}
}
public void paint(Graphics g){
int[] x = new int[3];
int[] y = new int[3];
x[0] = (int)(Math.abs(p1.getX()));
x[1] = (int)(Math.abs(p2.getX()));
x[2] = (int)(Math.abs(p3.getX()));
y[0] = (int)(Math.abs(p1.getY()));
y[1] = (int)(Math.abs(p2.getY()));
y[2] = (int)(Math.abs(p3.getY()));
Polygon pp = new Polygon(x,y,3);
g.setColor(Color.BLACK);
g.fillPolygon(pp);
pan.drawTriangle(g,level-1,p1,p2,p3);
}
public static void main(String[] args) {
Sierpinski s = new Sierpinski();
s.repaint();
}
}
class SierpinskiPanel extends JPanel{
public SierpinskiPanel(){
setSize(500,500);
setVisible(true);
}
public void drawTriangle(Graphics pen,int l,Point p1,Point p2,Point p3){
if(l == 0){
return;
}
int[] x = new int[3];
int[] y = new int[3];
x[0] = (int)(Math.abs(p1.getX() + p2.getX())/2);
x[1] = (int)(Math.abs(p1.getX() + p3.getX())/2);
x[2] = (int)(Math.abs(p2.getX() + p3.getX())/2);
y[0] = (int)(Math.abs(p1.getY() + p2.getY())/2);
y[1] = (int)(Math.abs(p1.getY() + p3.getY())/2);
y[2] = (int)(Math.abs(p2.getY() + p3.getY())/2);
Polygon p = new Polygon(x,y,3);
pen.setColor(Color.WHITE);
pen.fillPolygon(p);
Point temp1 = new Point();
Point temp2 = new Point();
temp1.setLocation(x[1],y[1]);
temp2.setLocation(x[0],y[0]);
drawTriangle(pen,l-1,p1,temp1,temp2);
temp1.setLocation(x[2],y[2]);
drawTriangle(pen,l-1,temp2,p2,temp1);
temp2.setLocation(x[1],y[1]);
drawTriangle(pen,l-1,temp2,temp1,p3);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.