⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plotinterval.java

📁 FuncPlotter is a combined Java application and applet for displaying two-dimensional plots of explic
💻 JAVA
字号:
/*====================================================================*\PlotInterval.javaPlot interval class.------------------------------------------------------------------------This file is part of FuncPlotter, a combined Java application and appletfor plotting explicit functions in one variable.Copyright 2005-2007 Andy Morgan-Richards.FuncPlotter is free software: you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation, either version 3 of the License, or (at youroption) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public License alongwith this program.  If not, see <http://www.gnu.org/licenses/>.\*====================================================================*/// IMPORTSimport java.math.BigDecimal;import util.IntegerRange;//----------------------------------------------------------------------// PLOT INTERVAL CLASSclass PlotInterval{//////////////////////////////////////////////////////////////////////////  Constants////////////////////////////////////////////////////////////////////////    public static final     double  MIN_VALUE   = -1.0E100;    public static final     double  MAX_VALUE   = -MIN_VALUE;    public static final     int MAX_NUM_SIGNIFICANT_DIGITS  = 12;    private static final    String  DEFAULT_LOWER_EP_STR    = "-1.0";    private static final    String  DEFAULT_UPPER_EP_STR    = "1.0";    private static final    double  ONE_OVER_LOG_10 = 1.0 / Math.log( 10.0 );    private static final    String  ZERO_STR                    = "0";    private static final    String  FIXED_ZERO_STR              = "0.0";    private static final    String  FIXED_MINUS_ZERO_STR        = "-0.0";    private static final    String  SCIENTIFIC_ZERO_STR         = "0.0E0";    private static final    String  SCIENTIFIC_MINUS_ZERO_STR   = "-0.0E0";//////////////////////////////////////////////////////////////////////////  Constructors////////////////////////////////////////////////////////////////////////    public PlotInterval( )    {        this( DEFAULT_LOWER_EP_STR, DEFAULT_UPPER_EP_STR );    }    //------------------------------------------------------------------    public PlotInterval( double lowerEndpoint,                         double upperEndpoint )    {        this( Double.toString( lowerEndpoint ), Double.toString( upperEndpoint ) );    }    //------------------------------------------------------------------    public PlotInterval( BigDecimal lowerEndpoint,                         BigDecimal upperEndpoint )    {        this.lowerEndpoint = lowerEndpoint;        this.upperEndpoint = upperEndpoint;        this.lowerEndpoint = new BigDecimal( getLowerEndpointString( ) );        this.upperEndpoint = new BigDecimal( getUpperEndpointString( ) );    }    //------------------------------------------------------------------    public PlotInterval( String lowerEndpointStr,                         String upperEndpointStr )        throws NumberFormatException    {        this( new BigDecimal( lowerEndpointStr ), new BigDecimal( upperEndpointStr ) );    }    //------------------------------------------------------------------    public PlotInterval( PlotInterval interval )    {        this( interval.lowerEndpoint, interval.upperEndpoint );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Class methods////////////////////////////////////////////////////////////////////////    public static boolean hasTooManySignificantDigits( String str )    {        str = str.toUpperCase( );        int endIndex = str.indexOf( 'E' );        if ( endIndex < 0 )            endIndex = str.length( );        int pointIndex = str.indexOf( '.' );        if ( (pointIndex >= 0) && (pointIndex < endIndex) )        {            int i = pointIndex;            while ( ++i < endIndex )            {                if ( str.charAt( i ) != '0' )                    break;            }            if ( i == endIndex )                endIndex = pointIndex;        }        int numDigits = 0;        for ( int i = 0; i < endIndex; ++i )        {            char ch = str.charAt( i );            if ( (ch == '0') && (numDigits == 0) )                continue;            if ( (ch >= '0') && (ch <= '9') )                ++numDigits;        }        return ( numDigits > MAX_NUM_SIGNIFICANT_DIGITS );    }    //------------------------------------------------------------------    public static String doubleToString( double       value,                                         int          numFractionDigits,                                         IntegerRange exponentRange,                                         boolean      applyFixedExponent,                                         int          fixedExponent )    {        final   int EXPONENT_OFFSET = MAX_NUM_SIGNIFICANT_DIGITS + 1;        // Handle zero as special case        if ( value == 0.0 )            return FIXED_ZERO_STR;        // Set flag for negative value; make value absolute        boolean negative = (value < 0.0);        if ( negative )            value = -value;        // Get significand as string        int exponent = (int)Math.floor( Math.log( value ) * ONE_OVER_LOG_10 );        String sigStr = Long.toString( (long)(value * Math.pow( 10.0, EXPONENT_OFFSET - exponent )) );        // Normalise radix point and exponent        exponent += sigStr.length( ) - EXPONENT_OFFSET;        int pointIndex = 0;        while ( pointIndex < sigStr.length( ) )        {            --exponent;            if ( sigStr.charAt( pointIndex++ ) != '0' )                break;        }        // Apply fixed exponent        if ( applyFixedExponent )        {            pointIndex += exponent - fixedExponent;            exponent = fixedExponent;        }        // Remove exponent if it is within fixed-point range        if ( exponentRange.contains( exponent ) )        {            pointIndex += exponent;            exponent = 0;        }        // Create string of digits in buffer, padding with zeros as required        StringBuilder buffer = new StringBuilder( MAX_NUM_SIGNIFICANT_DIGITS << 1 );        if ( pointIndex < 0 )        {            buffer.append( Util.createCharArray( '0', -pointIndex ) );            pointIndex = 0;        }        buffer.append( sigStr );        int maxLength = Math.min( pointIndex + numFractionDigits, buffer.length( ) - 2 );        if ( pointIndex > buffer.length( ) )            buffer.append( Util.createCharArray( '0', pointIndex - buffer.length( ) ) );        // Round up value and remove extraneous digits        int i = maxLength;        if ( buffer.charAt( i ) >= '5' )        {            while ( --i >= 0 )            {                char ch = buffer.charAt( i );                if ( ch == '9' )                    buffer.setCharAt( i, '0' );                else                {                    buffer.setCharAt( i, ++ch );                    break;                }            }            if ( i < 0 )            {                buffer.insert( 0, '1' );                if ( applyFixedExponent )                    ++pointIndex;                else                {                    if ( exponentRange.contains( ++exponent ) )                    {                        pointIndex += exponent;                        exponent = 0;                    }                }            }        }        buffer.setLength( maxLength );        // Insert radix point        if ( pointIndex < 0 )        {            buffer.insert( 0, Util.createCharArray( '0', -pointIndex ) );            pointIndex = 0;        }        if ( pointIndex > buffer.length( ) )            buffer.append( Util.createCharArray( '0', pointIndex - buffer.length( ) ) );        buffer.insert( pointIndex, '.' );        // Strip leading zeros        i = 0;        while ( buffer.charAt( i ) == '0' )            ++i;        if ( buffer.charAt( i ) == '.' )        {            if ( i == 0 )                buffer.insert( 0, '0' );            else                --i;        }        buffer.delete( 0, i );        // Strip trailing zeros        i = buffer.length( );        while ( buffer.charAt( --i ) == '0' )        {            // do nothing        }        buffer.setLength( ++i );        if ( buffer.charAt( i - 1 ) == '.' )            buffer.append( '0' );        // Insert minus sign at front of negative value        if ( negative )            buffer.insert( 0, '-' );        // Append exponent        if ( exponent != 0 )        {            buffer.append( 'E' );            buffer.append( Integer.toString( exponent ) );        }        // Return string        String str = buffer.toString( );        if ( str.equals( FIXED_MINUS_ZERO_STR ) || str.equals( SCIENTIFIC_ZERO_STR ) ||             str.equals( SCIENTIFIC_MINUS_ZERO_STR ) )            str = FIXED_ZERO_STR;        return str;    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods : overriding methods////////////////////////////////////////////////////////////////////////    public String toString( )    {        return new String( lowerEndpoint + ", " + upperEndpoint );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods////////////////////////////////////////////////////////////////////////    public double getLowerEndpoint( )    {        return lowerEndpoint.doubleValue( );    }    //------------------------------------------------------------------    public double getUpperEndpoint( )    {        return upperEndpoint.doubleValue( );    }    //------------------------------------------------------------------    public String getLowerEndpointString( )    {        return coordToString( getLowerEndpoint( ) );    }    //------------------------------------------------------------------    public String getUpperEndpointString( )    {        return coordToString( getUpperEndpoint( ) );    }    //------------------------------------------------------------------    public double getInterval( )    {        return ( upperEndpoint.doubleValue( ) - lowerEndpoint.doubleValue( ) );    }    //------------------------------------------------------------------    public double getHalfInterval( )    {        return ( 0.5 * (upperEndpoint.doubleValue( ) - lowerEndpoint.doubleValue( )) );    }    //------------------------------------------------------------------    public double getMedian( )    {        return ( 0.5 * (upperEndpoint.doubleValue( ) + lowerEndpoint.doubleValue( )) );    }    //------------------------------------------------------------------    public boolean isValid( )    {        double dLowerEndpoint = getLowerEndpoint( );        double dUpperEndpoint = getUpperEndpoint( );        return ( (dLowerEndpoint >= MIN_VALUE) && (dLowerEndpoint <= MAX_VALUE) &&                 (dUpperEndpoint >= MIN_VALUE) && (dUpperEndpoint <= MAX_VALUE) &&                 (dLowerEndpoint < dUpperEndpoint) );    }    //------------------------------------------------------------------    public boolean equals( PlotInterval interval )    {        return ( (interval != null) && lowerEndpoint.equals( interval.lowerEndpoint ) &&                 upperEndpoint.equals( interval.upperEndpoint ) );    }    //------------------------------------------------------------------    public String coordToString( double value )    {        AppConfig config = AppConfig.getInstance( );        return doubleToString( value, config.getPlotNumFractionDigits( ),                               config.getPlotFixedPointExponentRange( ),                               !config.isPlotNormaliseScientificNotation( ), getExponent( ) );    }    //------------------------------------------------------------------    public int getExponent( )    {        double absEp = Math.min( Math.abs( getLowerEndpoint( ) ), Math.abs( getUpperEndpoint( ) ) );        int exponent = (absEp == 0.0) ? 0 : (int)Math.floor( Math.log( absEp ) * ONE_OVER_LOG_10 );        if ( absEp * Math.pow( 10.0, -exponent ) >= 10.0 )            ++exponent;        return exponent;    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance variables////////////////////////////////////////////////////////////////////////    private BigDecimal  lowerEndpoint;    private BigDecimal  upperEndpoint;}//----------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -