Sunday, September 25, 2011

Complex Trickery

These last weeks have yet again be horribly busy, although less so than the time at Biton.  They've also been amazingly fun; I've started going to university, and although the volume of homework is more than I anticipated, it's mostly a lot of fun.

The only Computer Science class I have at the moment is Imperative Programming, which is basically an introduction to C#.  At the moment, we're writing a program that draws the Mandelbrot Set.  Next up is Reversi, and a Paint-like program (with, apparently, vector graphics and layers...).

Back to the Mandelbrot, though.  The change of language from C++ to C# is, in my opinion, not very noticeable.  I'm using more raw pointers than I was before (Huh?  But...), and there's more new sprinkled around my code.  Oh, and I suppose there's somewhat more inheritance than I had before.

The big difference between this project and what I've worked on before is that here, speed actually matters.  In the past, any GUI applications I may have had were not very calculation-intensive, and any calculation-intensive programs I had (Project Euler *cough* *cough*) were not GUI, and certainly weren't meant for others to see.

Now, this project does happen to combine the two.  Due to this, it would be nice if the program would:
  1. Calculate for each visible point whether it converges.
  2. Calculate the colour of each point.
  3. Allow the user to zoom in by (double-?)clicking on the image.
  4. Allow the user to move around by dragging the image.
  5. Allow the user to zoom in and out with the mouse wheel.
  6. Allow the user to resize the window.
  7. Do all this without frustrating the user (due to slowness) and still displaying an image of decent quality.
Some balance needs to be reached between image quality and speed, and at the moment, both aren't doing very well.  The cause of the problems is the choice of colours, which seems to be the hardest part of the whole matter.

Let's say we decide to take the simple approach, measure how many iterations it takes for the set to converge, and use that as an index.  This means that if we check for 500 iterations, we have 500 colours we can choose from:  all in all, not a particularly large number.  The number of iterations, therefore, does not only affect the detail at which the set can be viewed -- it also controls how colourful the end result is.

Now, how many colours are necessary for both smooth transitions and plenty of variety?  To be honest, I'm not sure.  If the palette is too small, the transitions will look off.  If it's too large, too many iterations will be needed to use all colours.

However, other than that, the problems aren't very big.  As I said, I'm using more raw pointers than in C++; drawing on a Bitmap directly is much faster than with SetPixel, so the code locks the bits, does the painting and then unlocks them.  This is also the first time I'm actively using threading -- however, at the moment, it gives little benefit.  Perhaps it'll be more noticeable with a larger screen, though.

Apart from that, there's some design concerns about the code, but it's nothing major.  The code is only suitable for drawing to a Bitmap, and that's unlikely to change (unless we decide we need the performance of OpenGL).  A bigger issue is the mixture of pixels and coordinates:  I think it may be time to unite all those things into bigger chunks (maybe an ImageData with pxSize, pySize, rxCentre, ryCentre, rScale?) with support for calculating the various information on the edges and all.

There's still plenty of work to be done (such as the code for only generating part of the image, which is up next), but things are going fairly well.  I'll be getting some more RAM soon (1 GB just isn't enough for a virtual machine), and will then hopefully be working at even greater speeds to finish all this.  As things are, I still have time to work at university, but it's just not quite the same as having an own machine.

I'll probably be rambling on about this for a while yet some other time.

No comments:

Post a Comment