Third Anniversary

Today marks this blog’s third anniversary. To celebrate and take a look back at the year, I have collected a few image highlights.

Multibrot Set
Multibrot Set
Pinhole Photographs MMXVII
Pinhole Photographs MMXVII
Asciify
Asciify
Mandelbrot Set III
Mandelbrot Set III
Rainbowify
Rainbowify
BMP Implementation in C
BMP Implementation in C
Animating the Quantum Drunkard’s Walk
Animating the Quantum Drunkard’s Walk
Christmas MMXVII
Christmas MMXVII
Lyapunov Fractal
Lyapunov Fractal

17500615947440398742637684298448259300459653195179624088723406481656498345927782897306957959023081425157582777952426879442535942327333206022815634243070984075006080698433225695442819778347008.0

Advertisements

BMP Implementation in C — Graphic Primitives

Continuing development of my C bitmap library, I added basic graphic primitives to augment the library’s functionality beyond simply reading and writing bitmaps and manually manipulating individual pixels. Source code can be seen below and also downloaded — bmp.c.
The underlying implementation of the bitmap file format can be seen in my prior post BMP Implementation in C.
Graphic primitives include drawing lines, rectangles, circles, ellipses; rotating, flipping, cropping, resizing and blitting images. A full list of defined graphic primitives can be seen below, together with a short functionality description.

drw
Test image regarding drawing primitives.
/* === DRAWING PRIMITIVES === */
void hline      (image *img, int x0, int x1, int y , int c               ); // draw horizontal line
void vline      (image *img, int x , int y0, int y1, int c               ); // draw vertical line
void line       (image *img, int x0, int y0, int x1, int y1, int c       ); // draw line
void fillrect   (image *img, int x0, int y0, int x1, int y1, int c       ); // draw filled rectangle
void rect       (image *img, int x0, int y0, int x1, int y1, int c       ); // draw rectangle
void fillcircle (image *img, int x , int y , int r , int c               ); // draw filled circle
void circle     (image *img, int x , int y , int r , int t , int c       ); // draw circle (with certain thickness)
void fillellipse(image *img, int x , int y , int rx, int ry, int c       ); // draw filled ellipse
void ellipse    (image *img, int x , int y , int rx, int ry, int t, int c); // draw ellipse (with certain thickness)

/* === TRANSFORMATION PRIMITIVES === */
image *resize   (image *img, int w , int h                                ); // resize an image
image *hflip    (image *img                                               ); // flip horizontally
image *vflip    (image *img                                               ); // flip vertically
image *rrotate  (image *img                                               ); // rotate clockwise
image *lrotate  (image *img                                               ); // rotate counter-clockwise
image *hrotate  (image *img                                               ); // rotate half a revolution
image *crop     (image *img, int x0, int y0, int x1, int y1               ); // crop an image
void   blit     (image *img, image*, int x , int y                        ); // blit an image onto another one
flp
Test image regarding transformation primitives.

Future plans for this library include performance optimizations regarding the ellipse drawing primitives; circle drawing is already optimized as it uses the shape’s symmetry to save computational cost.
Further primitives that may be added include a flood filling functionality as well as the ability to draw irregular polygons.


/* ================================================== *
 *                GENERAL INFORMATION                 *
 * ================================================== *
 * This C program implements functions for handling   *
 * 24-bit images, drawing and parts of the bitmap     *
 * (.bmp) file format. Supported color models include *
 * rgb hsl. There are also functions for mandelbrot   *
 * set fractal rendering.                             *
 *                                                    *
 * Written by Jonathan Frech.                         *
 *                                                    *
 * Edit history:                                      *
 * 23rd, 24th, 27th, 28th, 29th, 30th of June,        *
 * 1st, 2nd, 3rd, 10th, 11th, 13th, 14th, 15th, 16th, *
 * 17th, 18th, 19th, 20th, 25th, 26th, 27th, 29th of  *
 * July 2017, 21st, 22nd, 23rd of March 2018          */

Continue reading

Pi Day MMXVIII

Today it is the fourteenth of March 2018. Today’s date — when written in the M/D/Y format –, 3/14/18, looks close enough to Archimedes’ constant’s decimal representation for it to be the constant’s celebratory day.
As always on Pi Day, I have implemented an algorithm to generate \pi, albeit this year’s accuracy is not the greatest (Try it online).

                        typedef double d;typedef long l;l f(l n          
                   ){l f=1;while(n>1)f*=n--;return f;}d ne(d v,          
                 l p){d r=1;for(l k=0;k<p;k++)r*=v;return r;}d           
                ps(d(*c)(l),l i,d x){d s=0;for(l k=0;k<i;k++)s           
               +=c(k)*       ne(x,        k);return                      
              s;}           d exc         (     l                        
             n){            return       1./f (n)                        
                           ; } d         exp(d x                         
                          )   {         return                           
                         ps(exc        ,20,x);}                          
                        d G( d         x){return                         
                        exp(-x        *x);}d I                           
                       (d a,d         b,d g,d                            
                     (* f)(d         )){d cs=                            
                    0;for( d         x=a;x<=                             
                   b;x +=g)         cs+=f(x)                             
                 *g;return          cs ;  }          int                 
               main( ) { d          pi_root         =I(                  
              -2.5, 2.5 ,           1e-4,G);      d pi                   
             = pi_root *            pi_root+(0xf&0xf0                    
             ) ; printf(             "%c%c%c%c%c%f%c"                    
             ,'p','i',                ' ','=',' ',pi                     
               ,'\n'                     ) ; }                           

I use various methods of generating \pi throughout the Pi Days; this time I chose to use an improper integral paired with a power series. \pi is calculated using a famous identity involving infinite continuous sums, roots, e, statistics and — of course — \pi.

\int\limits_{-\infty}^\infty e^{-x^2}\mathrm{d}x = \sqrt{\pi}

Furthermore, to compute e, the following identity is used.

\exp{x} = \sum\limits_{n=0}^\infty\frac{x^n}{n!}

Both formulae are combined, the approximated value of \sqrt{\pi} is squared and \pi is printed to stdout.

You can download this program’s prettified (some call it obfuscated, see above) source code pi.c and also the (nearly, as #include is not missing so that the compiler does not need to guess my dependencies) equivalent code in a more traditional source layout tpi.c.

Happy Pi Day!