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

Source for file Rectangle.php

Documentation is available at Rectangle.php

  1. <?php
  2.  
  3. /**
  4.  * Rectangle class
  5.  *
  6.  * Description: Class to create the Rectangle object
  7.  *
  8.  * @author
  9.  * @copyright (c) 1995-2010 by Steema Software SL. All Rights Reserved. <info@steema.com>
  10.  * @version 1.0
  11.  * @package TeeChartPHP
  12.  * @link http://www.steema.com
  13.  */
  14.  
  15. class Rectangle  
  16. {
  17.  
  18.    public $height;
  19.    public $width;
  20.    public $x;
  21.    public $y;
  22.  
  23.     // Interceptors
  24.     function __get$property {
  25.       $method ="get{$property}";
  26.       if method_exists$this$method ) ) {
  27.         return $this->$method();
  28.       }
  29.     }
  30.  
  31.     function __set $property,$value {
  32.       $method ="set{$property}";
  33.       if method_exists$this$method ) ) {
  34.         return $this->$method($value);
  35.       }
  36.     }
  37.  
  38.    function Rectangle($x 0$y 0$width 0.0$height 0.0)
  39.    {
  40.       $this->x = $x;
  41.       $this->y = $y;
  42.       $this->width = $width;
  43.       $this->height = $height;
  44.       $this->setBounds($x$y$width$height);
  45.    }
  46.  
  47.    function setRectangle(&$rect)
  48.    {
  49.       $this->setBounds(
  50.       $rect->getX(),
  51.       $rect->getY(),
  52.       $rect->getWidth(),
  53.       $rect->getHeight()
  54.       );
  55.    }
  56.  
  57.    function setBounds($x$y$width$height)
  58.    {
  59.       $this->setSize($width$height);
  60.       $this->setLocation($x$y);
  61.    }
  62.  
  63.    function setLocation($x$y)
  64.    {
  65.       $this->x = $x;
  66.       $this->y = $y;
  67.    }
  68.  
  69.    function setSize($w$h)
  70.    {
  71.       $this->width = $w;
  72.       $this->height = $h;
  73.    }
  74.  
  75.    function setX($x)
  76.    {
  77.       $this->x = $x;
  78.    }
  79.  
  80.    function setY($y)
  81.    {
  82.       $this->y = $y;
  83.    }
  84.  
  85.    function getCenterX()
  86.    {
  87.       return $this->x + $this->width / 2.0;
  88.    }
  89.  
  90.    function getCenterY()
  91.    {
  92.       return $this->y + $this->height / 2.0;
  93.    }
  94.  
  95.    function getX()
  96.    {
  97.       return $this->x;
  98.    }
  99.  
  100.    function getY()
  101.    {
  102.       return $this->y;
  103.    }
  104.  
  105.    function toString()
  106.    {
  107.       return "Rectangle(" .
  108.       $this->x . "," .
  109.       $this->y . "," .
  110.       $this->width . "," .
  111.       $this->height . ")";
  112.    }
  113.  
  114.    public function copy()
  115.    {
  116.       return new Rectangle($this->x$this->y$this->width$this->height);
  117.    }
  118.  
  119.    public function getArea()
  120.    {
  121.       return $this->height * $this->width;
  122.    }
  123.  
  124.    static public function fromLTRB($x$y$right$bottom)
  125.    {
  126.       return new Rectangle($x$y$right $x 1$bottom $y 1);
  127.    }
  128.  
  129.    /**
  130.    * Gets rectangle Right coordinate.
  131.    *
  132.    * @return int 
  133.    */
  134.    public function getRight()
  135.    {
  136.       return $this->x + $this->width;
  137.    }
  138.  
  139.    /**
  140.    * Gets rectangle Bottom coordinate.
  141.    *
  142.    * @return int 
  143.    */
  144.    public function getBottom()
  145.    {
  146.       return $this->y + $this->height;
  147.    }
  148.  
  149.    /**
  150.    * Gets rectangle Left coordinate.
  151.    *
  152.    * @return int 
  153.    */
  154.    public function getLeft()
  155.    {
  156.       return $this->x;
  157.    }
  158.  
  159.    /**
  160.    * Sets rectangle Left coordinate.
  161.    *
  162.    */
  163.    public function setLeft($value)
  164.    {
  165.       $this->width += $this->x - $value;
  166.       $this->x = $value;
  167.    }
  168.  
  169.    /**
  170.    * Sets rectangle Right coordinate.
  171.    *
  172.    */
  173.    public function setRight($value)
  174.    {
  175.       $this->width = $value $this->x;
  176.    }
  177.  
  178.    /**
  179.    * Gets rectangle Top coordinate.
  180.    *
  181.    * @return int 
  182.    */
  183.    public function getTop()
  184.    {
  185.       return $this->y;
  186.    }
  187.  
  188.    /**
  189.    * Sets rectangle Top coordinate.
  190.    *
  191.    */
  192.    public function setTop($value)
  193.    {
  194.       $this->height += $this->y - $value;
  195.       $this->y = $value;
  196.    }
  197.  
  198.    /**
  199.    * Sets rectangle Bottom coordinate.
  200.    *
  201.    */
  202.    public function setBottom($value)
  203.    {
  204.       $this->height = $value $this->y;
  205.    }
  206.  
  207.    public function intersect($value)
  208.    {
  209.       self::__intersect($this$value$this);
  210.    }
  211.  
  212.    public static function __intersect($src1$src2$dest)
  213.    {
  214.       $x1 max($src1->getX()$src2->getX());
  215.       $y1 max($src1->getY()$src2->getY());
  216.       $x2 min($src1->getX($src1->getWidth()$src2->getX($src2->getWidth());
  217.       $y2 min($src1->getY($src1->getHeight()$src2->getY($src2->getHeight());
  218.       $dest->setFrame($x1$y1$x2 $x1$y2 $y1);
  219.    }
  220.  
  221.    /**
  222.    * Determines whether or not this <code>Rectangle</code> and the specified
  223.    * <code>Rectangle</code> intersect. Two rectangles intersect if
  224.    * their intersection is nonempty.
  225.    *
  226.    * @param the specified <code>Rectangle</code>
  227.    * @return    <code>true</code> if the specified <code>Rectangle</code>
  228.    *             and this <code>Rectangle</code> intersect;
  229.    *             <code>false</code> otherwise.
  230.    */
  231.    public function intersects($r)
  232.    {
  233.       (int)$tw $this->width;
  234.       (int)$th $this->height;
  235.       (int)$rw $r->width;
  236.       (int)$rh $r->height;
  237.       if($rw <= || $rh <= || $tw <= || $th <= 0)
  238.       {
  239.          return false;
  240.       }
  241.       (int)$tx $this->x;
  242.       (int)$ty $this->y;
  243.       (int)$rx $r->x;
  244.       (int)$ry $r->y;
  245.       $rw += $rx;
  246.       $rh += $ry;
  247.       $tw += $tx;
  248.       $th += $ty;
  249.  
  250.       return(($rw $rx || $rw $tx&&
  251.       ($rh $ry || $rh $ty&&
  252.       ($tw $tx || $tw $rx&&
  253.       ($th $ty || $th $ry));
  254.    }
  255.  
  256.    public function setFrame($x$y$w$h)
  257.    {
  258.       $this->setX($x);
  259.       $this->setY($y);
  260.       $this->setWidth($w);
  261.       $this->setHeight($h);
  262.    }
  263.  
  264.    public function getWidth()
  265.    {
  266.       return $this->width;
  267.    }
  268.  
  269.    public function setWidth($value)
  270.    {
  271.       $this->width = $value;
  272.    }
  273.  
  274.    public function getHeight()
  275.    {
  276.       return $this->height;
  277.    }
  278.  
  279.    public function setHeight($value)
  280.    {
  281.       $this->height = $value;
  282.    }
  283.  
  284.    public function center()
  285.    {
  286.       return new TeePoint($this->x + ($this->width / 2)$this->y + ($this->height / 2));
  287.    }
  288.  
  289.    /**
  290.    * Returns the location of this <code>Rectangle</code>.
  291.    * <p>
  292.    * This method is included for completeness, to parallel the
  293.    * <code>getLocation</code> method of <code>Component</code>.
  294.    * @return the <code>Point</code> that is the top-left corner of
  295.    *             this <code>Rectangle</code>.
  296.    */
  297.    public function getLocation()
  298.    {
  299.       return new TeePoint($this->x$this->y);
  300.    }
  301.  
  302.    /**
  303.    * Moves this <code>Rectangle</code> to the specified location.
  304.    * <p>
  305.    * This method is included for completeness, to parallel the
  306.    * <code>setLocation</code> method of <code>Component</code>.
  307.    * @param the <code>Point</code> specifying the new location
  308.    *                 for this <code>Rectangle</code>
  309.    */
  310.    public function _setLocation($p)
  311.    {
  312.       $this->setLocation($p->getX()$p->getY());
  313.    }
  314.  
  315.    /**
  316.    * Determines whether or not this <code>Rectangle</code> is empty. A
  317.    * <code>Rectangle</code> is empty if its width or its height is less
  318.    * than or equal to zero.
  319.    * @return     <code>true</code> if this <code>Rectangle</code> is empty;
  320.    *              <code>false</code> otherwise.
  321.    */
  322.    public function isEmpty()
  323.    {
  324.       return($this->width <= 0|| ($this->height <= 0);
  325.    }
  326.  
  327.    public function inflate($horizontal$vertical)
  328.    {
  329.       $this->width += $horizontal;
  330.       $this->height += $vertical;
  331.    }
  332.  
  333.    public function offset($w$h)
  334.    {
  335.       $this->x += $w;
  336.       $this->y += $h;
  337.    }
  338.  
  339.    /**
  340.     * Resizes the <code>Rectangle</code> both horizontally and vertically.
  341.     * <p>
  342.     * This method modifies the <code>Rectangle</code> so that it is
  343.     * <code>h</code> units larger on both the left and right side,
  344.     * and <code>v</code> units larger at both the top and bottom.
  345.     * <p>
  346.     * The new <code>Rectangle</code> has (<code>x&nbsp;-&nbsp;h</code>,
  347.     * <code>y&nbsp;-&nbsp;v</code>) as its top-left corner, a
  348.     * width of
  349.     * <code>width</code>&nbsp;<code>+</code>&nbsp;<code>2h</code>,
  350.     * and a height of
  351.     * <code>height</code>&nbsp;<code>+</code>&nbsp;<code>2v</code>.
  352.     * <p>
  353.     * If negative values are supplied for <code>h</code> and
  354.     * <code>v</code>, the size of the <code>Rectangle</code>
  355.     * decreases accordingly.
  356.     * The <code>grow</code> method does not check whether the resulting
  357.     * values of <code>width</code> and <code>height</code> are
  358.     * non-negative.
  359.     * @param the horizontal expansion
  360.     * @param the vertical expansion
  361.     */
  362.    public function grow($h$v)
  363.    {
  364.         $this->x -= $h;
  365.         $this->y -= $v;
  366.         $this->width += $h 2;
  367.         $this->height += $v 2;
  368.    }
  369.  
  370. }
  371.  
  372. class Square extends Rectangle
  373. {
  374.    public function __construct($size)
  375.    {
  376.       $this->height = $size;
  377.       $this->width = $size;
  378.    }
  379.  
  380.    public function getArea()
  381.    {
  382.       return pow($this->height2);
  383.    }
  384. }
  385. ?>

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