package { /** * @author nicoptere * http://en.nicoptere.net/ */ import flash.display.*; public class Voronoi { /** * draws a Voronoi diagram after a Delaunay triangles set * @param array the delaunay triangles set * @param graphics the output graphics */ public static function draw( array:Array, graphics:Graphics ):void { if( array.length < 3 )return; var i:int; var j:int; var L:int = array.length; var t:Triangle; var tt:Triangle; //retrieves the triangles' centers and midSides points for ( i = 0; i < L; i++ ) { t = array[ i ] as Triangle; t.getCenter(); } //compares each triangle to its three neighbours L = array.length; graphics.lineStyle( .5, 0xFF6600 ); for ( i = 0; i < L; i++ ) { t = array[ i ] as Triangle; for ( j = 0; j < L; j++ ) { tt = array[ j ] as Triangle; // if they have 2 points in common if( ( t.p1.Equals2D(tt.p1) || t.p1.Equals2D(tt.p2) || t.p1.Equals2D(tt.p3) ) && ( ( t.p2.Equals2D(tt.p1) || t.p2.Equals2D(tt.p2) || t.p2.Equals2D(tt.p3) ) || ( t.p3.Equals2D(tt.p1) || t.p3.Equals2D(tt.p2) || t.p3.Equals2D(tt.p3) ) ) ) { //then join the centers graphics.moveTo(t.center.x, t.center.y); graphics.lineTo(tt.center.x, tt.center.y); } } } } } }