Math curve (DuplicateSprite)

 

The purpose is to display a mathematical function using a script..
I suppose you know well how to create objects, thus I will not insist on.

courbe

In Swish2, you might have seen a general demonstration of parametrical curves.

Creating Objects

1 - Set up movie size 400x400 (background color #999999).
Frame Rate doesn't matter.

2 - Create an horizontal line and a vertical line crossing the middle of the scene ( scene width and scene height )

3 - Now create a red circle : X = 0 Y = 0 W = 3 H = 3
Anchor point : Center

4 - Convert this circle into sprite ( name : dot , Target toggled )
Menu Modify / Convert / Convert to sprite.

5 - Use tool text tool, enter the following text over 2 lines :
      x = a sin t cos 2t
      y = a cos t sin 2t
Choose color #FFCC00 for instance.

Script

In Outline, select Scene_1. Enter the following script :

onLoad()
{ // scene height width
      H = int(_root._height);
      W = int(_root._width);
      a = 100;
      n = 500; // number of points
}
function x( t ) { return ( a * Math.sin(t) * Math.cos(2*t) ); }
function y( t ) { return ( a * Math.cos(t) * Math.sin(2*t) ); }

/*
       In scene, (0,0) is the top left corner and y-coordinates go downwards
      ⇒ we need to adapt mathematical relative coordinates x(i) , y(i)
       to absolute coordinates of the scene.
*/

onFrame (1,afterPlacedObjectEvents)
{
      T = 2 * Math.PI; // period
      dt = T / n; // increment 2PI/n
      for (i=1; i < n; i++)
       {
             duplicateSprite(dot, "dot"+i, i); // new point
             ("dot"+i)._X = (W / 2) + x(i*dt); // absolute x-coord
             ("dot"+i)._Y = (H / 2) - y(i*dt); // absolute y-coord
       }
}

'Et voilà', that's it.
Of course, this is not a movie but it allows some folks to easily show a mathematical function for a few pennies.
For my own work, I use Matlab ( see my scripts in Matlab section)
But this software is quite expensive for anyone.

Otherwise, these mathematical functions could give ideas to set movement effects given in libraries as well. ( sometimes pretty tough to understand )

 


Page chargée en 0.001 sec.

Dernière Modification : Sam 14 Octobre 2023 9:48
Copyright © 1999-2024 Jean-Paul Molina Tous droits réservés.

 

to Google