// File: Java_ACM_misc.java // // Author: Robert W. Lindeman, with help from Rahul Simha. // Created: 10/31/2001 // // A package of useful I/O functions for the ACM. // See the main function below for examples of use. import java.io.*; import java.util.*; import java.text.*; import java.lang.Math.*; public class Acm_misc { public static double M_PI = 3.14159265358979323846; public static boolean numeric( char c ) { return( (c) >= '0' && (c) <= '9' ); } public static double abs( double x ) { return( (x) < 0 ? -(x) : (x) ); } public static double min( double a, double b ) { return( (a) < (b) ? (a) : (b) ); } public static double max( double a, double b ) { return( (a) > (b) ? (a) : (b) ); } public static double deg( double rad ) /* Rad to Deg. */ { return( ( (rad) * 180.0 ) / (Acm_misc.M_PI) ); } public static double rad( double deg ) /* Deg to Rad. */ { return( ( (deg) / 180.0 ) * (Acm_misc.M_PI) ); } public static double dist2d( double x1, double y1, double x2, double y2 ) { return( Math.sqrt( ( ( (x1) - (x2) ) * ( (x1) - (x2) ) ) + ( ( (y1) - (y2) ) * ( (y1) - (y2) ) ) ) ); } public static double dist3d( double x1, double y1, double z1, double x2, double y2, double z2 ) { return( Math.sqrt( ( ( (x1) - (x2) ) * ( (x1) - (x2) ) ) + ( ( (y1) - (y2) ) * ( (y1) - (y2) ) ) + ( ( (z1) - (z2) ) * ( (z1) - (z2) ) ) ) ); } // Bit Tests: Returns 'true' if bit 'b' is set (clear) in integer 'i'. public static boolean bit_set( int i, int b ) { return( ( ( (i) & ( 1 << (b) ) ) >> (b) ) != 0 ); } public static boolean bit_clear( int i, int b ) { return( (!bit_set( (i), (b) )) ); } // Bit Sets: Returns 'i' with bit 'b' set (clear). public static int set_bit( int i, int b ) { return( ( (i) | ( 1 << (b) ) ) ); } public static int clear_bit( int i, int b ) { return( ( (i) & ~( 1 << (b) ) ) ); } public static int toggle_bit( int i, int b ) { return( ( (i) ^ ( 1 << (b) ) ) ); } public static void main( String[] argv ) { System.out.println( "Testing macros..." ); System.out.println( " abs( -100 ): " + Acm_misc.abs( -100 ) ); System.out.println( " min( 50.1, 100 ): " + Acm_misc.min( 50.1, 100 ) ); System.out.println( " max( 50.1, 50.2 ): " + Acm_misc.max( 50.1, 50.2 ) ); System.out.println( " deg( " + Acm_misc.M_PI + " ): " + Acm_misc.deg( Acm_misc.M_PI ) ); System.out.println( " rad( 180.0 ): " + Acm_misc.rad( 180.0 ) ); System.out.println( " Dist2d (should be 5): " + Acm_misc.dist2d( 0.0, 0.0, 4.0, 3.0 ) ); System.out.println( " Dist3d (should be 5): " + Acm_misc.dist3d( 0.0, 0.0, 0.0, 4.0, 3.0, 0.0 ) ); System.out.println( " Acm_misc.bit_set( 3, 0 ) (should be true): " + Acm_misc.bit_set( 3, 0 ) ); System.out.println( " Acm_misc.bit_set( 2, 1 ) (should be true): " + Acm_misc.bit_set( 2, 1 ) ); System.out.println( " Acm_misc.bit_set( 54, 0 ) (should be true): " + Acm_misc.bit_set( 54, 0 ) ); System.out.println( " Acm_misc.bit_set( Acm_misc.set_bit( 0, 4 ), 4 ) (should be true): " + Acm_misc.bit_set( Acm_misc.set_bit( 0, 4 ), 4 ) ); System.out.println( " Acm_misc.bit_set( Acm_misc.clear_bit( 0, 4 ), 4 ) (should be false): " + Acm_misc.bit_set( Acm_misc.clear_bit( 0, 4 ), 4 ) ); System.out.println( " Acm_misc.bit_set( Acm_misc.toggle_bit( 6, 2 ), 2 ) (should be false): " + Acm_misc.bit_set( Acm_misc.toggle_bit( 6, 2 ), 2 ) ); System.out.println( ); } } // End of class "Acm_misc"