Sierpinski triangle
The following C program generates a Sierpinski triangle and a Sierpinski carpet. It uses the free Allegro library for graphic operations.
\r\n/*\r\n\r\nCopyright 2002 Damian Yerrick\r\n\r\nPermission is hereby granted, free of charge, to any person obtaining\r\na copy of this software and associated documentation files (the\r\n"Software"), to deal in the Software without restriction, including\r\nwithout limitation the rights to use, copy, modify, merge, publish,\r\ndistribute, sublicense, and/or sell copies of the Software, and to\r\npermit persons to whom the Software is furnished to do so, subject to\r\nthe following conditions:\r\n\r\nThe above copyright notice and this permission notice shall be\r\nincluded in all copies or substantial portions of the Software.\r\n\r\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\r\nOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\r\nBE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN\r\nAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF\r\nOR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\r\nIN THE SOFTWARE.\r\n\r\n*/\r\n\r\n/* To build this code, you need to install the Allegro library\r\n and then do one of the following:\r\n\r\nCompilation on Microsoft targets such as MinGW:\r\n gcc -Wall -O ascii.c -lalleg -o ascii.exe\r\nCompilation on POSIX targets such as Linux:\r\n gcc -Wall -O ascii.c `allegro-config --libs` -o ascii\r\n\r\n*/\r\n\r\n#include\r\n\r\nconst char init_siertri[6][6] =\r\n{\r\n {0, 0, 1, 1, 0, 0},\r\n {0, 0, 2, 2, 0, 0},\r\n {0, 1, 2, 2, 1, 0},\r\n {0, 1, 0, 0, 1, 0},\r\n {1, 2, 1, 1, 2, 1},\r\n {2, 2, 2, 2, 2, 2}\r\n};\r\n\r\nconst PALETTE pal_3gray =\r\n{\r\n {63, 63, 63},\r\n {32, 32, 32},\r\n {0, 0, 0}\r\n};\r\n\r\nconst PALETTE pal_bw =\r\n{\r\n {63, 63, 63},\r\n {0, 0, 0}\r\n};\r\n\r\nint main(void)\r\n{\r\n int x, y;\r\n BITMAP *bmp;\r\n\r\n install_allegro(SYSTEM_NONE, &errno, atexit);\r\n set_color_depth(8);\r\n\r\n bmp = create_bitmap(768, 768);\r\n if(!bmp)\r\n {\r\n allegro_message("Could not create bitmap\\n");\r\n return 1;\r\n }\r\n\r\n /* set initial sierpinski triangle */\r\n for(y = 0; y < 6; y++)\r\n for(x = 0; x < 6; x++)\r\n putpixel(bmp, x, y, init_siertri[y][x]);\r\n\r\n for(x = 0; x < 7; x++)\r\n {\r\n unsigned int scale = 3 << x;\r\n\r\n /* create bottom row */\r\n blit(bmp, bmp, 0, 0, 0, 2*scale, 2*scale, 2*scale);\r\n blit(bmp, bmp, 0, 0, 2*scale, 2*scale, 2*scale, 2*scale);\r\n /* remove top row */\r\n rectfill(bmp, 0, 0, 4*scale-1, 2*scale-1, 0);\r\n /* create new top row */\r\n blit(bmp, bmp, 2*scale, 2*scale, scale, 0, 2*scale, 2*scale);\r\n }\r\n\r\n save_bitmap("sierpinski-triangle.pcx", bmp, pal_3gray);\r\n destroy_bitmap(bmp);\r\n\r\n bmp = create_bitmap(729, 729);\r\n if(!bmp)\r\n {\r\n allegro_message("Could not create bitmap\\n");\r\n return 1;\r\n }\r\n\r\n /* set initial sierpinski carpet */\r\n rectfill(bmp, 0, 0, 2, 2, 1);\r\n putpixel(bmp, 1, 1, 0);\r\n\r\n for(x = 3; x < 729; x *= 3)\r\n {\r\n /* create top row */\r\n blit(bmp, bmp, 0, 0, x, 0, x, x);\r\n blit(bmp, bmp, 0, 0, 2*x, 0, x, x);\r\n /* create middle row */\r\n blit(bmp, bmp, 0, 0, 0, x, x, x);\r\n rectfill(bmp, x, x, 2*x-1, 2*x-1, 0);\r\n blit(bmp, bmp, 0, 0, 2*x, x, x, x);\r\n /* create bottom row */\r\n blit(bmp, bmp, 0, 0, 0, 2*x, 3*x, x);\r\n }\r\n\r\n save_bitmap("sierpinski-carpet.pcx", bmp, pal_bw);\r\n destroy_bitmap(bmp);\r\n\r\n return 0;\r\n} END_OF_MAIN();\r\n
Here is a text-mode Perl version of the triangle only (Copying rights as above):
perl -e 'for $y(0..15) {print " " x (1 + $y); for $x (0..15) { print $x & $y ? " " : "\\\\7";} print "\\n"}'
