0% found this document useful (0 votes)
35 views6 pages

Trinangulo de Sierpinski

This document contains code for drawing two fractal shapes: the Sierpinski triangle and the Hilbert curve. For the Sierpinski triangle, it defines a Lienzo2 class with methods to recursively draw the triangle at decreasing sizes. For the Hilbert curve, it defines a Lienzo3 class with a recursive dibujar method to draw lines that form the curve shape at different levels of recursion. Both shapes are displayed using Applet classes that initialize the canvas objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views6 pages

Trinangulo de Sierpinski

This document contains code for drawing two fractal shapes: the Sierpinski triangle and the Hilbert curve. For the Sierpinski triangle, it defines a Lienzo2 class with methods to recursively draw the triangle at decreasing sizes. For the Hilbert curve, it defines a Lienzo3 class with a recursive dibujar method to draw lines that form the curve shape at different levels of recursion. Both shapes are displayed using Applet classes that initialize the canvas objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Trinangulo De Sierpinski

package lienzo;

import java.awt.Canvas;

import java.awt.Graphics;

public class Lienzo2 extends Canvas {

private double xp1;

private double yp1;

private double xp2;

private double yp2;

private double sin60=Math.sin(3.14/3.);

public Lienzo2(double x1, double y1, double x2, double y2) {

this.xp1 = x1;

this.yp1 = y1;

this.xp2 = x2;

this.yp2 = y2;

this.setSize((int)xp1, (int)yp1);

public void paint(Graphics g) {

paintRecursivo(g, 6, xp1, yp1, xp2, yp2);

}
private void paintRecursivo(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 (i <= 0) {

g.drawLine((int) xp12, (int) yp12, (int) xp22, (int)


yp22);

g.drawLine((int) xp22, (int) yp22, (int) xp32, (int)


yp32);

g.drawLine((int) xp32, (int) yp32, (int) xp12, (int)


yp12);

} else {

paintRecursivo(g, i - 1, xp12, yp12, dx1, dy1);

paintRecursivo(g, i - 1, dx1, dy1, xp22, yp22);

paintRecursivo(g, i - 1, dx3, dy3, dx2, dy2);

}
}

package lienzo;

import java.applet.Applet;

public class TrianguloSierpinski extends Applet {

private Lienzo2 lz;


public void init() {
lz = new Lienzo2(500,500,50,500);
this.add(lz);
}

}
Curva de Hilbert
package lienzo;

import java.awt.Canvas;
import java.awt.Graphics;
public class Lienzo3 extends Canvas {

private int n,m;

public Lienzo3(int n,int m){


this.n = n;
this.m = n;

this.setSize(n,m);
}

public void paint(Graphics g) {

dibujar(g,this.getWidth()/2,this.getHeight()/2,4,this.getHeight()/2);
}

private void dibujar(Graphics g,int x, int y, int nivel, int size)


{

if(nivel == 0) return;

int x0 = x - size / 2;
int y0 = y - size / 2;
int x1 = x + size / 2;
int y1 = y + size / 2;

g.drawLine(x0, y0, x0, y1);


g.drawLine(x1, y0, x1, y1);
g.drawLine(x0, y, x1, y);

dibujar(g, x0, y0, nivel-1, size / 2);


dibujar(g, x0, y1, nivel-1, size / 2);
dibujar(g, x1, y0, nivel-1, size / 2);
dibujar(g, x1, y1, nivel-1, size / 2);

}
}
package lienzo;

import java.applet.Applet;

public class CurvaHilbert extends Applet {


private Lienzo3 lz;
public void init() {
lz = new Lienzo3(400,400);
this.add(lz);
}

You might also like