/*---------------------------------------------------*
        Hailstone.java

        Plots Hailstone numbers from 1 to n

        Rolf Muertter, September 27, 2006
 *---------------------------------------------------*/

import ptolemy.plot.*;    //  Import PtPlot


public class Hailstone
{   
        public static void main( String[] args )
        {  
                int  n = 300;

                // Plot Hailstone numbers from 1 to n
                plot_hailstone( n );     

                System.out.println( "Plot of Hailstone numbers from 1 to " + n + ".\n" );
        } 
 
        public static void  hailstone( int a, int i, Plot hsPlot )
        { 
                if( a == 1 )  return;

                if( a%2 == 1 )   a = 3*a + 1 ;
                else             a = a/2 ;

                hsPlot.addPoint( 0, i, a, false ); 
                hailstone( a, i, hsPlot );

                return;
        }
    
        public static void plot_hailstone( int n )       //  plot hailstone(i) using PtPlot
        { 
                Plot hsPlot = new Plot();                   // Create plot

                hsPlot.setTitle( "hailstone(n) vs n" );     // Set title
                hsPlot.setXLabel( "n" );                    // X axis label
                hsPlot.setYLabel( "hailstone(n)" );         // Y axis label
                hsPlot.setYRange( 0, 200 );                 // Vertical range
                hsPlot.setYLog( true );                     // Y axis log scale
                hsPlot.setMarksStyle( "dots", 0 );          // Use dots

                for( int i = 2; i < n; i++ )
                { 
                        hsPlot.addPoint( 0, i, i, false ); 
                        hailstone( i, i, hsPlot );
                }

                PlotApplication app = new PlotApplication( hsPlot );    // Display Plot
        }   
}