Third Anniversary

2018-03-28, post № 194

, #2017, #collage

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


BMP Implementation in C — Graphic Primitives

2018-03-24, post № 193

C, programming, #2017, #bitmap, #bmp, #drawing, #image library, #image manipulation, #library

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.

Test image regarding 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)

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
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.

Source code: bmp.c


2018-03-14, post № 192

C, mathematics, programming, #improper integral, #power series, #source layout, #Taylor series

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 𝜋, albeit this year’s accuracy is not the greatest (Try it online [1]).

           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 𝜋 throughout the Pi Days; this time I chose to use an improper integral paired with a power series. 𝜋 is calculated using a famous identity involving infinite continuous sums, roots, e, statistics and — of course — 𝜋.

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

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


Both formulae are combined, the approximated value of \sqrt{\pi} is squared and 𝜋 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!

Extra assets: pi_no-warnings.c
Jonathan Frech's blog; built 2021/04/16 20:21:20 CEST