File:Pi archi approx.svg

From Wikiquote
Jump to navigation Jump to search

Original file(SVG file, nominally 1,000 × 180 pixels, file size: 6 KB)

This file is from Wikimedia Commons and may be used by other projects. The description on its file description page there is shown below.

Summary

Description
English: Approximation to Pi by Archimedes' method
Français : Approximation de Pi par la méthode d'Archimède
Polski: Metoda Archimedesa aproksymacji Pi
Español: Método de aproximación del número π de Arquímedes.
Date
Source Own work
Author Guillaume Jacquenot
Other versions

Derivative works of this file:  Pi archi approx inter.svg

Archimedes_pi.svg

Licensing

I, the copyright holder of this work, hereby publish it under the following licenses:
GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License.
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported, 2.5 Generic, 2.0 Generic and 1.0 Generic 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.
You may select the license of your choice.
 
W3C-validity not checked.

Source code (C)

/* Compilation  instructions */
/* gcc -Wall -ansi -pedantic -o Pi_archi_approx Pi_archi_approx.c */

/* This program generates an SVG image, showing Archimede's technique to */
/* approximate pi */
/* One argument can be provided, as the maximum number of approximations desired */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define PI 3.14159265358979

int main(int argc, char **argv)
{
    int i,j,n;
    double id,coeff;
    FILE *fpt;
    double *x, *y;
    double R = 40.0;
    /* Angle values*/
    double alpha, alpha_offset;

    int    fontsize    = 10;
    double strokewidth = 0.8;
    if (argc == 2)
        n = (int)atof(argv[1]);
    else
    {
        n = 10;
    }
    if (n<3)
    {
        printf("\n");
        fflush(stdout);
        exit(1);
    }
    /* Opening result file*/
    fpt = fopen("Pi_archi_approx.svg","w");
    fprintf(fpt,"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
    fprintf(fpt,"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd \">\n");
    fprintf(fpt,"<svg\n");
    fprintf(fpt,"   xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" \n");
    fprintf(fpt,"   xmlns:svg=\"http://www.w3.org/2000/svg\" \n");
    fprintf(fpt,"   xmlns=\"http://www.w3.org/2000/svg\" \n");
    fprintf(fpt,"   width=\"%dpx\" \n",(int)(((n-2)*3+1)*R));
    fprintf(fpt,"   height=\"%dpx\" \n",(int)(4.5*R));
    fprintf(fpt,"   >\n");
    for (i=3;i<=n;i++)
    {
        id = (double) i;
        x  = (double *)malloc(i*sizeof(double));
        y  = (double *)malloc(i*sizeof(double));
        fprintf(fpt,"   <g>\n");
        fprintf(fpt,"    <circle cx=\"%f\" cy=\"%f\" r=\"%f\" ",((id-2)*3-0.75)*R,2.5*R,R);
        fprintf(fpt,"stroke=\"black\" stroke-width=\"%f\" fill=\"none\"/>\n",strokewidth);
        fprintf(fpt,"    <polygon points=\"");
        alpha_offset = -PI/2 + ((i%2)==0) * PI/id;
        for (j=0;j<i;j++)
        {
            alpha = alpha_offset + 2*j*PI/id;
            x[j]  = cos(alpha);
            y[j]  = sin(alpha);
            fprintf(fpt,"%f,%f ",R*x[j]+((id-2)*3-0.75)*R,R*(y[j]+2.5));
        }
        fprintf(fpt,"\" fill=\"none\" stroke=\"black\" stroke-width=\"%f\"/>\n",strokewidth);
        coeff = 2.0/sqrt((x[0]+x[1])*(x[0]+x[1]) + (y[0]+y[1])*(y[0]+y[1]));
        fprintf(fpt,"    <polygon points=\"");
        for (j=0;j<i;j++)
        {
            fprintf(fpt,"%f,%f ",R*coeff*x[j]+((id-2)*3-0.75)*R,R*(coeff*y[j]+2.5));
        }
        fprintf(fpt,"\" fill=\"none\" stroke=\"black\" stroke-width=\"%f\"/>\n",strokewidth);
        free(x);
        free(y);
        fprintf(fpt,"    <text x = \"%f\" y = \"%f\" text-anchor=\"middle\" fill = \"black\" font-size = \"%d\">\n",
                    ((id-2)*3-0.75)*R,4*R,fontsize);
        fprintf(fpt,"      n = %d\n",i);
        fprintf(fpt,"    </text>\n");
        fprintf(fpt,"  </g>\n");
    }
    fprintf(fpt,"</svg>\n");
    fclose(fpt);
    return (0);
}

Captions

Add a one-line explanation of what this file represents

Items portrayed in this file

depicts

22 March 2009

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current12:21, 22 March 2009Thumbnail for version as of 12:21, 22 March 20091,000 × 180 (6 KB)Gjacquenot{{Information |Description={{en|1=Archimedes' Pi aproximation}} {{fr|1=Approximation de pi par la méthode d'Archimède}} |Source=travail personnel (own work) |Author=Guillaume Jacquenot |Date=2009-03-22 |Permission= |other_versions=fi

Global file usage

The following other wikis use this file: