A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Chaos / Fractal Query: is it possible to model the Lorenz Attractor in Flash (5)?

  1. #1

    Chaos / Fractal Query: is it possible to model the Lorenz Attractor in Flash (5)?

    I am writing something for my new (non-commercial) website, which touches upon Chaos Theory. It occurred to me that maybe Flash could be programmed to model a fairly simple example of this, such as the Lorenz Attractor, which uses three variables to produce an inherently unpredictable graph, or the Lorenzian Waterwheel.

    A good page depicting both, with example formulas, can be found at:
    http://astronomy.swin.edu.au/~pbourke/fractals/lorenz/
    Another page with Java Applet illustrating the Attractor can be found at: http://www.cmp.caltech.edu/~mcc/chaos_new/Lorenz.html (I found speed=50 about right)

    Not only is the math beyond me, but so to is how to implement it in Flash. But I know there are some real brains here, so haven't given up hope yet...

    The least help I'm hoping for is that someone can tell me whether or not it's possible to use Actionscript to model either of those systems [with particular reference to Flash 5].

    Medium help would be to give me a pointer to a helpful manual or URL (for simpletons) that I could check out.

    Medium+ help would be to give me a clue how.

    Maximum help would be - of course - to do the Flash or Actionsript
    business for me, if it appears simple enough (to you). At the very least I'd include a credit and a link on what I hope will be a fairly sparky website and not a damp squib, but by all means name your terms...

    I am naively hoping the Attractor could be have some sort of user input start values, or that the Wheel could be made into an interactive movie, where the 'water rate' can be dynamically varied.

    Comments gladly received!
    Thanks.

  2. #2
    Senior Member
    Join Date
    May 2002
    Location
    Salt Lake City
    Posts
    211
    Flash can't handle differential equations natively. You may be able to do it by first writing a function that handles the differential equations. I can't remember what the formula is, but I remember it's similar to calc in that there's a shortcut, although, there are lots of rules and exceptions. Maybe create a function specific to these differential equations. You've also got a system of equations to deal with, which you could probably handle with an array or something; think linear algebra. I would just look into how to solve systems of differential equations and see if you can interpret the math into something flash can do. An easy way would be to find an example proof and use that. Here's something, but I'm not familiar with the code.

    http://mathews.ecs.fullerton.edu/n20...Mod_lnk_5.html

    And something else:

    http://planetmath.org/encyclopedia/LorenzAttractor.html

    Also, if you want a 3d representation then you'll have to learn how to do 3d in flash which I haven't done, but if you can get through the math for the equation, you could probably get through the math for the 3d stuff, although it might be a lot more code.

    Also, I think you could do it in Flash 5, but MX would be a lot easier.

    You may want to post this in the math and physics section.

    Good luck.
    tap water builds character
    -Aesop Rock

  3. #3
    You are very kind, thank you!

    Thanks for the links and the tip about the math and physics section - since I'd never had this sort of notion before, that forum simply didn't register with me. I shall do as you suggest.

    Thanks again. Good on'yer!

  4. #4
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Flash is certainly capable of handling any kind of math you want to throw at it...

    Here's a script that draws a lorenz attractor, kinda sorta (I'm faking the Z coordinate placement). Most of the complication arises from trying to keep the graph centered and scaled properly...

    This version throws out the first 300 data-points (cause they tend to be way off the chart) so you need to wait about about 10 seconds before you'll see anything.

    code:


    SW = Stage.width;
    SH = Stage.height;

    a = 10;
    b = 8/3;
    c = 28;
    d = .003;
    x = 1;
    y = 1;
    z = 1;

    kRenderScale = 1000;
    kPointsToThrowAway = 300;
    lx = 0;
    ly = 0;
    n = 0;

    _root.createEmptyMovieClip("container", 1);
    container._x = Stage.width/2;
    container._y = Stage.height/2;
    lorenz = container.createEmptyMovieClip("lorenz", 1);

    lorenz.lineStyle(.5,0xFF0000,100);
    minY = 10000;
    maxY = -10000;
    minX = 10000;
    maxX = -10000;
    maxD = 0;

    MovieClip.prototype.addPoint = function(x,y)
    {
    if (n++ < kPointsToThrowAway) {
    this.moveTo(x,y);
    return;
    }
    this.lineTo(x,y);

    // this code keeps the graph centered and scaled
    maxX = maxX > x? maxX : x;
    minX = minX < x? minX : x;
    maxY = maxY > y? maxY : y;
    minY = minY < y? minY : y;
    maxD = (maxY - minY) > (maxX - minx)? (maxY - minY) : (maxX - minx);
    this._x = -(minX+maxX)/2;
    this._y = -(minY+maxY)/2;
    scale = 100 * (SH/maxD);
    _root.container._xscale = _root.container._yscale = scale;
    }

    lorenz.onEnterFrame = function()
    {
    xnew=x+d*a*(y-x);
    ynew=y+d*(x*(c-z)-y);
    znew=z+d*(x*y-b*z);

    x = xnew;
    y = ynew;
    z = znew;
    // NOTE: Z is faked here by using it to offset x and y - as in an iso-rendering
    this.addPoint(x*kRenderScale+z*kRenderScale/2,y*kRenderScale+z*kRenderScale/2);
    }


    Last edited by jbum; 08-06-2004 at 03:09 PM.

  5. #5
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    Heres my attempt, leave it running and go have a coffee or something

    code:

    _root.createEmptyMovieClip("lor",1);
    var scaler = 10;

    function lorenz(h, a, b, c, x0, y0, z0, N, rad){
    i=0;
    lor.clear();
    _root.onEnterFrame = function(){
    x0 = x0 + h * a * (y0 - x0);
    y0 = y0 + h * (x0 * (b - z0) - y0);
    z0 = z0 + h * (x0 * y0 - c * z0);
    if (i == 750){
    lor.moveTo((x0 * scaler) + (z0 * scaler),(y0 * scaler) + (z0 * scaler))
    }
    if (i > 750){
    R = (255*(i/N)).toString(16);
    G = "00";
    B = (255*(i/N)).toString(16);
    col = "0x" + R + G + B;
    trace((i/N) + " / " + col);
    lor.lineStyle(0,col,100);
    lor.lineTo((x0 * scaler) + (z0 * scaler),(y0 * scaler) + (z0 * scaler));
    }
    if (i++ == N) {
    delete this.onEnterFrame;
    }
    }
    }
    lorenz(0.001099, 10, 28, 8/3, 1, 1, 1, 20000, 0.04);

    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  6. #6
    Well, I'm very impressed and honoured by such awesomely detailed responses, which so flatteringly over-estimate my comprehension...

    I was doubtful that implementing either of the above could be as simple as attaching the actionscript to frame 1 of a movie, but I tried anyway, and even took a coffee break, but no sign of anything. And now I have to shamefacedly parade my ignorance before the whole FlashKit community.

    Well thanks a bunch, guys...

    So if you'd kindly spell it out for me - if anything underestimating my intelligence - that'd help end my humiliation.

    I don't suppose it's 'cos I'm using Flash 5, whereas you're writing for MX?

    But really, thank you very much for going as far as you have!

  7. #7
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Hi,

    Both of the above codes work. The problem is using flash 5. The drawing methods that are in both above codes were not implemented until flash MX and they are not backward compatable.

    NTD

  8. #8
    Member
    Join Date
    Oct 2002
    Location
    Northern Hemisphere
    Posts
    58
    I was curious in seeing how this worked so I pasted this into the action pane of frame 1 in an empty movie and nothing. I am using MX. Should this be attached to a mc or something?

    edit: Never mind. It's working now My coffee break wasn't long enough! Ciao.

    \/---- curious and ignorant
    Last edited by rkitecsure; 08-06-2004 at 02:31 PM.
    Carl - rkitecsure@hotmail.com

    "one half mile to antelope freeway... one quarter mile to antelope freeway... one eighth mile to antelope freeway... one sixteenth mile to antelope freeway..." FireSign theatre rocks! Firesigntheatre.com

  9. #9
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Hi,

    It works as frame code, but if you read Lexicon's post... "go get a cup of coffee...".... It takes a bit of time for it to do it's thing. Leave it running for a minute or so and both codes work.

    NTD

  10. #10
    Ah, well, at least my standing in the community isn't so much in question, then, just my choice of Flash version.

    Dammit. Anyway, I'll keep the A/s handy and see what I can do about u/grading to MX.

    Thanks again guys.

  11. #11
    Junior Member
    Join Date
    Dec 2000
    Location
    the netherlands
    Posts
    29
    Well, I've just noticied this thread ... doh!
    I've been replying here:
    http://www.flashkit.com/board/showth...hreadid=574315

  12. #12
    As far as I'm concerned, you've been posting 100% in the right place.

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center