Anyway, I just had a browse to see what kinds of examples I could find of fractals present in nature and some of the images I found were unbelievable! Just check out some of the images below (click for full size). I find the images of rivers and streams are the closest in appearance to the Mandelbrot set, although some other patterns are also found, like the spiral shape of the tornado.
Wednesday, 30 September 2009
Fractals And The Mandelbrot Set In Nature!
Anyway, I just had a browse to see what kinds of examples I could find of fractals present in nature and some of the images I found were unbelievable! Just check out some of the images below (click for full size). I find the images of rivers and streams are the closest in appearance to the Mandelbrot set, although some other patterns are also found, like the spiral shape of the tornado.
More Emergence - The Mandelbrot Set
The rules that govern the Mandelbrot set are very simple - anyone with a basic understanding of complex number theory should be able to make it work. Understanding why, and how it works is a much harder question, and something that I won't go into. I'm just here to make some pretty pictures!
So how does the mandelbrot set work? Imagine the image to the right is made up of points, set co-ordinates. Each point is described by one number which tells us where it lies vertically, and another which tells us where it lies horizontally. Mandelbrot mapped the vertical location to imaginary space, and the horizontal location to real space - in other words he mapped the co-ordinates to complex space (for mathematicians out there - a pixel is pretty much equivalent to a point on an Argand diagram).
Complex space is made up of complex numbers of the form a + ib, where a and b are both real numbers.
We can calculate the square of a complex number using:
(a + ib).(a + ib) = a*a - b*b + 2iab
The real component of the new number is now (a*a-b*b), and the imaginary component is 2ab.
The Mandelbrot set arrises when the number of iterations for the value of a*a+b*b is calculated to be greater than some threshold value for each pixel. The colour variation you see on the diagram depends on the number of iterations for the prior calculation for that particular starting number.
For lower iterations the detail in the image does not appear as much. Where as for higher iterations (the demo below is set to 500 iterations per pixel) the detail achieved will be far greater.
One issue arrises in the way that computers - in this case flash - handles numbers. Floating point numbers will not achieve the required accuracy when the amount of zoom on the graph is too high. Unfortunately this results in an infinite amount of zoom being impossible on this platform.
Just click on the image to the right to launch the mandelbrot set program. Enjoy it, and have a play around going down several routes. To the left of the main cardioid bulb is a region dubbed seahorse valley. It is one of my favourites!
For some more interesting articles on the Mandelbrot Set and some incredible zooms visit:
University of Utah
Mandelbrotset.net
Enjoy!
Tuesday, 29 September 2009
Population Growth Model
I wrote a small program in flash which took some of the points mentioned in the book on population growth - which incidentally can be compared to bacterial growth and many of the shapes produced on a large scale are very similar! These similarities can be explained by a process called emergence, whereby complex patterns - for example the shape of a city - can arise from simple mathematical rules - for example people prefer to live near water. To the right is a nice image showing some outcomes of emergence.
Back to the flash side of it - I wrote some software which models two populations. The populations could be bacteria, they could be human, or animal - it doesn't really matter. On the screen any red pixels represent some kind of natural resources. The populations spread onto the red pixels whenever they can. If a population encounters another population they cross breed, creating hybrids of the two.
If you run the program you'll notice the button on the bottom left can be set to various modes - obstacle creates physical blockades which block populations from growing, and immigration allows new populations to randomly appear across the screen.
Of course there are lots of other factors that could be taken into account like populations dying, different populations, one population having genetic advantage and so on - but I've found the best way to build up a model is to start simple with only a few rules and then build up more rules until you have a fit that matches real world observations.
Just click below to run the app:
Enjoy!
Saving Webcam Snaps using AS3
We already have the following code to create a webcam on the stage -
var CAM:Camera = Camera.getCamera();
CAM.setQuality(0, 100);
CAM.setMode(550, 400, stage.frameRate );
var VIDEO:Video = new Video(550, 400);
VIDEO.attachCamera(CAM);
addChild(VIDEO);
So lets add an event listener to the stage for when a user clicks on the image, this will later invoke the function to save the image.
stage.addEventListener(MouseEvent.MOUSE_DOWN, save);
function save(e:MouseEvent):void
{
}
The tool I use to encode image data is called AS3 core lib. This tool has a load of classes but the one we want to use is called JPGEncoder.as, and you can download it here.
So lets import that into our movie. First create a folder, call it "com" and drag your newly acquired file into it. This folder should be in the same directory as you .fla and .swf files. Above all of the other code on the first frame write the following:import com.JPGEncoder;
This imports the class we'll need to use to create an image. Next, and just below that add this:
var jpg_encoder:JPGEncoder = new JPGEncoder(100);
This initiates the class and sets the jpg output quality to 100. Of course if you want to output low quality jpegs you could always change this to 50 or something else.
So we have the jpeg encoder, what this does is converts bitmap data into a byte array which your computer will be able to read as a file. So next we'll create a bitmap, this should have the same resolution as your webcam stream (in this case 550x400).
var bitmap:BitmapData(550,400);
Now, within the save function lets draw the VIDEO object into the bitmap data using the draw() function
bitmap.draw(VIDEO);
If you read my previous post on saving from flash you'll already know exactly how to save files, just in case you missed it here it is -
1) Create a file reference object:
fileToSave:FileReference = new FileReference();
2) In your save function encode the jpeg data, we'll use the ByteArray class...
var bytes:ByteArray = jpg_encoder.encode(bitmap);
3) and finally just after that within the save function
fileToSave.save(bytes,"screenshot"+".jpg");
The parameters are data and name, the data being the encoded jpeg and in this case the name is "screenshot.jpg". Note you'll need the extension for your computer to recognise the data as an image.
So here is an example, take a little snap of yourself from your webcam. (The whole file is only 8kb!)
Enjoy guys!
Webcam Tutorial - Getting a webcam set up in flash
var CAM:Camera = Camera.getCamera();
CAM.setQuality(0, 100);
CAM.setMode(550, 400, stage.frameRate );
var VIDEO:Video = new Video(550, 400);
VIDEO.attachCamera(CAM);
addChild(VIDEO);
So lets go through this one line at a time.
First the Camera class is initiated and a camera object is created using
var CAM:Camera = Camera.getCamera();
Some properties of the camera are then set, the size, quality and frame rate.
CAM.setQuality(0, 100);
CAM.setMode(550, 400, stage.frameRate );
Finally an object needs to be created to make the camera appear on the screen so lets attach the camera to the VIDEO object, and then add the VIDEO object to the stage.
var VIDEO:Video = new Video(550, 400);
VIDEO.attachCamera(CAM);
addChild(VIDEO);
Just click below for an example of how this works.
Sunday, 27 September 2009
Webcam Edge Detection
After calculating the difference blend, the contrast is increased using a matrix filter. This method could be used for shape detection.
The next challenge would be to determine points along each contour, and store contour data to actually do something with it, instead of just showing it visually. Enjoy!
Friday, 25 September 2009
jQuery UI - Tab Systems
Tabs are primarily a form of navigation, but what makes them different from a standard navigation header bar, with standard buttons is that the flow through the site is much improved. You don't need to leave a page to view content inside a different tab. This means less wait times, and a nice way to store anything you want to show on your site.
You could opt to write your own tab system, but often it can be hard to achieve complete, or maximum browser compatibility, especially if you're in a rush, or don't want to spend to much time writing something in javascript/ajax.
In a post a few weeks ago I introduced jQuery, and briefly mentioned the jQuery UI package. The jQuery UI package builds on the jQuery framework by adding a set of transitions, actions and web components which work in practically any browser. If you have a quick look on the jQuery UI website you'll notice the build custom download button on the right of the page. You can graphically design most features of the components you want and export the styles and classes associated with them straight from the website.
So lets get started, we wanted to make a tab system.
1) Click Themes on the menu at the top of the jQuery homepage. We want to be a bit interesting so lets make a custom theme.
2) You'll notice on the right you are presented with the UI components, in the default style and on the left you'll find a menu with the headings "Roll your own" and "Gallery". For a set of premade styles click gallery. Often you'll find something here that is in keeping with the design of your website, or you'll find these styles a good jumpstart so that you don't have to design everything from scratch. So I'm making a pretty Techy website, and want something quite cool. Click on the gallery tab and select edit under the Dot Luv style. I really don't like the diagonal lines at the top though, so I'll play around with the top bar. Obviously if you like them, keep them, and have a play around with some of the other settings on the left.
3) Once you're completely happy with your design press the Download Theme button at the top of the settings, you'll be confronted with a new page. Panic not! We're making a tab menu so we don't need any of the other features (with the exception of UI Core which contains some basic functions and initializers) so deselect everything except UI Core and Tabs (in the widgets section).
4) When you're happy with this download the file using the button on the right. At this stage its a good idea to open the file called index.html within the package you downloaded. This contains all of the UI content you downloaded as well as some useful buttons (a tip - if you're looking for lightweight you can remove the styles and images for all of these buttons quite easily by hand). So there is your tab system!
5) To include it on your website you'll have to upload your stylesheet, javascript files and associated images onto your server. Once they are up just link to your files. You'll have to load in the jQuery style sheet first, then the standard jQuery library, and then the jQuery custom library.
For example:
<link type="text/css" href="css/dot-luv/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
would achieve the required effect.
Then to initiate the tab system you'll need to use a simple line of jQuery
<script type="text/javascript">
$(function()
{
// Tabs
$('#tabs').tabs();
}
</script>
And finally the actually object needs to be placed in your page:<div id="tabs">
<ul>
<li> <a href="#tabs-1">Tab 1</a> </li>
<li><a href="#tabs-2">Tab 2</a></li>
<li><a href="#tabs-3">Tab 3</a></li>
</ul>
<div id="tabs-1">I am tab 1's content</div>
<div id="tabs-2">I am tab 2's content</div>
<div id="tabs-3">I am tab 3's content</div>
</div>
As you can see all of the content is loaded into seperate divs, one for each tab!This is really useful as I mentioned earlier for getting smooth flows around your site, and for improving the user experience!
Unfortunately blogger blogs don't allow you load in style sheets on the fly from within posts so I don't think I can get you a live example, but here is a link to a page I'm working on which does use the tab system. Bear in mind its work in progress!
Also check out The Game Portal if you haven't done so already as that also uses jQuery UI!
Enjoy!
Tuesday, 15 September 2009
Nitrome - Some featured games!
Monday, 14 September 2009
Build your own color detector.
Understanding the nature of the colours present in light is a vital part of this project. Light has an additive nature, unlike pigments, which have a subtractive nature. This means that increasing proportions of red, green, and blue light result in
the formation of white light, and various combinations of those colours can form every colour in the visible spectrum.
Due to their ability to produce such a vast array of hues they also form the basis for colour formation in standard computer monitors, televisions and other liquid crystal displays. Technically the choice of red, green and blue are not exact, they are chosen due in large part to the sensitivities of the cone cells in the rear of the retina. Differences in the hues of the red, green and blue constituents chosen may produce different absolute colour spaces, for example sRGB and Apple RGB are two absolute colour spaces used in computer monitors. In computing terms the R, G, B value of a colour represents its red, green and blue components proportionally, normally each on a scale of 0-255 (This requires three 8-bit values). For various other purposes in computer graphics a further two bits per unit may be added, for example RGBA includes information about the alpha channel (transparency) of a pixel, and contains three 10-bit units of colour per pixel. In the case of visible light however only the RGB values, and their associated intensities need to be taken into account.
Since the LDRs that will be used in the colour detector have little or no wavelength dependence it is necessary to use colour filters to determine which wavelengths of light are hitting which detector and in what proportions. Colour filters work by transmitting certain wavelengths of light and reflecting or destroying through interference, the other wavelengths. Manufacturers often produce filters that allow not only the target colours wavelengths through. The effect of this is is often to brighten up the filters, by letting through more light.
A problem with this is that it is practically impossible to determine the exact chromaticities of the filter. A solution to this involves measuring intensities of pure red, green and blue light after they pass through the filter when compared to the intensities before transmission. By creating a transformation matrix from one set of colours to the other and inverting that matrix, a transform will be created to convert measured intensities (post filter) to the original colour of light (pre filter).
Since an optical interface is being used which can only transfer data digitally there is no way of transferring the 3 analogue voltage values directly to the computer. The PIC is used to sample the 3 values, store them, and then convert them into a form which is analysable by the PC. The way chosen to do this was to take a voltage value (in 8-bit so between 0 and 255), and a time delay proportional to this which turned the output port either on or off after each delay. The code snippet below shows how this was achieved although full code for all programs used in the project can be found in the appendices.
//liner delay subroutine (0xFF times input variable);
a equ 0x45
b equ 0x46
delay movwf a
movlw 0xFF
movwf b
ca movwf b
cb decfszb
goto cb
decfsz a
goto ca
return
The delay loop assumes that the voltage value has previously been stored in a. When the loop is called the embedded loop repeats 256a times, creating a time delay proportional to a. By turning the output port on and off, before and after the loop is called, a square wave output of voltage can be created with a period proportional to the input voltage.
movlw b'00000001'
movwf PORTB
// then call the delay loop
movlw b'00000000'
movwf PORTB
This can be repeated 3 times for each of the measured values, resulting in three flashes. The timed pulses will then be sent optically to the computer.
For documentation on how to program your PIC or other microchip I recommend visiting the manufacturers website at http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=64
So the image to the right is pretty much a summary of the whole system from
LDR to PIC to LED. After this stage the 3 flashes and their corresponding delays (how long they are on/off for) is sent to the computer via an optical to digital converter.
The final step was to write some software in C (as well as a codec for the optical to digital converter), to output RGB values and display something nice on the screen, in this case a blue LED is being shone onto the LDRs.
Verlet Integration
var previous:Point = (0,0);
var tx:Number = 0;
var ty:Number = 0;
tx = current.x;
ty = current.y;
current.x += (current.x - previous.x + F)*R
current.y += (current.y - previous.y + F)*R
previous.x = tx;
previous.y = ty;