[ Index ]
 

Code source de phpMyVisites 2.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

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


Généré le : Mon Nov 26 14:10:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics