TeeChartPHP
[ class tree: TeeChartPHP ] [ index: TeeChartPHP ] [ all elements ]

Source for file MathUtils.php

Documentation is available at MathUtils.php

  1. <?php
  2.  
  3. /**
  4.  * MathUtils class
  5.  *
  6.  * Description: Math utility procedures
  7.  *
  8.  * @author
  9.  * @copyright (c) 1995-2008 by Steema Software SL. All Rights Reserved. <info@steema.com>
  10.  * @version 1.0
  11.  * @package TeeChartPHP
  12.  * @subpackage misc
  13.  * @link http://www.steema.com
  14.  */
  15.  
  16. class MathUtils {
  17.  
  18.     /*static*/ public/*final double*/ $PISTEP M_PI// / 180;
  19.  
  20.     // Interceptors
  21.     function __get$property {
  22.       $method ="get{$property}";
  23.       if method_exists$this$method ) ) {
  24.         return $this->$method();
  25.       }
  26.     }
  27.  
  28.     function __set $property,$value {
  29.       $method ="set{$property}";
  30.       if method_exists$this$method ) ) {
  31.         return $this->$method($value);
  32.       }
  33.     }
  34.  
  35.     /**
  36.      * A constant holding the positive infinity of type
  37.      * <code>double</code>. It is equal to the value returned by
  38.      * <code>Double.longBitsToDouble(0x7ff0000000000000L)</code>.
  39.      */
  40. //    public static /*final*/ $POSITIVE_INFINITY = 1.0 / 0.0;
  41.  
  42.     /**
  43.      * A constant holding the negative infinity of type
  44.      * <code>double</code>. It is equal to the value returned by
  45.      * <code>Double.longBitsToDouble(0xfff0000000000000L)</code>.
  46.      */
  47. //    public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
  48.  
  49.     /**
  50.      * A constant holding a Not-a-Number (NaN) value of type
  51.      * <code>double</code>. It is equivalent to the value returned by
  52.      * <code>Double.longBitsToDouble(0x7ff8000000000000L)</code>.
  53.      */
  54. //    public static final double NaN = 0.0d / 0.0;
  55.  
  56.     /**
  57.      * A constant holding the largest positive finite value of type
  58.      * <code>double</code>,
  59.      * (2-2<sup>-52</sup>)&middot;2<sup>1023</sup>.  It is equal to
  60.      * the hexadecimal floating-point literal
  61.      * <code>0x1.fffffffffffffP+1023</code> and also equal to
  62.      * <code>Double.longBitsToDouble(0x7fefffffffffffffL)</code>.
  63.      */
  64. //    public static final double MAX_VALUE = 1.7976931348623157e+308; // 0x1.fffffffffffffP+1023
  65.  
  66.     /**
  67.      * A constant holding the smallest positive nonzero value of type
  68.      * <code>double</code>, 2<sup>-1074</sup>. It is equal to the
  69.      * hexadecimal floating-point literal
  70.      * <code>0x0.0000000000001P-1022</code> and also equal to
  71.      * <code>Double.longBitsToDouble(0x1L)</code>.
  72.      */
  73. //    public static final double MIN_VALUE = 4.9e-324; // 0x0.0000000000001P-1022
  74.  
  75.  
  76.  
  77.  
  78.     static public function getPiStep ({
  79.         return M_PI 180;
  80.     }
  81.  
  82.     static public function /*final double*/ sqr(/*double*/ $x{
  83.         return $x $x;
  84.     }
  85.  
  86.     /**
  87.      * Note: This function has been added to MathUtils because J2ME Math
  88.      * class does not include it.
  89.      *
  90.      * Returns Math.log of value parameter.
  91.      *
  92.      * @param value double
  93.      * @return double 
  94.      */
  95.     public static function /*double*/ log(/*double*/ $value{
  96.         return log($value);
  97.     }
  98.  
  99.     static public function _log($x$numBase{
  100.         return log($xlog($numBase);
  101.     }
  102.  
  103.     // Note: This function is implemented here instead of using Math.round
  104.     // because J2ME Math class does not include it (as far as in CLDC 1.1)
  105.  
  106.     /**
  107.      * Returns the integer nearest to "value" parameter
  108.      *
  109.      * @param value double
  110.      * @return int 
  111.      */
  112.     public static function round($value{
  113.         return /*(int)*/ floor($value 0.5)// before 0.5d);
  114.     }
  115.  
  116.     /**
  117.      * Returns TeePoint
  118.      *
  119.      * @param value TeePoint
  120.      * @param value TeePoint
  121.      * @param value int
  122.      * @return TeePoint 
  123.      */
  124.     static public function pointAtDistance($aFrom$aTo$aDist{
  125.  
  126.         $res new TeePoint($aTo->x$aTo->y);
  127.  
  128.         if ($aFrom.!= $aTo.x{
  129.             $tmp MathUtils::atan2(($aTo->getY($aFrom->getY())($aTo->getX($aFrom->getX()));
  130.  
  131.             $tmpSin sin($tmp);
  132.             $tmpCos cos($tmp);
  133.             $res->-= self::round($aDist $tmpCos);
  134.             $res->-= self::round($aDist $tmpSin);
  135.         else {
  136.             if ($aTo->getY($aFrom->getY()) {
  137.                 $res->setY($res->getY($aDist);
  138.             else {
  139.                 $res->setY($res->getY($aDist);
  140.             }
  141.         }
  142.  
  143.         return $res;
  144.     }
  145.  
  146.     static public function /*double*/ calcDistance(/*Point/* $p, /*int*/ $x0/*int*/ $y0/*int*/ $x1/*int*/ $y1{
  147.  
  148. //        /*double*/ dx, dy;
  149.         $dx=0;
  150.         $dy=0;
  151.  
  152.         if (($x1 == $x0&& ($y1 == $y0)) {
  153.  
  154.             $dx $p.$x0;
  155.             $dy $p.$y0;
  156.  
  157.             return sqrt($dx $dx $dy $dy);
  158.  
  159.         else {
  160.  
  161.             $dx $x1 $x0;
  162.             $dy $y1 $y0;
  163.  
  164.             /*double*/ $tmpResult=0;
  165.             $tmpResult (($p.$x0$dx ($p.$y0$dy/
  166.                         ($dx $dx $dy $dy);
  167.  
  168.             if ($tmpResult 0{
  169.                 $dx $p.$x0;
  170.                 $dy $p.$y0;
  171.             else if ($tmpResult 1{
  172.                 $dx $p.$x1;
  173.                 $dy $p.$y1;
  174.             else {
  175.                 $dx $p.($x0 $tmpResult $dx);
  176.                 $dy $p.($y0 $tmpResult $dy);
  177.             }
  178.  
  179.             return sqrt($dx $dx $dy $dy);
  180.         }
  181.     }
  182.  
  183.     static public function /*boolean*/ pointInLineTolerance(/*Point*/ $p/*int*/ $x0/*int*/ $y0/*int*/ $x1,
  184.                                                /*int*/ $y1/*int*/ $tolerance{
  185.         if ((($p.== $x0&& ($p.== $y0)) || (($p.== $x1&& ($p.== $y1))) {
  186.             return true;
  187.         else {
  188.             return abs(calcDistance($p$x0$y0$x1$y1)) <= $tolerance++;
  189.         }
  190.     }
  191.  
  192.     /**  TODO REMOVE THIS FUNCTION BCOS BCCCOMP IS USED INSTEAD
  193.      * Compares Double d1 with d2. Returns 0 if both are equal.
  194.      * Returns -1 if d1 is lower than d2, and 1 if d1 is bigger than d2.
  195.      *
  196.      * @param d1 double
  197.      * @param d2 double
  198.      * @return int 
  199.      */
  200.     public static function compareDoubles($d1$d2{
  201.         if ($d1 $d2{
  202.             return -1// Neither val is NaN, thisVal is smaller
  203.         }
  204.         if ($d1 $d2{
  205.             return 1// Neither val is NaN, thisVal is larger
  206.         }
  207.  
  208.  
  209.         /*long*/ $thisBits Double.doubleToLongBits($d1);
  210.         /*long*/ $anotherBits Double.doubleToLongBits($d2);
  211.  
  212.         return ($thisBits == $anotherBits // Values are equal
  213.                 ($thisBits $anotherBits ? -// (-0.0, 0.0) or (!NaN, NaN)
  214.                  1))// (0.0, -0.0) or (NaN, !NaN)
  215.     }
  216.  
  217.     /**
  218.      * Note: This method has been implemented here in MathUtils class
  219.      * because J2ME Math class does not include it.
  220.      *
  221.      * Returns Math.atan2.
  222.      *
  223.      * @param double
  224.      * @param double
  225.      * @return double 
  226.      */
  227.     public static function atan2($y$x{
  228.        return atan2($y,$x);
  229.     }
  230.  
  231.     /**
  232.      * Note: This method has been implemented here in MathUtils class
  233.      * because J2ME Math class does not include it.
  234.      *
  235.      * Returns Math.pow.
  236.      *
  237.      * @param double
  238.      * @param double
  239.      * @return double 
  240.      */
  241.     public static function pow($a$b{
  242.         return pow($a,$b);
  243.     }
  244.  
  245.     /**
  246.      * Note: This method has been implemented here in MathUtils class
  247.      * because J2ME Math class does not include it.
  248.      *
  249.      * Returns Math.exp.
  250.      *
  251.      * @param double
  252.      * @return double 
  253.      */
  254.     public static function exp($a{
  255.         return exp($a);
  256.     }
  257. }
  258. ?>

Documentation generated on Wed, 16 Jun 2010 12:07:03 +0200 by phpDocumentor 1.4.1