~ Office Supplies ~~ Buy Posters ~~ A-Z Products ~~ Website Advertising


Image:Sierpinski triangle - Wikipedia

<<Up     Contents

Image:Sierpinski triangle

Redirected from Sierpinski triangle/Image credits

The following C program generates a Sierpinski triangle and a Sierpinski carpet. It uses the free Allegro library for graphic operations.

/*

Copyright 2002 Damian Yerrick

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.

*/

#include <allegro.h>

const char init_siertri[6][6] =
{
  {0, 0, 1, 1, 0, 0},
  {0, 0, 2, 2, 0, 0},
  {0, 1, 2, 2, 1, 0},
  {0, 1, 0, 0, 1, 0},
  {1, 2, 1, 1, 2, 1},
  {2, 2, 2, 2, 2, 2}
};

const PALETTE pal_3gray =
{
  {63, 63, 63},
  {32, 32, 32},
  {0, 0, 0}
};

const PALETTE pal_bw =
{
  {63, 63, 63},
  {0, 0, 0}
};

int main(void)
{
  int x, y;
  BITMAP *bmp;

  install_allegro(SYSTEM_NONE, &errno, atexit);
  set_color_depth(8);

  bmp = create_bitmap(768, 768);
  if(!bmp)
  {
    allegro_message("Could not create bitmap\n");
    return 1;
  }

  /* set initial sierpinski triangle */
  for(y = 0; y < 6; y++)
    for(x = 0; x < 6; x++)
      putpixel(bmp, x, y, init_siertri[y][x]);

  for(x = 0; x < 7; x++)
  {
    unsigned int scale = 3 << x;

    /* create bottom row */
    blit(bmp, bmp, 0, 0, 0, 2*scale, 2*scale, 2*scale);
    blit(bmp, bmp, 0, 0, 2*scale, 2*scale, 2*scale, 2*scale);
    /* remove top row */
    rectfill(bmp, 0, 0, 4*scale-1, 2*scale-1, 0);
    /* create new top row */
    blit(bmp, bmp, 2*scale, 2*scale, scale, 0, 2*scale, 2*scale);
  }

  save_bitmap("sierpinski-triangle.pcx", bmp, pal_3gray);
  destroy_bitmap(bmp);

  bmp = create_bitmap(729, 729);
  if(!bmp)
  {
    allegro_message("Could not create bitmap\n");
    return 1;
  }

  /* set initial sierpinski carpet */
  rectfill(bmp, 0, 0, 2, 2, 1);
  putpixel(bmp, 1, 1, 0);

  for(x = 3; x < 729; x *= 3)
  {
    /* create top row */
    blit(bmp, bmp, 0, 0, x, 0, x, x);
    blit(bmp, bmp, 0, 0, 2*x, 0, x, x);
    /* create middle row */
    blit(bmp, bmp, 0, 0, 0, x, x, x);
    rectfill(bmp, x, x, 2*x-1, 2*x-1, 0);
    blit(bmp, bmp, 0, 0, 2*x, x, x, x);
    /* create bottom row */
    blit(bmp, bmp, 0, 0, 0, 2*x, 3*x, x);
  }

  save_bitmap("sierpinski-carpet.pcx", bmp, pal_bw);
  destroy_bitmap(bmp);

  return 0;
}

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"}'

Image links

There are no pages that link to this image.

wikipedia.org dumped 2003-03-17 with terodump




 
 
13 ct pink red gemmy banded RHODOCHROSITE Gorgeous gemstone tongue freeform 14 mm Single gem piece
 13 ct pink red my banded RHODOCHROSITE Gorgeous tongue freeform 14 mm Single piece 
 
7 carat RARE yellow orange CLINOHUMITE gem stone crystal cabbing lapidary cabochon rough 1 gram C
 7 carat RARE yellow orange CLINOHUMITE crystal cabbing lapidary cabochon 1 gram C 
 
14 gram red blue gold PIETERSITE gem stone Tumbled cab cabbing rough raw gemstone 73 carat PRETTY
 14 gram red blue gold PIETERSITE Tumbled cab cabbing raw 73 carat PRETTY 
 
50 cts Neon Blue Green APATITES gem stones Jewelry rough tumbled polished gemstones lots 10 grams
 50 cts Neon Blue Green APATITES Jewelry tumbled polished lots 10 grams 
 
25 carats CHRYSOBERYL gems stones Facet uncut raw rough gemstones crystals lot 1 to 2 ct jewels Nice
 25 carats CHRYSOBERYL uncut raw crystals lot 1 to 2 ct jewels Nice