Ultrashock Forums > Flash > ActionScript > UltraMath
Evenly distribute objects in defined area

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
Evenly distribute objects in defined area
Old 2008-02-06

Hey,

Ok... I need some help here!! I am attempting to take x amount of objects that have random dimensions and evenly distribute them in a 2d area. I know there has to be some sort of algorithm for this I have been googling for the last 45 minutes and haven't come up with anything yet. The most important part is that there can be a random number of objects that have random dimensions. To bad I can't decompile the flash IDE and look at the code for the align panel



Thanks!
postbit arrow 3 comments | 974 views postbit arrow Reply: with Quote   
Registered User
sims11tz is offline
seperator
Posts: 570
2002-02-01
Age: 28
sims11tz lives in United States
sims11tz's Avatar
seperator

Ultrashock Member Comments:
sims11tz's Avatar sims11tz sims11tz is offline sims11tz lives in United States 2008-02-06 #2 Old  
I ran across this post volume (space) equation help It seems to be my exact dillema and the poster said he found a solution and posted links to his code... but the links are dead
Reply With Quote  
spooky's Avatar spooky spooky is offline spooky lives in United States 2008-02-06 #3 Old  
Here are some AS2 functions I had stashed away in a project.
They can be polished more, but they got the job done for the old task.

ActionScript Code:
  1. /****************************************
  2. Feed all the MovieClips you want aligned as an Array
  3. into each of these functions
  4. ****************************************/
  5. function centerHorizontal(objects:Array) {
  6.     alignHorizontal(objects);
  7.     distributeHorizontal(objects);
  8. }
  9. function centerVertical(objects:Array) {
  10.     alignVertical(objects);
  11.     distributeVertical(objects);
  12. }
  13. function alignHorizontal(objects:Array) {
  14.     var topmost;
  15.     var bottommost;
  16.     var midy;
  17.     var mc;
  18.     var n = 0;
  19.     while (n<objects.length) {
  20.         mc = objects[n];
  21.         if (topmost == null) {
  22.             topmost = mc._y;
  23.         }
  24.         if (bottommost == null) {
  25.             bottommost = mc._y;
  26.         }
  27.         if (mc._y<topmost) {
  28.             topmost = mc._y;
  29.         }
  30.         if (mc._y>bottommost) {
  31.             bottommost = mc._y;
  32.         }
  33.         n++;
  34.     }
  35.     midy = topmost+Math.abs(topmost-bottommost)/2;
  36.     n = 0;
  37.     while (n<objects.length) {
  38.         mc = objects[n];
  39.         mc._y = midy;
  40.         n++;
  41.     }
  42. }
  43. function alignVertical(objects:Array) {
  44.     var leftmost;
  45.     var rightmost;
  46.     var midx;
  47.     var mc;
  48.     var n = 0;
  49.     while (n<objects.length) {
  50.         mc = objects[n];
  51.         if (leftmost == null) {
  52.             leftmost = mc._x;
  53.         }
  54.         if (rightmost == null) {
  55.             rightmost = mc._x;
  56.         }
  57.         if (mc._x<leftmost) {
  58.             leftmost = mc._x;
  59.         }
  60.         if (mc._x>rightmost) {
  61.             rightmost = mc._x;
  62.         }
  63.         n++;
  64.     }
  65.     midx = leftmost+Math.abs(leftmost-rightmost)/2;
  66.     n = 0;
  67.     while (n<objects.length) {
  68.         mc = objects[n];
  69.         mc._x = midx;
  70.         n++;
  71.     }
  72. }
  73. function distributeVertical(objects:Array) {
  74.     var topmost;
  75.     var bottommost;
  76.     var midy;
  77.     var mc;
  78.     var n = 0;
  79.     while (n<objects.length) {
  80.         mc = objects[n];
  81.         if (topmost == null) {
  82.             topmost = mc._y;
  83.         }
  84.         if (bottommost == null) {
  85.             bottommost = mc._y;
  86.         }
  87.         if (mc._y<topmost) {
  88.             topmost = mc._y;
  89.         }
  90.         if (mc._y>bottommost) {
  91.             bottommost = mc._y;
  92.         }
  93.         n++;
  94.     }
  95.     var ny = topmost;
  96.     n = 0;
  97.     while (n<objects.length) {
  98.         mc = objects[n];
  99.         mc._y = ny;
  100.         ny += mc._height;
  101.         n++;
  102.     }
  103. }
  104. function distributeHorizontal(objects:Array) {
  105.     var leftmost;
  106.     var rightmost;
  107.     var midx;
  108.     var mc;
  109.     var n = 0;
  110.     while (n<objects.length) {
  111.         mc = objects[n];
  112.         if (leftmost == null) {
  113.             leftmost = mc._x;
  114.         }
  115.         if (rightmost == null) {
  116.             rightmost = mc._x;
  117.         }
  118.         if (mc._x<leftmost) {
  119.             leftmost = mc._x;
  120.         }
  121.         if (mc._x>rightmost) {
  122.             rightmost = mc._x;
  123.         }
  124.         n++;
  125.     }
  126.     var nx = leftmost;
  127.     n = 0;
  128.     while (n<objects.length) {
  129.         mc = objects[n];
  130.         mc._x = nx;
  131.         nx += mc._width;
  132.         n++;
  133.     }
  134. }
Reply With Quote  
sims11tz's Avatar sims11tz sims11tz is offline sims11tz lives in United States 2008-02-06 #4 Old  
Awesome, thanks for the post and CODE Spoooky! Ill respond with my results in implementing this for others that stumble upon this.
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: