package  
{
 
    import flash.events.Event;
    
    import org.papervision3d.core.geom.Lines3D;
    import org.papervision3d.core.geom.renderables.Line3D;
    import org.papervision3d.core.geom.renderables.Vertex3D;
    import org.papervision3d.materials.ColorMaterial;
    import org.papervision3d.materials.special.LineMaterial;
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.objects.primitives.Sphere;
    import org.papervision3d.view.BasicView;
 
     [SWF(backgroundColor="#000000",width="425",height="350")]
    public class Line3DChart extends BasicView 
    {
        public var chartContainer:DisplayObject3D;
        public var chart:DisplayObject3D;
        private var dataPoints : Array;
             
         private static const RADIUS : int = 20;
         private static const MAX_SIZE : int = 600; 
         private static const AXIS_SIZE : int = 800; 
 
        public function Line3DChart()
        {
            super(stage.stageWidth, stage.stageHeight, false, true);
            init(); 
        }
 
        private function init():void
        {
 
            chartContainer = new DisplayObject3D("Chart Container");
            chart = new DisplayObject3D("Chart");
            
            chartContainer.rotationY=-15;
            chartContainer.rotationX=-15;
            dataPoints = new Array(); 
 
             var defaultMaterial : LineMaterial = new LineMaterial(0xFFFFFF, .1);
             var xAxisMaterial : LineMaterial = new LineMaterial( 0xFF0000, .75 );
             var yAxisMaterial : LineMaterial = new LineMaterial( 0x00FF00, .75 );
             var zAxisMaterial : LineMaterial = new LineMaterial( 0x0000FF, .75 );
             var dataMaterial : ColorMaterial = new ColorMaterial( 0xFFFFFF, .65, false );
         
             var axes : Lines3D = new Lines3D( defaultMaterial, "Axes" );
             axes.addLine( new Line3D( axes, xAxisMaterial, 2, new Vertex3D( -AXIS_SIZE,0,0 ), new Vertex3D( AXIS_SIZE,0,0 ) ));
             axes.addLine( new Line3D( axes, yAxisMaterial, 2, new Vertex3D( 0,-AXIS_SIZE,0 ), new Vertex3D( 0,AXIS_SIZE,0 ) ));
             axes.addLine( new Line3D( axes, zAxisMaterial, 2, new Vertex3D( 0,0,-AXIS_SIZE ), new Vertex3D( 0,0,AXIS_SIZE ) ));
             
             var lines : Lines3D = new Lines3D( defaultMaterial, "Lines" );
             
             chart.addChild( axes );
             chart.addChild( lines );
             
             var lastVertex : Vertex3D;
             var currentVertex : Vertex3D = new Vertex3D();
             
            for(var i:int = 0; i<50; i++)
            {
                lastVertex = currentVertex;
                currentVertex = new Vertex3D();
                
                currentVertex.x = Math.random() * MAX_SIZE * positiveOrNegative();
                currentVertex.y = Math.random() * MAX_SIZE * positiveOrNegative();
                currentVertex.z = Math.random() * MAX_SIZE * positiveOrNegative();
                
                var point : Sphere = new Sphere( dataMaterial, RADIUS );
                point.x = currentVertex.x;
                point.y = currentVertex.y;
                point.z = currentVertex.z;
                
                chart.addChild( point ); 
                
                var line : Line3D = new Line3D( lines, defaultMaterial, 3, lastVertex, currentVertex );
                lines.addLine( line );
            }
 
             chartContainer.addChild(chart);
            scene.addChild(chartContainer); 
 
            singleRender(); 
 
            addEventListener(Event.ENTER_FRAME, onEnterFrame); 
        }
 
        private function onEnterFrame(e:Event): void 
        {
            chart.rotationY+=1; 
            singleRender(); 
        }
        
        private function positiveOrNegative() : int
        {
            var seed : Number = Math.random() * 100;
            if ( seed > 50 )
                return 1;
            else 
                return -1;
        }
    }
}