לדלג לתוכן

קובץ:Pascal's Triangle.png

תוכן הדף אינו נתמך בשפות אחרות.
מתוך ויקיפדיה, האנציקלופדיה החופשית

Pascal's_Triangle.png(371 × 368 פיקסלים, גודל הקובץ: 51 ק"ב, סוג MIME‏: image/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.
תאריך יצירה
מקור נוצר על־ידי מעלה היצירה
יוצר Philip Rideout

רישיון

אני, בעל זכויות היוצרים על עבודה זו, מפרסם בזאת את העבודה תחת הרישיון הבא:
w:he:Creative Commons
ייחוס שיתוף זהה
הקובץ הזה מתפרסם לפי תנאי רישיון קריאייטיב קומונז ייחוס-שיתוף זהה 3.0 לא מותאם.
הנכם רשאים:
  • לשתף – להעתיק, להפיץ ולהעביר את העבודה
  • לערבב בין עבודות – להתאים את העבודה
תחת התנאים הבאים:
  • ייחוס – יש לתת ייחוס הולם, לתת קישור לרישיון, ולציין אם נעשו שינויים. אפשר לעשות את זה בכל צורה סבירה, אבל לא בשום צורה שמשתמע ממנה שמעניק הרישיון תומך בך או בשימוש שלך.
  • שיתוף זהה – אם תיצרו רמיקס, תשנו, או תבנו על החומר, חובה עליכם להפיץ את התרומות שלך לפי תנאי רישיון זהה או תואם למקור.

Source Code

Uses 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;
}

כיתובים

נא להוסיף משפט שמסביר מה הקובץ מייצג

פריטים שמוצגים בקובץ הזה

מוצג

היסטוריית הקובץ

ניתן ללחוץ על תאריך/שעה כדי לראות את הקובץ כפי שנראה באותו זמן.

תאריך/שעהתמונה ממוזערתממדיםמשתמשהערה
נוכחית07:59, 16 באפריל 2012תמונה ממוזערת לגרסה מ־07:59, 16 באפריל 2012‪368 × 371‬ (51 ק"ב)Philiprideout

אין בוויקיפדיה דפים המשתמשים בקובץ זה.

מטא־נתונים