Pages

Showing posts with label mathematics. Show all posts
Showing posts with label mathematics. Show all posts

Monday, 9 August 2010

FlashMath Demo 1

Just a really simple demo, which uses the FlashMath library. Click to launch it. The demo uses a string evaluation function to generate the curve. This function is also used to calculate the integral of the curve using Simpsons rule between min and max, and by clicking on the graph, finite differencing is used to calculate the gradient of the curve. Data set and graph classes are used to store / draw the data. The parser was created by http://www.undefined.ch/ and is a really useful tool! The demo might be a bit buggy at the moment, but its a good sign of things to come!

Tuesday, 3 August 2010

Experimentalized Maths Library

It struck me today, that although I reuse a lot of code and classes, the one thing I tend to rewrite each and every time I start a new program is the mathematics. I dread to think how many hours this has wasted me over the years, so I finally decided to write a toolkit encompassing almost anything maths based. So far I've written a couple of constant utilities, one for mathematical constants, and one for scientific constants and a complex number class. Currently on the list are:
  • Matrices
  • Data sampling and statistics
  • Quadrature (integration) - Rectangle, Trapezium & Simpson's rule 
  • Numerical differentiation - finite differencing ✔,  runge-kutta (and other ODE solvers)
  • Graphs
  • Vectors ( 2, 3  and N dimensional)
  • Geometry class & trigonometry solvers
  • Probability
  • Random numbers (Mersenne Twister, other PRNGs)
  • Time and date
  • Complex numbers 
  • Bitwise operators 
  • Mathematical constants 
  • Scientific constants 
It would be great if you could let me know what kinds of maths utilities you would find useful, and even better if you have some of your own code and would like to collaborate with me on this.

Update: Your suggestions:

  • Bezier curves (Cubic & Quadratic), Bezier surfaces?

Wednesday, 7 July 2010

Circle Packing - brute force approach

Earlier this year, as part of my degree, I carried out a project that involved measuring the fractal dimension, and packing densities of various systems. I thought I'd have a play around with something along similar lines, circle packing.

There are many subtle and interesting and above all intelligent ways of packing circles into spaces, but this is not one of them. I decided to take a brute force approach. This is the structure my algorithm took:
  • Pick a random point, if it is within any circles that already exist find a new point
  • Increase the radius of that point until it overlaps with either the edge of the scene, or another circle on the scene.
  • Draw the circle
This is as far as I can tell, both the simplest approach and the slowest. Lets take a look at the results, click the image to run the application. An interesting idea is to draw a line between each circle when they collide. This forms a kind of node based structure between circles, creating a network where larger circles hold more power in the network. I wonder if you could create a page ranking system based on something like this...?

Tuesday, 6 July 2010

Benoit Mandelbrot - Fractals and the art of roughness

A personal hero of mine talks about fractals.



I enjoyed his quote at the end and found it quite relevant to a lot of the work I do on this blog:

"Bottomless wonders spring from simple rules... repeated without end"

Friday, 2 July 2010

Computer Vision and the Hough Transform

I was working on computer vision yesterday and went to my girlfriend's graduation ceremony today so haven't had a chance to upload. I decided to take another look at computer vision - and in this case a slightly more intelligent one than previously. The method I took a look at is known as the Hough transform. I think that the simplest and most effective way to explain how the Hough Transform works is using a bullet pointed list, but before I begin you should know that it is used to extract information about shape features from an image. In this case a line / all the lines present in an image.

  • We start with an image in which we want to detect lines:

  • First of all apply a Canny Line Detector to the image isolating edge pixels:

  • At this stage, to reduce computation required in the next stage I decided to sample edge pixels instead of using all of them. The results for s=0.2 and s=0.9 are shown below:

  • Then, for every possible pair of sampled edge pixels, calculate the angle of the line between it, as well as the distance to the origin. Plot a cumulative bitmap graph of angle against distance. The way I did this was to increase the pixel value in that location each time a distance/angle occurred. Recall that a line can be defined by an angle and distance to an origin. The resulting bitmap is known as the Hough Transform:

  • By increasing contrast and playing around with thresholds in the Hough transform it is clear that 8 distinct regions are visible, we know that the detector has done its job as there are 8 lines in the original image:

  • Remember that each point provides us with data about the line angle and distance from the origin, so lets use the flash API to draw some lines:

    • As you can see the result isn't too bad at all, we get 8 lines, although they are a bit diffuse. The next step is to look into grouping algorithms to only pick single pixel points from the final hough transforms for each lines. In this version of the program a line is drawn for each pixel, which results in an angle and position range for each line. Pretty close though.
    So thats how a Hough transform works for line detection. Just for fun, heres a real time hough transform on a webcam stream. Just click the image below to launch it.


    Tuesday, 22 June 2010

    Von Neumann Cellular Automata

    I just saw the TED talk from Stephen Wolfram below and noticed I'd never written any 1D cellular automata. He briefly mentions rule 30 and the emergence of complexity from a simple set of rules. The Von Neumann automata is very simple to write. Given a single cell, the state of the new cell (either on or off) is determined by the cells in its local neighbourhood. In one dimension this means the state of itself, the cell just to its left, and the cell just to its right. This means there are 8 possible states for local neighbourhoods (8 rules) and therefore 256 possible outcomes of the system.



    As Mr Wolfram mentions in the talk, most of the outcomes are not particularly interesting, some are very plain and some are quite pretty. Below is rule 26, it looks nice but beyond that it is quite a simple rule. Self repetition on a number of different scales is pretty much all it is capable of. Just click the image to launch it.



    The next two examples are rule 30 and rule 110 respectively. These are extremely interesting. Rule 30 displays what Wolfram calls "Class 3" behaviour, which is a chaotic and seemingly random, whereas Rule 110 displays "Class 4" behaviour, which is neither completely random nor completely repetitive. The interaction of various local structures is used to prove Universality. Again just click the images to launch the programs!

    Rule 30 can be used as an excellent random generator:


    Notice the life-like region travelling up near the centre in Rule 110:

    Friday, 26 March 2010

    Travelling Salesman Problem

    I have been extremely busy over the last few weeks with various University projects, so the genetic algorithm work has moved to the bottom of the stack. The ideas and problems are still quite fresh in my mind, so without going straight into programming the physics based evolving creatures, I'm going to have a look at something else quite closely related. It is linked in to both evolution and optimization. There is a very old mathematical problem, known as the travelling salesman problem (TSP). The TSP involves finding the shortest route between a number of cities, visiting each city only once.

    Finding fast solutions to the problem is vital for applications such as route finding on maps. Anyway, I recently read an interesting paper one approach to solving the TSP ( Ant colonies for the traveling salesman problem ). The paper shows that using ant agents, which lay virtual pheremones, a shortest path can be found. Ants prefer to follow paths with more pheremones on them - this means that shorter paths will build up more pheremones because more ants will travel along them.

    When I get back to my computer I will have a look at creating a similar solution to the problem in flash, and see the kinds of results that can be produced.

    It might also be interesting to look at the effects of changing the properties of ants on how well the system optimizes itself.

    I'll get back to you with any results!

    Tuesday, 15 December 2009

    New Scientist Features the Mandelbulb

    I discovered an article in the latest New Scientist magazine about Daniel White's Mandelbulb. Found it really cool as I've been working on this stuff quite a lot recently! Read the whole article here at New Scientist!

    Thursday, 26 November 2009

    Ray Tracer in AS3: Rendering the Mandelbulb in Flash

    So I went off for a few hours and thought how I could improve the voxel engine. Both from an efficiency perspective as well as that of realism.

    I realised that the only route was ray tracing and after reading this fantastic article by SuperJer about his pixel machine experiment I was motivated and inspired to try something along these lines for my renderer.

    The article takes the creation of a ray tracer from its very early stages to a really polished program. All this happens in one weekend by the way, I really suggest you read it.

    Anyway the idea behind raytracing is that a ray of light is passed from your camera through a pixel on the screen. The ray formed between the two points continues to pass through your scene until it hits something. When it hits something the pixel through which the ray passed is updated with the colour value of the object it hit, or failing that the colour of the sky behind, and a ray is "fired" through the next pixel until your image is formed.

    So in my case I'm trying to render the Mandulbulb set. Something I've now done many times before, but lets be honest, in the past I've needed huge numbers of voxels and the results weren't great. So what happens in the ray tracer?

    1. I fire a ray through the current pixel - starting at 0,0 working all the way to the image width/height.

    2. I check along the length of the ray to see if that location is a part of the Mandelbulb Set.

    3. If it is - check if that point is in light or shade.

    4. Fire another ray from this point to my light source.

    5. Check a bit away from this point in the direction of the light source, if that point is also a member then any light from the light source would have been stopped. So shadow the pixel.

    6. Otherwise colour the pixel normally.

    The resulting images that are built up show a lot more detail. Not only that they have pixel perfect shadowing, perspective, rotation and you can't see any stupid voxels.

    Lets take a look below at some ray tracing in flash. Below you can see increasing resolutions from
    137 x 100 to 1100 x 800 pixels.













    And below is one I left to render for about an hour (2200 x 1600 pixels). Just click to enlarge it!



    Sunday, 22 November 2009

    Inverse Mandelbulb Pixel Mapping in Flash

    When we map the 2d Mandelbrot Set we normally colour a pixel depending on the rate at which that point tends to infinity on some colour scale. In 3D normally this wouldn't work - where you have solid objects you'd be changing their colour internally. But since we are looking at cross sections using the voxel renderer it is actually very doable. Here are a few examples:


















    Remember that the inverse is being mapped. I really like the effect, and its definitely nice to add some colour to the dull grey images of the previous post.

















    Its like looking into a three dimensional cave of fractal goodness. Another possibility would be to use transparent pixels and change transparency levels. I will give this a go next as it would allow the whole shape to be seen externally, with colour (possibly? we'll see!)  Anyway click here to see all this fractal goodness happening in real time in flash using AS3! Enjoy













    Working - 3D Mandelbrot in Flash

    Possibly a world first, I don't know, but I can confirm that I have rendered Daniel White's Mandelbulb set using AS3.

    The first render is 216 Million voxels (600*600*600) and took about 10 minutes to do.

    This is the 8th order of the Mandelbulb (where things start to get interesting from a fractal detail point of view).

    First screenshot is half way through the rendering process:




















    And here is the finished product:



    Hopefully soon I'll have a 3D Mandelbrot Explorer ready and working much like the 2d version previously. I'm currently rendering an image 1000x1000x1000 voxels in size (1 Gigavoxel), and I'll add it to this post when it comes out!

    edit: here is the gigavoxel render (zoomed in a bit too much though which I'm annoyed about so it cut off the "colosseum" structure at the top!


    here is a quick preview of the voxel engine rendering a lower resolution (400x400x400) version.

    To zoom in at the moment you'll just have to right click and press "zoom in" but this won't actually zoom in, just give you can enlarged version. Worth doing if you want to see individual voxels though :)

    Enjoy!

    edit: Just started playing with zooming and rotating:

    Here is a view of the colosseum structure (normally at the top) from the side:

    Wednesday, 18 November 2009

    First Demo of the Mandelbulb in flash.

    So I've put together a demo of the Voxel Renderer doing something - not entirely sure if it is the true Mandelbrot in 3d - but  click the image below to see the approach I'm taking -

    Zoom in and you'll see how it's constructed from isometric cubes.



    Tuesday, 17 November 2009

    Mandelbulb - First Thoughts

    In light of Harry's comment I thought I'd take a look at trying to code a Mandelbulb set.

    The first obstacle was how to render:

    The examples I've seen mainly use ray tracing - there is no way as3 is capable of raytracing this kind of complexity within my lifetime. I decided to write a simple voxel (3D pixel) engine to display the set. The theory behind this is fairly simple. Work in isometry (no perspective), render from left to right, bottom to top and front to back and all should work out.

    Just so I knew the kinds of things I was looking for here are a few of Daniel Whites original renders of the Mandelbulb.



    So quite something to aim for with flash!

    Anyway, I thought I'd start BIG, and by BIG I probably mean I regret doing it now because of the insane render time. Poor flash player.

    As I write this the whole render (1000x1000x1000 - 1 million voxels - 1 mega voxel!) is about 2 percent complete. I don't know whether what I am rendering is the right thing or not but hopefully as more shapes begin to unveil themselves it will become clear and I'll know whether to start again or not!

    Here are a few renders using the Voxel Engine:

    The first few cross-sections - some potential? We'll see..


    About 10 cross-sections down - only 990 to go! There are some interesting shapes going on though - note this is a zoom. You can see the individual voxels and how I build them up quite nicely. There does seem to be an awfully regular plane forming which shouldn't be there.




    Yet more complete - starting to get some nice inwards curving - hopefully it will make a shape thats kind of spherical...



    Just a bit more - about an hour down and the stage I go to bed at! I'll take another look in the morning - until then good night!


    Below is what I found the next morning: Not exactly what I was looking for - fractal? possibly - but not nearly as complex as the Mandelbrot I should have been getting.

    Unfortunately this means I'll have to change the settings and start over: at least I know the Voxel Engine works, and that I shouldn't do quite as ambitious renders! I'll keep you updated on the results!


    Wednesday, 11 November 2009

    Ported Mandelbrot Explorer for Flash AS3

    Hi everyone - I've got some very very exciting news!

    I've made a flash version of the project I've been working on at uni with export functionality.

    Click here to launch it

    The features list is as follows:

    Colour Settings
     -> Red, Green, Blue
     -> Greyscale
     -> 3 types of multicoloured rendering

    Form Settings
     -> Classic Mandelbrot
     -> Conjugate Mandelbrot
     -> Absolute Mandelbrot
     -> Imaginary Absolute Mandelbrot
     -> Real Mandelbrot

    Dimension Settings
     -> 2,3,4,5,6

    Preview Image
     -> 150px by 120px

    Output Image
     -> png format
     -> 400-2000 width, 300-2000 height

    Panning and Zoom - outputted location to interface.

    Rendering is never more than about 20seconds even for 2000x2000 pixels on my 2GHz 2GB iMac which is pretty good considering its flash!

    Here is a little render of a Burning Ship Fractal at (-1.88480, -0.00041) with a zoom of 126988x. Just click to view it full size!


    Monday, 9 November 2009

    Outward zoom of embossed fractal!

    Just a quick animation from the application and exported with emboss settings - only 35 frames but you get the idea!

    Hi Resolution Mandelbrot Renders - from the Mandelbrot Explorer

    Here are four hi-res images of outputs from my new piece of software - Mandelbrot explorer. The software normally only runs on Windows - but I managed to get it working on my Mac at home using WINE and thought I'd upload some images with various render settings!

    Without any further ado - here they are:








    Enjoy their high-def beauty!

    Again - if you want the app just send me an email!

    Tuesday, 3 November 2009

    Buddhabrot Flash - the one you've been waiting for!

    Here are some initial renders of the Buddhabrot written in as3 - remember as3... not C++ or Java!

    The images generated are 1200 by 1200 pixels. I'm not quite getting the desired effect - but I'm playing around and hopefully eventually I'll get there.

    The image below shows a particular render at various stages in it's lifetime. The gradual changing of the colour is really quite hypnotic. Each render is 1200 by 1200 pixels.





    This next one shows a single 1200x1200 pixel render where members of the mandelbrot are rendered as well as non members. Just click on it to expand the image!













    Finally here is an example of the set being rendered in flash - I'll put up some nicer looking examples when I can work out how to get the working, but until then, enjoy! P.S this one takes a while to get going.

    Wednesday, 28 October 2009

    Some incredible renders of the Buddhabrot and Nebulabrot

    I'm working on implementing buddhabrot/nebulabrot rendering in the Mandelbrot Explorer application. The buddhabrot - so named because it resembles the classic statue of Buddha - is a more beautiful version of the Mandelbrot, and the Nebulabrot is a slight variation on this.

    The idea is that regions that escape the Mandelbrot set are plotted as they head off to infinity. The colour of each point depends on how often that point is seen to head off to infinity. Here are a few images - I can't wait to get a working demo - it does run very slowly in flash at the moment so I'll try and sort that out. Click on the images to expand them!


    The nebulabrot uses a rendering Method that is exactly the same as the way false colour is applied to images of the universe by astrophysicists. The resemblance is phenomenal!

    Enjoy them and I'll post a flash generator soon!

    (Images are: Buddhabrot, Anti-buddhabrot, Nebulabrot, Nebulabrot horizontal)