segunda-feira, 16 de maio de 2016

Fractais recursivos - JAVA

 1- Koch Snowflake;
 2 - Sierpinski Triangle;
 3 - Koch Snowflake and Sierpinski Triangle;
 4 - Fractal Tree;
 5 - Twig #1;
 6 - Twig #2;
 7 - Twig #3;
 8 - Twig #4.

Algoritmo em Java que permite gerar Fractais em diferentes níveis de interação, utilizando de métodos recursivos.

Código:

package fractal;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Fractal extends JFrame implements ActionListener {

    double sin60 = Math.sin(3.14 / 3.);
    int nivel_de_recursividade = 1;
    int color = 10;
    int op = 0;
    int t = 1;

    JPanel P1, P2, P3;
    JComboBox C1, C2;
    JTextField T1;
    JLabel L1, L2;
    JButton B1;

    public static void main(String[] args) {
        Fractal app = new Fractal(20);
        app.setVisible(true);
    }

    public void Snowflake(Graphics g, int i, double xp12, double yp12, double xp22, double yp22) {
        double dx = (xp22 - xp12) / 2.;
        double dy = (yp22 - yp12) / 2.;
        double xp32 = xp12 + dx - 2 * dy * sin60;
        double yp32 = yp12 + dy + 2 * dx * sin60;

        SnowflakeRecursivo(g, i, xp32, yp32, xp22, yp22);
        SnowflakeRecursivo(g, i, xp22, yp22, xp12, yp12);
        SnowflakeRecursivo(g, i, xp12, yp12, xp32, yp32);
    }

    public void SnowflakeRecursivo(Graphics g, int i, double xp12, double yp12, double xp22, double yp22) {
        double dx = (xp22 - xp12) / 3.;
        double dy = (yp22 - yp12) / 3.;
        double xx = xp12 + 3 * dx / 2. - dy * sin60;
        double yy = yp12 + 3 * dy / 2. + dx * sin60;

        if (color == 254 || color == 1) {
            t = t * -1;
            color = color + t;
        } else {
            color = color + t;
        }

        if (i <= 0) {
            g.setColor(new Color(255 - color, color, 255));
            g.drawLine((int) xp12, (int) yp12, (int) xp22, (int) yp22);
        } else {
            SnowflakeRecursivo(g, i - 1, xp12, yp12, xp12 + dx, yp12 + dy);
            SnowflakeRecursivo(g, i - 1, xp12 + dx, yp12 + dy, xx, yy);
            SnowflakeRecursivo(g, i - 1, xx, yy, xp22 - dx, yp22 - dy);
            SnowflakeRecursivo(g, i - 1, xp22 - dx, yp22 - dy, xp22, yp22);
        }
    }

    public void Triangle(Graphics g, int i, double xp12, double yp12, double xp22, double yp22) {
        double dx = (xp22 - xp12) / 2.;
        double dy = (yp22 - yp12) / 2.;
        double xp32 = xp12 + dx - 2 * dy * sin60;
        double yp32 = yp12 + dy + 2 * dx * sin60;
        double dx1 = (xp22 + xp12) / 2.;
        double dy1 = (yp22 + yp12) / 2.;
        double dx2 = (xp32 + xp22) / 2.;
        double dy2 = (yp32 + yp22) / 2.;
        double dx3 = (xp12 + xp32) / 2.;
        double dy3 = (yp12 + yp32) / 2.;

        if (color == 254 || color == 1) {
            t = t * -1;
            color = color + t;
        } else {
            color = color + t;
        }

        if (i <= 0) {
            g.setColor(new Color(255, color, 255 - color));
            g.drawLine((int) xp12, (int) yp12, (int) xp22, (int) yp22);
            g.setColor(new Color(255, color, 255 - color));
            g.drawLine((int) xp22, (int) yp22, (int) xp32, (int) yp32);
            g.setColor(new Color(255, color, 255 - color));
            g.drawLine((int) xp32, (int) yp32, (int) xp12, (int) yp12);
        } else {
            Triangle(g, i - 1, xp12, yp12, dx1, dy1);
            Triangle(g, i - 1, dx1, dy1, xp22, yp22);
            Triangle(g, i - 1, dx3, dy3, dx2, dy2);
        }
    }

    public void Snowflake_Triangle(Graphics g, int i, double xp12, double yp12, double xp22, double yp22) {
        Snowflake(g, i, xp12, yp12, xp22, yp22);
        Triangle(g, i, xp12, yp12, xp22, yp22);
    }

    public void FractalTree(Graphics2D g2d, int i, double x1, double y1, double angle) {
        double x2 = x1 + (Math.sin(Math.toRadians(angle)) * i * 12.7);
        double y2 = y1 + (Math.cos(Math.toRadians(angle)) * i * 12.7);

        g2d.setStroke(new BasicStroke(i * 1.5f));
        g2d.setColor(new Color(117, 200 - i * 20, 0));
        if (i > 0) {
            g2d.drawLine((int) x1, (int) y1, (int) x2, (int) y2);
            FractalTree(g2d, i - 1, x2, y2, angle - 29 + i);
            FractalTree(g2d, i - 1, x2, y2, angle + 29 - i);
        }
    }

    public void Twig1(Graphics g, int i, double p1x, double p1y, double p2x, double p2y) {
        double dx = (p2x - p1x);
        double dy = (p2y - p1y);
        double p3x = p2x - dx + dy;
        double p3y = p2y - dy - dx;
        double p4x = p1x + dx + dy;
        double p4y = p1y + dy - dx;
        double dx2 = dx / 3.;
        double dy2 = dy / 3.;
        double p5x = p4x - dx2 + dy2 * Math.sin(Math.toRadians(65));
        double p5y = p4y - dy2 - dx2 * Math.sin(Math.toRadians(65));

        g.setColor(new Color(255, 0 + 28 * i, 0));
        g.drawLine((int) p1x, (int) p1y, (int) p3x, (int) p3y);
        g.drawLine((int) p1x, (int) p1y, (int) p2x, (int) p2y);
        g.drawLine((int) p2x, (int) p2y, (int) p4x, (int) p4y);
        g.drawLine((int) p3x, (int) p3y, (int) p4x, (int) p4y);
        g.drawLine((int) p3x, (int) p3y, (int) p5x, (int) p5y);
        g.drawLine((int) p5x, (int) p5y, (int) p4x, (int) p4y);

        if (i > 0) {
            Twig1(g, i - 1, p3x, p3y, p5x, p5y);
            Twig1(g, i - 1, p5x, p5y, p4x, p4y);
        }
    }

    public void Twig2(Graphics g, int i, double p1x, double p1y, double p2x, double p2y) {
        double dx = (p2x - p1x);
        double dy = (p2y - p1y);
        double p3x = p2x - dx + dy;
        double p3y = p2y - dy - dx;
        double p4x = p1x + dx + dy;
        double p4y = p1y + dy - dx;
        double dx2 = dx / 2.;
        double dy2 = dy / 2.;
        double p5x = p4x - dx2 + dy2 * sin60;
        double p5y = p4y - dy2 - dx2 * sin60;

        g.setColor(new Color(0, 200, 0 + 28 * i));
        g.drawLine((int) p1x, (int) p1y, (int) p3x, (int) p3y);
        g.drawLine((int) p1x, (int) p1y, (int) p2x, (int) p2y);
        g.drawLine((int) p2x, (int) p2y, (int) p4x, (int) p4y);
        g.drawLine((int) p3x, (int) p3y, (int) p4x, (int) p4y);
        g.drawLine((int) p3x, (int) p3y, (int) p5x, (int) p5y);
        g.drawLine((int) p5x, (int) p5y, (int) p4x, (int) p4y);

        if (i > 0) {
            Twig2(g, i - 1, p3x, p3y, p5x, p5y);
            Twig2(g, i - 1, p5x, p5y, p4x, p4y);
        }
    }

    public void Twig3L(Graphics g, int i, double p1x, double p1y, double p2x, double p2y) {
        double dx = (p2x - p1x);
        double dy = (p2y - p1y);
        double p3x = p2x - dx - dy;
        double p3y = p2y - dy + dx;
        double p4x = p1x + dx - dy;
        double p4y = p1y + dy + dx;
        double dx2 = dx / 3.;
        double dy2 = dy / 3.;
        double p5x = p4x - dx2 - dy2 * sin60;
        double p5y = p4y - dy2 + dx2 * sin60;

        g.setColor(new Color(255, 0, 0 + 28 * i));
        g.drawLine((int) p1x, (int) p1y, (int) p3x, (int) p3y);
        g.drawLine((int) p1x, (int) p1y, (int) p2x, (int) p2y);
        g.drawLine((int) p2x, (int) p2y, (int) p4x, (int) p4y);
        g.drawLine((int) p3x, (int) p3y, (int) p4x, (int) p4y);
        g.drawLine((int) p3x, (int) p3y, (int) p5x, (int) p5y);
        g.drawLine((int) p5x, (int) p5y, (int) p4x, (int) p4y);

        if (i > 0) {
            Twig3L(g, i - 1, p3x, p3y, p5x, p5y);
            Twig3R(g, i - 1, p4x, p4y, p5x, p5y);
        }
    }

    public void Twig3R(Graphics g, int i, double p1x, double p1y, double p2x, double p2y) {
        double dx = (p2x - p1x);
        double dy = (p2y - p1y);
        double p3x = p2x - dx + dy;
        double p3y = p2y - dy - dx;
        double p4x = p1x + dx + dy;
        double p4y = p1y + dy - dx;
        double dx2 = dx / 3.;
        double dy2 = dy / 3.;
        double p5x = p4x - dx2 + dy2 * sin60;
        double p5y = p4y - dy2 - dx2 * sin60;

        g.setColor(new Color(255, 0, 0 + 28 * i));
        g.drawLine((int) p1x, (int) p1y, (int) p3x, (int) p3y);
        g.drawLine((int) p1x, (int) p1y, (int) p2x, (int) p2y);
        g.drawLine((int) p2x, (int) p2y, (int) p4x, (int) p4y);
        g.drawLine((int) p3x, (int) p3y, (int) p4x, (int) p4y);
        g.drawLine((int) p3x, (int) p3y, (int) p5x, (int) p5y);
        g.drawLine((int) p5x, (int) p5y, (int) p4x, (int) p4y);

        if (i > 0) {
            Twig3L(g, i - 1, p5x, p5y, p3x, p3y);
            Twig3R(g, i - 1, p5x, p5y, p4x, p4y);
        }
    }

    public void Twig4(Graphics g, int i, double p1x, double p1y, double p2x, double p2y) {
        double dx = (p2x - p1x);
        double dy = (p2y - p1y);
        double p3x = p2x - dx + dy;
        double p3y = p2y - dy - dx;
        double p4x = p1x + dx + dy;
        double p4y = p1y + dy - dx;
        dx = dx - dx / 5;
        dy = dy - dy / 5;
        double p5x = p3x + dx + .65 * dy * Math.sin(Math.toRadians(60));
        double p5y = p3y + dy - .65 * dx * Math.sin(Math.toRadians(60));

        g.drawLine((int) p1x, (int) p1y, (int) p3x, (int) p3y);
        g.drawLine((int) p1x, (int) p1y, (int) p2x, (int) p2y);
        g.drawLine((int) p2x, (int) p2y, (int) p4x, (int) p4y);
        g.drawLine((int) p3x, (int) p3y, (int) p4x, (int) p4y);
        g.drawLine((int) p3x, (int) p3y, (int) p5x, (int) p5y);
        g.drawLine((int) p5x, (int) p5y, (int) p4x, (int) p4y);

        if (i > 0) {
            g.setColor(new Color(255, 255 - 28 * i, 255));
            Twig4(g, i - 1, p3x, p3y, p5x, p5y);
            Twig4(g, i - 1, p5x, p5y, p4x, p4y);
        }
    }

    public void Twig4main(Graphics g, int i, double p1x, double p1y, double p2x, double p2y) {
        double dx = (p2x - p1x);
        double dy = (p2y - p1y);
        double p3x = p2x - dx + dy;
        double p3y = p2y - dy - dx;
        double p4x = p1x + dx + dy;
        double p4y = p1y + dy - dx;
        dx = dx - dx / 5;
        dy = dy - dy / 5;
        double p5x = p3x + dx + .65 * dy * Math.sin(Math.toRadians(57));
        double p5y = p3y + dy - .65 * dx * Math.sin(Math.toRadians(57));

        g.drawLine((int) p1x, (int) p1y, (int) p3x, (int) p3y);
        g.drawLine((int) p1x, (int) p1y, (int) p2x, (int) p2y);
        g.drawLine((int) p2x, (int) p2y, (int) p4x, (int) p4y);
        g.drawLine((int) p3x, (int) p3y, (int) p4x, (int) p4y);
        g.drawLine((int) p3x, (int) p3y, (int) p5x, (int) p5y);
        g.drawLine((int) p5x, (int) p5y, (int) p4x, (int) p4y);

        if (i > 0) {
            g.setColor(Color.white);
            Twig4main(g, i - 1, p3x, p3y, p5x, p5y);
            Twig4(g, nivel_de_recursividade, p5x, p5y, p4x, p4y);
        }
    }

    public Fractal(int diametro) {
        setTitle("E-Fractal");
        setSize(1366, 745);
        getContentPane().setBackground(Color.black);
        setExtendedState(MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        L1 = new JLabel("Algorithm", JLabel.CENTER);

        String[] Algorithm = {"...", "Koch Snowflake", "Sierpinski Triangle", "Koch Snowflake and Sierpinski Triangle",
            "Fractal Tree", "Twig #1", "Twig #2", "Twig #3", "Twig #4"};
        C1 = new JComboBox(Algorithm);

        L2 = new JLabel("Nível de recursividade:", JLabel.CENTER);

        String[] Level = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        C2 = new JComboBox(Level);

        B1 = new JButton("Enter");
        B1.addActionListener(this);
        B1.setFont(new java.awt.Font("Lucida Sans", 0, 14));

        P2 = new JPanel();
        P2.setLayout(new GridLayout(4, 1));
        P2.add(L1);
        P2.add(C1);
        P2.add(L2);
        P2.add(C2);

        P3 = new JPanel();
        P3.setLayout(new BorderLayout());
        P3.add(B1, BorderLayout.CENTER);
        P3.add(Box.createVerticalStrut(40), BorderLayout.NORTH);
        P3.add(Box.createVerticalStrut(40), BorderLayout.SOUTH);
        P3.add(Box.createHorizontalStrut(20), BorderLayout.EAST);
        P3.add(Box.createHorizontalStrut(20), BorderLayout.WEST);

        P1 = new JPanel();
        P1.setLayout(new GridLayout(6, 1));
        P1.add(P2);
        P1.add(P3);

        getContentPane().add(P1, BorderLayout.WEST);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g.setColor(Color.white);
        g2d.setStroke(new BasicStroke(1.0f));

        switch (op) {
            case 1:
                Snowflake(g, nivel_de_recursividade, 1130, 560, 530, 560);
                break;
            case 2:
                Triangle(g, nivel_de_recursividade, 1167, 680, 450, 680);
                break;
            case 3:
                Snowflake_Triangle(g, nivel_de_recursividade, 1130, 560, 530, 560);
                break;
            case 4:
                FractalTree(g2d, nivel_de_recursividade + 1, 800, 730, 180);
                break;
            case 5:
                Twig1(g, nivel_de_recursividade, 800, 720, 1000, 720);
                break;
            case 6:
                Twig2(g, nivel_de_recursividade, 720, 720, 920, 720);
                break;
            case 7:
                Twig3R(g, nivel_de_recursividade, 720, 720, 900, 720);
                break;
            case 8:
                Twig4main(g, 45, 910, 570, 1010, 570);
                break;
        }
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == B1) {
            op = C1.getSelectedIndex();

            nivel_de_recursividade = Integer.parseInt((String) C2.getSelectedItem());
            repaint();
        }
    }
}


Nenhum comentário:

Postar um comentário