Encyclopedia > Image:Sierpinski triangle

  Article Content

Image:Sierpinski triangle

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.



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
Sanskrit language

... and a few others besides); in a tatpurusha, one component is related to another. For example, a doghouse is a dative compound, a house for a dog. It would be called a ...

 
 
 
This page was created in 41.9 ms