Curba lui Koch

/*    Simple fractal program that illustrates recursion.    Recall that the vector <-y,x> is perpendicular to the vector .    Check the dot product.  */ import java.awt.*; import java.applet.*; public class Fractal extends Applet {   public void init()   {            resize(600,400);   }        Point addPoints(Point one, Point two)   {       return new Point (one.x+two.x,one.y+two.y);   }      Point up(Point one, Point two, double scale)   {        int x = (int) (scale*(two.x-one.x));        int y = (int) (scale*(two.y-one.y));        return addPoints(one, new Point(x,y));   }      Point rotate(Point one,Point two)   {       Point p = up(one,two, 1.0/2);       int y = (int)((one.x – two.x)/4.0 );       int x = (int) ((two.y- one.y)/4.0);       return addPoints( p, new Point(x,y));   }             public void drawFractal(Point b,Point e, int d, Graphics g)   {       if (d == 0) g.drawLine(b.x,b.y,e.x,e.y);         else        {           Point next = up(b,e,1.0/4);           Point mid = rotate(b,e);           Point last = up(b,e,3.0/4);           drawFractal(b,next, d-1,g);           drawFractal(next,mid,d-1,g);           drawFractal(mid,last,d-1,g);           drawFractal(last,e,d-1,g);         }   }      public void paint(Graphics g)   {     g.setColor(Color.blue);     Point begin = new Point(100,200);     Point end = new Point(500,200);     int depth = 4;     // How deep is too deep?     drawFractal(begin,end,depth, g );…

Arbore Fractal

  import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame;   public class FractalTree extends JFrame {       public FractalTree() {         super(“Fractal Tree”);         setBounds(100, 100, 800, 600);         setResizable(false);         setDefaultCloseOperation(EXIT_ON_CLOSE);     }       private void drawTree(Graphics g, int x1, int y1, double angle, int depth) {         if (depth == 0) return;         int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0);         int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0);         g.drawLine(x1, y1, x2, y2);         drawTree(g, x2, y2, angle – 20, depth – 1);         drawTree(g, x2, y2, angle + 20, depth – 1);     }       @Override     public void paint(Graphics g) {         g.setColor(Color.BLACK);         drawTree(g, 400, 500, -90, 9);     }       public static void main(String[] args) {         new FractalTree().setVisible(true);     } }   Rezultat:

Curba Dragonului

Se trasează (începe) cu un segment de dreaptă. La prima iterație, se înlocuiește acest segment cu alte două segmente, în așa fel încât segmentul original să fie ipotenuza unui triunghi isoscel. De-a lungul segmentului original, trasăm (punem) cele două segmente noi spre stânga. La a doua iterație, înlocuim fiecare dintre segmente cu două noi segmente la…

Feriga Barnsley

  // FernApplet // by Doug Babcock // June – July 2004 import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import javax.swing.*; import java.util.Random; /**  * A <code>Fern</code> object creates a window for drawing the Barnsley fern.  * The window contains: a drop-down box to select the color scheme to use when  * drawing the fern; a button to start and stop drawing of the fern; and an  * area to actually draw the fern in.  *  * @author Doug Babcock  */ public class FernApplet extends JApplet implements ActionListener {     /** The text to appear on the button when drawing is not in progress. */     private static String BUTTON_TEXT_OFF = “Draw Fern”;          /** The text to appear on the button when drawing is in progress. */     private static String BUTTON_TEXT_ON = “Stop”;          /** Which color scheme to use (set from the comboBox) */     private static int colorScheme = 0;          /** A reference to the JButton that draws (and stops drawing) the fern. */     JButton drawButton = null;          /** A reference to the JComboBox that allows the user to select a color scheme. */     JComboBox colorChoices = null;          /** A reference to the FernArea, the area in which the fern is drawn. */     private FernArea myFernArea = null;          /** A reference to a FernDraw, a Thread which draws the fern. */     private FernDraw myFernDraw = null;          public void init() {         JFrame.setDefaultLookAndFeelDecorated(true);                  JPanel mainPane = new JPanel();         mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));                  // Make the combo box and have the Fern listen to it.         String[] colors = {“Green”, “Gradient”, “Crystal”, “Psycho”};         colorChoices = new JComboBox(colors);         colorChoices.setSelectedIndex(0);         colorChoices.addActionListener(this);                  // Set the combo box on a JPanel, give it a label, and set its max size.         JPanel comboBoxPanel = new JPanel();         comboBoxPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));         comboBoxPanel.add(new JLabel(“Color Scheme:”));…

Fulgul Koch

Fulgul de zăpadă al lui Koch se obține pornind de la un triunghi echilateral și se înlocuiește treimea din mijloc de pe fiecare latură cu două segmente astfel încât să se formeze un nou triunghi echilateral exterior. Apoi se execută aceiași pași pe fiecare segment de linie a formei rezultate, la infinit. La fiecare iterație,…

Fractalul Mandelbrot

Mulțimea Mandelbrot este o mulțime de numere complexe c, astfel încât dacă iterăm funcția z=z^2+c, aceasta nu merge la infinit (începând cu z=0). Mulțimea Mandelbrot se calculează astfel: – pentru fiecare pixel c, începând cu z=0, se repetă z=z^2+c de N ori – aceasta va eșua dacă magnitudinea lui z devine tot mai mare –…

Covorul lui Sierpinski

Covorul lui Sierpinski este un fractal plan, descoperit de matematicianul polonez Waclaw Sierpinski în 1916. Figura se obţine împărţind un pătrat în 9 pătrate mai mici, dispuse sub forma unei matrice 3 x 3, din care se scoate pătratul central. Procedeul se repetă în cele opt pătrate rămase, ad infinitum. Implementare: import java.applet.*; import java.awt.*; import java.util.Random; class SierpPlot extends Canvas  {…

Triunghiul lui Sierpinski

Probabil cel mai cunoscut fractal al tuturor timpurilor este așa numitul triunghi al lui Sierpinski. În 1915, Waclaw Sierpinski(1882-1969. Modul de realizare al acestui fractal este foarte simplu: la început se desenează un triunghi pe care îl vom diviza în patru părți egale, iar trei dintre ele (cele din exterior) vor fi si ele divizate…