Friday 24 June 2011

“Project Cooper” Mandelbrot set calculation

I’ve had a couple of people ask me on separate occasions to show some “Project Cooper” code. I’ve not posted any thus far, as the product is quite early in its beta cycle and very few people have access to it. When it has matured and is in more hands I have plans to write lots more on the subject, but since I’ve been asked, I thought I’d post a simple project.

The ever-popular Mandelbrot set is the subject of this little demo, and it’s just a simple console application, completely bypassing the Android support, just building an app that runs on the Java VM.

unit Mandelbrot; 

interface

type
ConsoleApp = class
public
class method Main(args: array of string);
end;

implementation

class method ConsoleApp.Main(args: array of string);
const
cols = 78;
lines = 30;
chars = " .,:;=|iI+hHOE#$-";
maxIter = Length(chars);
begin
var minRe := -2.0;
var maxRe := 1.0;
var minIm := -1.0;
var maxIm := 1.0;
var im := minIm;
while im <= maxIm do
begin
var re := minRe;
while re <= maxRe do
begin
var zr := re;
var zi := im;
var n: Integer := -1;
while n < maxIter-1 do
begin
Inc(n);
var a := zr * zr;
var b := zi * zi;
if a + b > 4.0 then
Break;
zi := 2 * zr * zi + im;
zr := a - b + re;
end;
system.out.print(chars.charAt(n));
re := re + ((maxRe - minRe) / cols);
end;
system.out.println;
im := im + ((maxIm - minIm) / lines)
end;
//Press ENTER to continue
//system.out.println("Press ENTER to exit");
system.in.read();
end;

end.

Simple, huh? When built I get a Mandelbrot.jar file that weighs in at 1,754 bytes.


This is what it looks like running in a Windows command prompt:


Mandelbrot_Windows


And here is the same code (same binary, after all, it’s running on the cross-platform Java VM) running on Ubuntu Linux in a terminal window (note – this thumbnail looks pretty ghastly, but if you click it to see the original file, it looks a bit more presentable there):


Mandelbrot


Yeah, ok, so fair enough it’s not much of an example, but it is code!


Before signing off I’ll mention another demo, which I’ve got some screenshots of, but without any code to show from currently. Jim McKeeth from RemObjects has been playing around with jMonkeyEngine 3, a gaming platform supporting a rendering loop, all the graphics and object support you’d expect and the obligatory physics engine. It’s a set of Java libraries, so he followed one of the tutorials using “Project Cooper” and got the expected application out at the end. A wall, with a moveable camera point of you that allows you to fire a cannonball. All this is from a source file with around 250 lines in it, code and white space.


JME1


JME2


JME3


jMonkeyEngine has a goal to work on as many platforms as possible, and I’d read that they were planning Android support. At the time of writing, jMonkeyEngine 3 is at Alpha 4. The Android support is so preliminary that it isn’t in the main build yet, but I pulled it down from their nightly build area. The goal is to take the same source file as in the regular project above, and slip it into an Android harness with minimal code changes (preferably none). It worked out pretty well – here are a couple of screenshots from my phone:


JME_A_1


JME_A_2

No comments:

Post a Comment