home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Coordinates.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  4.0 KB  |  128 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net>                |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Coordinates.php,v 1.3 2003/01/04 11:56:25 mj Exp $
  20. //
  21.  
  22. /**
  23.  * Utility class for defining 3D coordinates and
  24.  * its associated distance() method
  25.  *
  26.  * @author  Jesus M. Castagnetto <jmcastagnetto@php.net>
  27.  * @version 1.0
  28.  * @access  public
  29.  * @package Science_Chemistry
  30.  */
  31. class Science_Chemistry_Coordinates {
  32.  
  33.     /**
  34.      * Array of tridimensional coordinates: (x, y, z)
  35.      * 
  36.      * @var     array
  37.      * @access  private
  38.      */
  39.     var $coords;
  40.     
  41.     /**
  42.      * Constructor for the class, returns null if parameter is
  43.      * not an array with 3 entries
  44.      *
  45.      * @param   array   $coords array of three floats (x, y, z)
  46.      * @return  object  Science_Chemistry_Coordinates
  47.      * @access  public
  48.      */
  49.     function Science_Chemistry_Coordinates($coords) {
  50.         if (is_array($coords) && count($coords) == 3)
  51.             $this->coords = $coords;
  52.         else
  53.             return null;
  54.     }
  55.     
  56.     /**
  57.      * Castesian distance calculation method
  58.      *
  59.      * @param   object  Science_Chemistry_Coordinates $coord
  60.      * @return  float   distance
  61.      * @access  public
  62.      */
  63.     function distance($coord) {
  64.         if (Science_Chemistry_Coordinates::areCoordinates($coord)) {
  65.             $xyz2 = $coord->getCoordinates();
  66.             for ($i=0; $i<count($xyz2); $i++)
  67.                 $sum2 += pow(($xyz2[$i] - $this->coords[$i]),2);
  68.             return sqrt($sum2);
  69.         }
  70.     }
  71.  
  72.     /**
  73.      * Checks if the object is an instance of Science_Chemistry_Coordinates
  74.      * 
  75.      * @param   object  Science_Chemistry_Coordinates    $obj
  76.      * @return  boolean
  77.      * @access  public
  78.      */
  79.     function areCoordinates($obj) {
  80.         return  ( is_object($obj) && 
  81.                  (strtolower(get_class($obj)) == strtolower("Science_Chemistry_Coordinates")
  82.                   || is_subclass_of($obj, strtolower("Science_Chemistry_Coordinates")))
  83.                 );
  84.     }
  85.  
  86.     /**
  87.      * Returns the array of coordinates
  88.      *
  89.      * @return  array   array (x, y, z)
  90.      * @access  public
  91.      */
  92.     function getCoordinates() {
  93.         if (is_array($this->coords) && !empty($this->coords))
  94.             return $this->coords;
  95.     }
  96.  
  97.     /**
  98.      * Returns a string representation of the coordinates: x y z
  99.      *
  100.      * @return  string 
  101.      * @access  public
  102.      */
  103.     function toString() {
  104.         for ($i=0; $i<count($this->coords); $i++)
  105.             $tmp[$i] = sprintf("%10.4f",$this->coords[$i]);
  106.         return implode(" ",$tmp);
  107.     }
  108.  
  109.     /**
  110.      * Returns a CML representation of the coordinates
  111.      *
  112.      * @return  string
  113.      * @access  public
  114.      */
  115.     function toCML() {
  116.         $out = "<coordinate3 builtin=\"xyz3\">";
  117.         $tmp = array();
  118.         for ($i=0; $i < count($this->coords); $i++)
  119.             $tmp[] = trim(sprintf("%10.4f", $this->coords[$i]));
  120.         $out .= implode(" ",$tmp)."</coordinate3>\n";
  121.         return $out;
  122.     }
  123.  
  124. } // end of class Science_Chemistry_Coordinates
  125.  
  126. // vim: expandtab: ts=4: sw=4
  127. ?>
  128.