Drawing spheres with H2D2

Since I wrote this blog post, I have been looking for a better demo program to show what I can do with my H2D2 programming language. I needed something more interesting than "hello world" ... and which didn't involve drawing the Mandelbrot set.

I started trawling through the Rosetta Code website for inspiration and came across this sphere drawing program in C. I guess it's a simple ray tracing algorithm really. It seemed like a reasonable candidate.

So I set about porting it from C to H2D2, which was an interesting experience. Interesting because H2D2 is not very forgiving when you make mistakes - since I have not (yet) made the compiler error messages very user friendly. So it makes you reason things out very carefully in your head, rather than rely on the compiler to spot problems with your code.

I have managed to get it to work though, and the resulting H2D2 example program shows a few of the language features (like a function call and arrays for example). However, it also highlights a few things I should work on, like creating a syntactic shortcut for assigning values to an array. Anyway, I can now create output like this:

             !!...!!!***Bo               
         ..............!!**Boo           
      ..................!!**BBooe        
    .....................!!**BBooee      
   .......................!!**BBooee     
  ........................!!**BBoooee    
 .........................!!**BBoooeee   
!........................!!***BBoooeeee  
........................!!***BBooooeeee  
!......................!!***BBBoooeeeee  
!....................!!!***BBBoooeeeeeo  
*!.................!!!***BBBBoooeeeeeeo  
B*!!!...........!!!!****BBBooooeeeeeeoB  
 B***!!!!!!!!!!!*****BBBBoooooeeeeeeoo   
  oBB*************BBBBBoooooeeeeeeeoo    
   ooBBBBBBBBBBBBBBooooooeeeeeeeeooB     
    eoooooooooooooooooeeeeeeeeeooo*      
      eeeooooooooeeeeeeeeeeeeooB*        
         eeeeeeeeeeeeeeeeeoooB           
             oeeeeeeooooBB

But to show it in action, I made this recording, which shows it off in realtime in the browser:

Anyway, I am really pleased that it works so well. I can go back to fiddling around with the language now.

Raspberry Pi low-res fractal in C

Of course, it wouldn't be me unless I tried drawing a mandelbrot on the Raspberry Pi. Here is the traditional ASCII character version, but with some low-res colours.

Raspberry Pi Mandelbrot in C

I have to admit that the colour idea was inspired by somebody called Paulo Lellis, who had read some of my previous posts and adapted my code to include colour codes. It was very kind of Paulo to show me what he had done. I've simply tweaked it some more.

Anyway, if you'd like to try it, here is the source code:

#include <stdio.h>

/*
   Compile like this:
   gcc mandel.c

   Then run the code, like this
   /.a.out
*/

main()
{
   float b,e,r,n,d;
   int h;
	
   for(e=1.1; e>-1.2; e-=.1)
   {
      for(b=-2; b<1; b+=.04)
      {
         // see: http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
         printf("\033[1;4%dm%c\033[0m",(h%8),32);
				
         n=0;
         h=127;
         for(r=0; r*r+n*n<4 && --h>32; d=r)
         {
            r = r*r-n*n+b;
            n = 2*d*n+e;
         }
      }
      printf("\r\n");
   }
}

Well, I had to try the C compiler on the Raspberry Pi at some point.

Fractals on the real thing

My RT-11 boot disk with DECUS C worked a treat on the real machine.  It took 16 seconds for it to draw the mandelbrot set in ASCII art on the actual uPDP-11/53.  Here is a picture of the real thing in action:

ASCII fractal on a real PDP-11

I would say that's a result.  My next project is to use memory-mapped I/O in C to communicate over the second serial line.  This may allow me to hook the PDP to my serial-to-wifi board.