[ Index ]
 

Code source de Cr@wltr@ck 2.2.1

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/graphs/artichow/php5/inc/ -> Math.class.php (source)

   1  <?php
   2  /*
   3   * This work is hereby released into the Public Domain.
   4   * To view a copy of the public domain dedication,
   5   * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
   6   * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
   7   *
   8   */
   9   
  10  require_once dirname(__FILE__)."/../Graph.class.php";
  11  
  12  abstract class awShape {
  13      
  14      /**
  15       * Is the shape hidden ?
  16       *
  17       * @var bool
  18       */
  19      protected $hide = FALSE;
  20      
  21      /**
  22       * Shape style
  23       *
  24       * @var int
  25       */
  26      public $style;
  27      
  28      /**
  29       * Shape thickness
  30       *
  31       * @var int
  32       */
  33      public $thickness;
  34      
  35      /**
  36       * Solid shape
  37       *
  38       * @var int
  39       */
  40      const SOLID = 1;
  41      
  42      /**
  43       * Dotted shape
  44       *
  45       * @var int
  46       */
  47      const DOTTED = 2;
  48      
  49      /**
  50       * Dashed shape
  51       *
  52       * @var int
  53       */
  54      const DASHED = 3;
  55      
  56      /**
  57       * Change shape style
  58       *
  59       * @param int $style Line style
  60       */
  61  	public function setStyle($style) {
  62          $this->style = (int)$style;
  63      }
  64      
  65      /**
  66       * Return shape style
  67       *
  68       * @return int
  69       */
  70  	public function getStyle() {
  71          return $this->style;
  72      }
  73      
  74      /**
  75       * Change shape thickness
  76       *
  77       * @param int $thickness Shape thickness in pixels
  78       */
  79  	public function setThickness($thickness) {
  80          $this->thickness = (int)$thickness;
  81      }
  82      
  83      /**
  84       * Return shape thickness
  85       *
  86       * @return int
  87       */
  88  	public function getThickness() {
  89          return $this->thickness;
  90      }
  91      
  92      /**
  93       * Hide the shape
  94       *
  95       * @param bool $hide
  96       */
  97  	public function hide($hide) {
  98          $this->hide = (bool)$hide;
  99      }
 100      
 101      /**
 102       * Show the shape
 103       *
 104       * @param bool $shape
 105       */
 106  	public function show($shape) {
 107          $this->hide = (bool)!$shape;
 108      }
 109      
 110      /**
 111       * Is the line hidden ?
 112       *
 113       * @return bool
 114       */
 115  	public function isHidden() {
 116          return $this->hide;
 117      }
 118      
 119  }
 120  
 121  registerClass('Shape', TRUE);
 122  
 123  /**
 124   * Describe a point
 125   *
 126   * @package Artichow
 127   */
 128  class awPoint extends awShape {
 129  
 130      /**
 131       * X coord
 132       *
 133       * @var float
 134       */
 135      public $x;
 136  
 137      /**
 138       * Y coord
 139       *
 140       * @var float
 141       */
 142      public $y;
 143      
 144      /**
 145       * Build a new awpoint
 146       *
 147       * @param float $x
 148       * @param float $y
 149       */
 150  	public function __construct($x, $y) {
 151      
 152          $this->setLocation($x, $y);
 153          
 154      }
 155      
 156      /**
 157       * Change X value
 158       *
 159       * @param float $x
 160       */
 161  	public function setX($x) {
 162          $this->x = (float)$x;
 163      }
 164      
 165      /**
 166       * Change Y value
 167       *
 168       * @param float $y
 169       */
 170  	public function setY($y) {
 171          $this->y = (float)$y;
 172      }
 173      
 174      /**
 175       * Change point location
 176       *
 177       * @param float $x
 178       * @param float $y
 179       */
 180  	public function setLocation($x, $y) {
 181          $this->setX($x);
 182          $this->setY($y);
 183      }
 184      
 185      /**
 186       * Get point location
 187       *
 188       * @param array Point location
 189       */
 190  	public function getLocation() {
 191          return array($this->x, $this->y);
 192      }
 193      
 194      /**
 195       * Get distance to another point
 196       *
 197       * @param awPoint $p A point
 198       * @return float
 199       */
 200  	public function getDistance(awPoint $p) {
 201      
 202          return sqrt(pow($p->x - $this->x, 2) + pow($p->y - $this->y, 2));
 203      
 204      }
 205      
 206      /**
 207       * Move the point to another location
 208       *
 209       * @param Point A Point with the new awlocation
 210       */
 211  	public function move($x, $y) {
 212      
 213          return new awPoint(
 214              $this->x + $x,
 215              $this->y + $y
 216          );
 217          
 218      }
 219  
 220  }
 221  
 222  registerClass('Point');
 223   
 224  /* <php4> */
 225  
 226  define("LINE_SOLID", 1);
 227  define("LINE_DOTTED", 2);
 228  define("LINE_DASHED", 3);
 229  
 230  /* </php4> */
 231  
 232  /**
 233   * Describe a line
 234   *
 235   * @package Artichow
 236   */
 237  class awLine extends awShape {
 238  
 239      /**
 240       * Line first point
 241       *
 242       * @param Point
 243       */
 244      public $p1;
 245  
 246      /**
 247       * Line second point
 248       *
 249       * @param Point
 250       */
 251      public $p2;
 252      
 253      /**
 254       * Build a new awline
 255       *
 256       * @param awPoint $p1 First point
 257       * @param awPoint $p2 Second point
 258       * @param int $type Style of line (default to solid)
 259       * @param int $thickness Line thickness (default to 1)
 260       */
 261  	public function __construct($p1 = NULL, $p2 = NULL, $type = awLine::SOLID, $thickness = 1) {
 262      
 263          $this->setLocation($p1, $p2);
 264          $this->setStyle($type);
 265          $this->setThickness($thickness);
 266          
 267      }
 268      
 269      /**
 270       * Build a line from 4 coords
 271       *
 272       * @param int $x1 Left position
 273       * @param int $y1 Top position
 274       * @param int $x2 Right position
 275       * @param int $y2 Bottom position
 276       */
 277  	public static function build($x1, $y1, $x2, $y2) {
 278      
 279          return new awLine(
 280              new awPoint($x1, $y1),
 281              new awPoint($x2, $y2)
 282          );
 283      
 284      }
 285      
 286      /**
 287       * Change X values of the line
 288       *
 289       * @param int $x1 Begin value
 290       * @param int $x2 End value
 291       */
 292  	public function setX($x1, $x2) {
 293          $this->p1->setX($x1);
 294          $this->p2->setX($x2);
 295      }
 296      
 297      /**
 298       * Change Y values of the line
 299       *
 300       * @param int $y1 Begin value
 301       * @param int $y2 End value
 302       */
 303  	public function setY($y1, $y2) {
 304          $this->p1->setY($y1);
 305          $this->p2->setY($y2);
 306      }
 307      
 308      /**
 309       * Change line location
 310       *
 311       * @param awPoint $p1 First point
 312       * @param awPoint $p2 Second point
 313       */
 314  	public function setLocation($p1, $p2) {
 315          if(is_null($p1) or $p1 instanceof awPoint) {
 316              $this->p1 = $p1;
 317          }
 318          if(is_null($p2) or $p2 instanceof awPoint) {
 319              $this->p2 = $p2;
 320          }
 321      }
 322      
 323      /**
 324       * Get line location
 325       *
 326       * @param array Line location
 327       */
 328  	public function getLocation() {
 329          return array($this->p1, $this->p2);
 330      }
 331      
 332      /**
 333       * Get the line size
 334       *
 335       * @return float
 336       */
 337  	public function getSize() {
 338      
 339          $square = pow($this->p2->x - $this->p1->x, 2) + pow($this->p2->y - $this->p1->y, 2);
 340          return sqrt($square);
 341      
 342      }
 343      
 344      /**
 345       * Test if the line can be considered as a point
 346       *
 347       * @return bool
 348       */
 349  	public function isPoint() {
 350          return ($this->p1->x === $this->p2->x and $this->p1->y === $this->p2->y);
 351      }
 352      
 353      /**
 354       * Test if the line is a vertical line
 355       *
 356       * @return bool
 357       */
 358  	public function isVertical() {
 359          return ($this->p1->x === $this->p2->x);
 360      }
 361      
 362      /**
 363       * Test if the line is an horizontal line
 364       *
 365       * @return bool
 366       */
 367  	public function isHorizontal() {
 368          return ($this->p1->y === $this->p2->y);
 369      }
 370  
 371  }
 372  
 373  registerClass('Line');
 374  
 375  /**
 376   * A vector is a type of line
 377   * The sense of the vector goes from $p1 to $p2.
 378   *
 379   * @package Artichow
 380   */
 381  class awVector extends awLine {
 382      
 383      /**
 384       * Get vector angle in radians
 385       *
 386       * @return float
 387       */
 388  	public function getAngle() {
 389      
 390          if($this->isPoint()) {
 391              return 0.0;
 392          }
 393          
 394          $size = $this->getSize();
 395      
 396          $width = ($this->p2->x - $this->p1->x);
 397          $height = ($this->p2->y - $this->p1->y) * -1;
 398          
 399          if($width >= 0 and $height >= 0) {
 400              return acos($width / $size);
 401          } else if($width <= 0 and $height >= 0) {
 402              return acos($width / $size);
 403          } else {
 404              $height *= -1;
 405              if($width >= 0 and $height >= 0) {
 406                  return 2 * M_PI - acos($width / $size);
 407              } else if($width <= 0 and $height >= 0) {
 408                  return 2 * M_PI - acos($width / $size);
 409              }
 410          }
 411      
 412      }
 413  
 414  }
 415  
 416  registerClass('Vector');
 417   
 418  /* <php4> */
 419  
 420  define("POLYGON_SOLID", 1);
 421  define("POLYGON_DOTTED", 2);
 422  define("POLYGON_DASHED", 3);
 423  
 424  /* </php4> */
 425  
 426  /**
 427   * Describe a polygon
 428   *
 429   * @package Artichow
 430   */
 431  class awPolygon extends awShape {
 432  
 433      /**
 434       * Polygon points
 435       *
 436       * @var array
 437       */
 438      protected $points = array();
 439  
 440      /**
 441       * Set a point in the polygon
 442       *
 443       * @param int $pos Point position
 444       * @param awPoint $point
 445       */
 446  	public function set($pos, $point) {
 447          if(is_null($point) or $point instanceof awPoint) {
 448              $this->points[$pos] = $point;
 449          }
 450      }
 451      
 452      /**
 453       * Add a point at the end of the polygon
 454       *
 455       * @param awPoint $point
 456       */
 457  	public function append($point) {
 458          if(is_null($point) or $point instanceof awPoint) {
 459              $this->points[] = $point;
 460          }
 461      }
 462      
 463      /**
 464       * Get a point at a position in the polygon
 465       *
 466       * @param int $pos Point position
 467       * @return Point
 468       */
 469  	public function get($pos) {
 470          return $this->points[$pos];
 471      }
 472      
 473      /**
 474       * Count number of points in the polygon
 475       *
 476       * @return int
 477       */
 478  	public function count() {
 479          return count($this->points);
 480      }
 481      
 482      /**
 483       * Returns all points in the polygon
 484       *
 485       * @return array
 486       */
 487  	public function all() {
 488          return $this->points;
 489      }
 490  
 491  }
 492  
 493  registerClass('Polygon');
 494  ?>


Généré le : Thu Sep 6 14:14:11 2007 par Balluche grâce à PHPXref 0.7