1.4 grid distribution
"basics in generative art" is a series of articles available here:http://en.nicoptere.net/?p=1180
♥0 |
Line 50 |
Modified 2010-12-13 07:45:48 |
MIT License
archived:2017-03-20 07:51:31
ActionScript3 source code
/**
* Copyright nicoptere ( http://wonderfl.net/user/nicoptere )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ussv
*/
package
{
import flash.geom.Point;
import flash.geom.Rectangle;
/**
* @author Nicolas Barradeau
* http://en.nicoptere.net
*/
public class Grid
{
public var rect:Rectangle;
public var x:Number;
public var y:Number;
public var width:Number;
public var height:Number;
public var colNum:int;
public var rowNum:int;
private var points:Vector.<Point>;
public function Grid( rect:Rectangle, colNum:int = 5, rowNum:int = 5 )
{
this.rect = rect;
this.x = rect.x;
this.y = rect.y;
this.width = rect.width;
this.height = rect.height;
this.colNum = colNum;
this.rowNum = rowNum;
}
public function distribute( shuffle:Boolean = false, jitter:Number = 0 ):Vector.<Point>
{
points = new Vector.<Point>();
var p:Point, i:int = colNum * rowNum, offsetX:Number, offsetY:Number, x:int, y:int, dx:int, dy:int;
offsetX = width / colNum;
offsetY = height / rowNum
while ( i-- )
{
x = i % colNum;
y = int( i / colNum );
dx = this.x + x * offsetX + offsetX / 2 + ( Math.random() - .5 ) * jitter;
dy = this.y + y * offsetY + offsetY / 2 + ( Math.random() - .5 ) * jitter;
p = new Point();
p.x = dx;
p.y = dy;
points.push( p );
}
if ( shuffle ) points.sort( this.shuffle );
return points;
}
private function shuffle( a:Number, b:Number ):int
{
return ( Math.random() > .5 ) ? 1 : -1;
}
}
}