Militares de Lituania
These recursive programs written in languages Java, C and Caml generates Tapis of Sierpinski.
The TapisSierpinsky function written in Caml not only enables you to obtain a carpet of Sierpinsky, but also to color the various squares according to their size by constantly redefining the color according to parameters such as the length (lng) or the height (htr).
let rec TapisSierpinsky X there lng htr =
yew lng > 2 && htr > 2 then begin
let nlng = lng/3 and nhtr = htr/3 in
set_color (rgb (9 * lng) (3 * lng) htr);
fill_rect (X + nlng) (there + nhtr) nlng nhtr;
for K = 0 to 8 C
yew K <> 4 then let I = K/3 and J = K MOD 3 in
TapisSierpinsky (X + I * nlng) (there + J * nhtr) nlng nhtr
give;
end; ;
The Java program is a Applet, of which the method drawSierpinskiCarpet is recursive:
importation java.awt.*;
importation java.applet.*;
public class SierpinskiCarpet extends Applet {
private Graphics g=null;
private int d0=729; // 3^6
public void init () {
g=getGraphics ();
resize (d0, d0);
}
public void Paint (Graphics G) {
// start recursion:
drawSierpinskiCarpet (0,0, getWidth (), getHeight ());
}
private void drawSierpinskiCarpet (int xHG, int yHG, int width, int height) {
yew (largeur>2 && hauteur>2) {
int w=largeur/3, h=hauteur/3;
g.fillRect (xHG+w, yHG+h, W, H);
for (int k=0; k<9; k++) yew (K! =4) {
int i=k/3, j=k%3;
drawSierpinskiCarpet (xHG+i*w, yHG+j*h, W, H); // recursion
}
}
}
}
For the program in language C one proceeds differently that in the program written for the triangles of Sierpiński. Instead of drawing a form each time and to remove a piece of it, one removes pieces with only one form drawn once for all; i.e. one “gruyèrise” a rectangle.
/* to compile to type in line of order: GCC tapis.c - O carpet `allegro-config --libs ` to carry out to type carpet followed by the iteration count: carpet 7 *
include
- include
- include “allegro.h”
define MAXX 768
- define MAXY 665
- define OX 5
- define OY 5
- define TX 758
- define TY 655
BITMAP *bmp;
/* recursive function, which has as parameters the coordinates (X, there) of one of the tops of a rectangle, with the length and B the width of this rectangle and, N the iteration count *
void tapis_Sierpinski (double X, doubles, double has there, double B, int N) { yew (n>0) { /* one removes the small central rectangle * rectfill (bmp, (int) (x+a/3), (int) (y+b/3), (int) (x+2*a/3), (int) (y+2*b/3), 8); /* recursive calls for the eight remaining rectangles * tapis_Sierpinski (X, there, A/3, B/3, n-1); tapis_Sierpinski (x+a/3, there, A/3, B/3, n-1); tapis_Sierpinski (x+2*a/3, there, A/3, B/3, n-1); tapis_Sierpinski (X, y+b/3, A/3, B/3, n-1); tapis_Sierpinski (x+2*a/3, y+b/3, A/3, B/3, n-1); tapis_Sierpinski (X, y+2*b/3, A/3, B/3, n-1); tapis_Sierpinski (x+a/3, y+2*b/3, A/3, B/3, n-1); tapis_Sierpinski (x+2*a/3, y+2*b/3, A/3, B/3, n-1); } else /* one draws a full rectangle * rectfill (bmp, (int) X, (int) there, (int) (x+a), (int) (y+b), 1); }
int hand (int argc, tank *argv) { unsigned long n=1; yew (argc>1) n=strtoul (argv, NO ONE, 10); /* initialization of allegro * allegro_init (); set_color_depth (8);
bmp = create_bitmap (MAXX, MAXY); yew (! bmp) { allegro_message (" Cannot create the image \ n"); return 1; } tapis_Sierpinski (OX, OY, TX, TY, (int) N); save_bitmap (" Tapis_de_Sierpinski.pcx" , bmp, desktop_palette); destroy_bitmap (bmp); return 0; }
END_OF_MAIN (); /* final note of the programs allegro *
| Random links: | Stenodactylus doriae | Envermeu | Hinds | Tournament of Stuttgart | Shafik Ahmad | Militaires_de_la_Lithuanie |