File:Pascal's Triangle.png
Pascal's_Triangle.png (371 × 368 pixels, file size: 51 KB, MIME type: image/png)
File information
Structured data
Captions
Summary
editDescriptionPascal's Triangle.png |
English: Odd numbers are highlighted to show how this is related to the Sierpinski fractal.
To generate Pascal's triangle, write it out as a sequence of rows. Each number is the sum of two other numbers: the number directly above, and the number above and to the left. Each subsequent row represents an improved approximation of the Gaussian Function. |
Date | |
Source | Own work |
Author | Philip Rideout |
Licensing
editI, the copyright holder of this work, hereby publish it under the following license:
This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license.
- You are free:
- to share – to copy, distribute and transmit the work
- to remix – to adapt the work
- Under the following conditions:
- attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
- share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
Source Code
editUses C99 and Cairo.
#include <cairo.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
cairo_t *crc;
void number(int n, double x, double y)
{
cairo_text_extents_t extents;
char str[32];
const double radius = 15;
double theta;
sprintf(str, "%d", n);
if (n > 999)
cairo_set_font_size(crc, 10);
else if (n > 99)
cairo_set_font_size(crc, 12);
else if (n > 9)
cairo_set_font_size(crc, 14);
else
cairo_set_font_size(crc, 16);
cairo_set_source_rgb(crc, 0, 0, 0);
cairo_move_to(crc, x + radius, y);
for (theta = 0; theta < M_PI * 2; theta += 0.1)
{
double cx = x + radius * cos(theta);
double cy = y + radius * sin(theta);
cairo_line_to(crc, cx, cy);
}
cairo_close_path(crc);
if (n % 2)
{
cairo_stroke_preserve(crc);
cairo_set_source_rgb(crc, 1, 1, 1);
cairo_fill(crc);
}
else
cairo_fill(crc);
if (n % 2)
cairo_set_source_rgb(crc, 0, 0, 0);
else
cairo_set_source_rgb(crc, 1, 1, 1);
cairo_text_extents(crc, str, &extents);
cairo_move_to(crc, x - extents.width / 2, y + extents.height / 2);
cairo_show_text(crc, str);
}
int main()
{
double width = 600, height = 510;
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
crc = cairo_create(surface);
cairo_font_options_t* font;
int *previous;
int *current;
int row, col, count;
double x, y;
// Set up font (face is second field in 'xlsfonts'; first field is foundry)
font = cairo_font_options_create();
cairo_font_options_set_antialias(font, CAIRO_ANTIALIAS_SUBPIXEL);
cairo_set_font_options(crc, font);
cairo_select_font_face(crc, "nimbus sans l", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_font_options_destroy(font);
// Draw first two rows manually.
y = 30;
number(1, 30, y);
y += 30;
number(1, 30, y);
number(1, 30 + 30, y);
y += 30;
// Continue with pascal's triangle.
count = 2;
previous = malloc(count++ * sizeof(int));
previous[0] = 1;
previous[1] = 1;
for (row = 2; row < 16; row++, count++, y+= 30)
{
x = 30;
current = malloc(count * sizeof(int));
for (col = 0; col < count; col++)
{
if (col == 0 || col == count - 1)
current[col] = 1;
else
current[col] = previous[col] + previous[col - 1];
number(current[col], x, y);
x += 30;
}
free(previous);
previous = current;
}
free(previous);
// Done.
cairo_surface_write_to_png(surface, "pascal.png");
return 0;
}
File history
Click on a date/time to view the file as it appeared at that time.
Date/Time | Thumbnail | Dimensions | User | Comment | |
---|---|---|---|---|---|
current | 04:59, 16 April 2012 | 371 × 368 (51 KB) | Philiprideout (talk | contribs) |
You cannot overwrite this file.
File usage on Commons
The following page uses this file:
Metadata
This file contains additional information such as Exif metadata which may have been added by the digital camera, scanner, or software program used to create or digitize it. If the file has been modified from its original state, some details such as the timestamp may not fully reflect those of the original file. The timestamp is only as accurate as the clock in the camera, and it may be completely wrong.
File change date and time | 21:23, 19 February 2007 |
---|---|
Horizontal resolution | 28.34 dpc |
Vertical resolution | 28.34 dpc |