linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > php >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·PHP生成静态页面的一些经验
·PHP无限分类与树型论坛的实现方
·php5学习笔记
·入门级PHP程序员面试题
·PHP 和 MySQL初学入门
·PHP入门速成
·vim的代码折叠
·文件下载统计php编程代码
·如何建立自己的新闻发布系统
·织梦CMS中文转换拼音函数研究
·Windows下Apache+Tomcat+MySQL
·PHP初学者头疼问题总结
·Cookie及其使用详细介绍
·生成sessionid和随机密码的例子
·使用无限生命期Session的方法
最新文章
·PHP入门速成
·用php实现广告轮播
·Zend Optimizer 问题浅析
·功能强大的CGI语言----PHP3
·用Session对Web页面进行保护
·PHP--进行模块化设计
·如何将PHP的结果输出到非PHP页
·如何开发一个虚拟域名系统
·PHP4调用自己编写的COM组件
·简单的页面缓冲技术(三)
·简单的页面缓冲技术(二)
·简单的页面缓冲技术(一)
·用Socket发送电子邮件(二)
·用Socket发送电子邮件(一)
·PHP/MySQL 购物车
Google
 
使用 PHP 5.0创建图形的巧妙方法(4)
[ 作者:  加入时间:2008-02-13 11:30:01  来自:Linux联盟收集整理 ]
创建 viewport E88Linux联盟
   E88Linux联盟
    viewport 是一个人造的坐标系统,可以转换成图像的物理坐标系统。viewport 的扩展可以是您希望的任何东西。例如,x 和 y 轴的起点和终点可以是 -2 和 2,这样 viewport 坐标平面的中心就是 0, 0。这对于三角图形(例如 sin 和 cosine)来说是很好的一个 viewport。或者,这个 viewport 也可以是不对称的,其中 y 值的范围从 -1 到 1,x 值的范围是从 0 到 10,000,这取决于您的需要。 E88Linux联盟
   E88Linux联盟
    这个 viewport 的其他值可以确保构建一个 400X400 的图像所采用的逻辑与构建一个 4000X2000 的图像所采用的逻辑是相同的。代码负责向这个 viewport 中写入数据,然后这个 viewport 自动实现到图像的物理尺寸的自动映射。 E88Linux联盟
   E88Linux联盟
    要让您的 viewport 正常工作,您需要将这个 viewport 的范围从 0,0 修改为 1,1,这可以让图形对象回调图形环境,从而将 viewport 的坐标转换成物理坐标。您可以将所有的代码都放到 BoxObject 基类中进行简化。 E88Linux联盟
   E88Linux联盟
    图 7 显示了有关新添加的代码的两个内容。首先是添加的 tx 和 ty 方法,这会将 x 和 y 坐标从 viewport 转换成物理图像的坐标。第二个是对 BoxObject 增加了 draw 方法,它的派生类应该用来进行制图。BoxObject 在 render 方法中实现 viewport 的转换,并使用物理坐标来调用 draw 方法。使用这种方法,Line、Oval 和 Rectangle 类都可以利用 viewport 坐标,而不需要担心坐标转换的问题。 E88Linux联盟
   E88Linux联盟
   E88Linux联盟
   E88Linux联盟
  这个新库的代码如清单 7 所示: E88Linux联盟
   E88Linux联盟
    清单 7. 具有 viewport 支持的图形库 E88Linux联盟
   E88Linux联盟
   E88Linux联盟
  <?PHP E88Linux联盟
  class GraphicsEnvironment E88Linux联盟
  { E88Linux联盟
   public $width; E88Linux联盟
   public $height; E88Linux联盟
   public $gdo; E88Linux联盟
   public $colors = array(); E88Linux联盟
   E88Linux联盟
   public function __construct( $width, $height ) E88Linux联盟
   { E88Linux联盟
   $this->width = $width; E88Linux联盟
   $this->height = $height; E88Linux联盟
   $this->gdo = imagecreatetruecolor( $width, $height ); E88Linux联盟
   $this->addColor( "white", 255, 255, 255 ); E88Linux联盟
   imagefilledrectangle( $this->gdo, 0, 0, E88Linux联盟
   $width, $height, E88Linux联盟
   $this->getColor( "white" ) ); E88Linux联盟
   } E88Linux联盟
   E88Linux联盟
   public function width() { return $this->width; } E88Linux联盟
   E88Linux联盟
   public function height() { return $this->height; } E88Linux联盟
   E88Linux联盟
   public function addColor( $name, $r, $g, $b ) E88Linux联盟
   { E88Linux联盟
   $this->colors[ $name ] = imagecolorallocate( E88Linux联盟
   $this->gdo, E88Linux联盟
   $r, $g, $b ); E88Linux联盟
   } E88Linux联盟
   E88Linux联盟
   public function getGraphicObject() E88Linux联盟
   { E88Linux联盟
   return $this->gdo; E88Linux联盟
   } E88Linux联盟
   E88Linux联盟
   public function getColor( $name ) E88Linux联盟
   { E88Linux联盟
   return $this->colors[ $name ]; E88Linux联盟
   } E88Linux联盟
   E88Linux联盟
   public function saveASPng( $filename ) E88Linux联盟
   { E88Linux联盟
   imagepng( $this->gdo, $filename ); E88Linux联盟
   } E88Linux联盟
   E88Linux联盟
   public function tx( $x ) E88Linux联盟
   { E88Linux联盟
   return $x * $this->width; E88Linux联盟
   } E88Linux联盟
   E88Linux联盟
   public function ty( $y ) E88Linux联盟
   { E88Linux联盟
   return $y * $this->height; E88Linux联盟
   } E88Linux联盟
  } E88Linux联盟
   E88Linux联盟
  abstract class GraphicsObject E88Linux联盟
  { E88Linux联盟
   abstract public function render( $ge ); E88Linux联盟
   abstract public function z(); E88Linux联盟
  } E88Linux联盟
   E88Linux联盟
  function zsort( $a, $b ) E88Linux联盟
  { E88Linux联盟
   if ( $a->z() < $b->z() ) return -1; E88Linux联盟
   if ( $a->z() > $b->z() ) return 1; E88Linux联盟
   return 0; E88Linux联盟
  } E88Linux联盟
   E88Linux联盟
  class Group extends GraphicsObject E88Linux联盟
  { E88Linux联盟
   private $z; E88Linux联盟
   protected $members = array(); E88Linux联盟
   public function __construct( $z ) E88Linux联盟
   { E88Linux联盟
   $this->z = $z; E88Linux联盟
   } E88Linux联盟
   public function add( $member ) E88Linux联盟
   { E88Linux联盟
   $this->members []= $member; E88Linux联盟
   } E88Linux联盟
   public function render( $ge ) E88Linux联盟
   { E88Linux联盟
   usort( $this->members, "zsort" ); E88Linux联盟
   foreach( $this->members as $gobj ) E88Linux联盟
   { E88Linux联盟
   $gobj->render( $ge ); E88Linux联盟
   } E88Linux联盟
   } E88Linux联盟
   public function z() { return $this->z; } E88Linux联盟
  } E88Linux联盟
   E88Linux联盟
  abstract class BoxObject extends GraphicsObject E88Linux联盟
  { E88Linux联盟
   protected $color; E88Linux联盟
   protected $sx; E88Linux联盟
   protected $sy; E88Linux联盟
   protected $ex; E88Linux联盟
   protected $ey; E88Linux联盟
   protected $z; E88Linux联盟
   E88Linux联盟
   public function __construct( $z, $color, $sx, $sy, $ex, $ey ) E88Linux联盟
   { E88Linux联盟
   $this->z = $z; E88Linux联盟
   $this->color = $color; E88Linux联盟
   $this->sx = $sx; E88Linux联盟
   $this->sy = $sy; E88Linux联盟
   $this->ex = $ex; E88Linux联盟
   $this->ey = $ey; E88Linux联盟
   } E88Linux联盟
   E88Linux联盟
   public function render( $ge ) E88Linux联盟
   { E88Linux联盟
   $rsx = $ge->tx( $this->sx ); E88Linux联盟
   $rsy = $ge->ty( $this->sy ); E88Linux联盟
   $rex = $ge->tx( $this->ex ); E88Linux联盟
   $rey = $ge->ty( $this->ey ); E88Linux联盟
   $this->draw( $rsx, $rsy, $rex, $rey, E88Linux联盟
   $ge->getGraphicObject(), E88Linux联盟
   $ge->getColor( $this->color ) ); E88Linux联盟
   } E88Linux联盟
   E88Linux联盟
   abstract public function draw( $sx, $sy, E88Linux联盟
   $ex, $ey, $gobj, $color ); E88Linux联盟
   E88Linux联盟
   public function z() { return $this->z; } E88Linux联盟
  } E88Linux联盟
   E88Linux联盟
  class Line extends BoxObject E88Linux联盟
  { E88Linux联盟
   public function draw( $sx, $sy, $ex, $ey, E88Linux联盟
   $gobj, $color ) E88Linux联盟
   { E88Linux联盟
   imageline( $gobj, $sx, $sy, $ex, $ey, E88Linux联盟
   $color ); E88Linux联盟
   } E88Linux联盟
  } E88Linux联盟
   E88Linux联盟
  class Rectangle extends BoxObject E88Linux联盟
  { E88Linux联盟
   public function draw( $sx, $sy, $ex, $ey, E88Linux联盟
   $gobj, $color ) E88Linux联盟
   { E88Linux联盟
   imagefilledrectangle( $gobj, $sx, $sy, E88Linux联盟
   $ex, $ey, $color ); E88Linux联盟
   } E88Linux联盟
  } E88Linux联盟
   E88Linux联盟
  class Oval extends BoxObject E88Linux联盟
  { E88Linux联盟
   public function draw( $sx, $sy, $ex, $ey, E88Linux联盟
   $gobj, $color ) E88Linux联盟
   { E88Linux联盟
   $w = $ex - $sx; E88Linux联盟
   $h = $ey - $sy; E88Linux联盟
   imagefilledellipse( $gobj, E88Linux联盟
   $sx + ( $w / 2 ), $sy + ( $h / 2 ), E88Linux联盟
   $w, $h, $color ); E88Linux联盟
   } E88Linux联盟
  } E88Linux联盟
  ?> E88Linux联盟
   E88Linux联盟
   E88Linux联盟
   E88Linux联盟
    GraphicsEnvironment 类中的 viewport 转换代码是高亮显示的,正如 GraphicsObject 中的 render 代码一样,这会回调图形环境来进行坐标转换的工作。 E88Linux联盟
   E88Linux联盟
    测试代码只需要稍加修改即可(请参看清单 8)。这些对象现在需要在 0,0 和 1,1 之间的 viewport 中进行指定。 E88Linux联盟
   E88Linux联盟
    清单 8. 使用新 viewport 坐标的测试代码 E88Linux联盟
   E88Linux联盟
   E88Linux联盟
  $g1 = new Group( 0 ); E88Linux联盟
  $g1->add( new Oval( 200, "red", 0.1, 0.1, 0.5, 0.5 ) ); E88Linux联盟
  $g1->add( new Rectangle( 100, "black", 0.4, 0.4, 0.9, 0.9 ) ); E88Linux联盟
   E88Linux联盟
   E88Linux联盟
   E88Linux联盟
    这非常不错,但是您可能实际上并不希望使用一个 0,0 与 1,1 之间的 viewport;而是希望使用任意的 viewport —— 例如,在 -1000,-1000 到 1000,1000 之间。要让这成为可能,这个图形环境就需要知道 viewport 的起点和终点 E88Linux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·使用 PHP 5.0创建图形的巧妙方法(3)  (2008-02-13 11:26:43)
 ·使用 PHP 5.0创建图形的巧妙方法 (2)  (2008-02-13 11:25:14)
 ·使用 PHP 5.0创建图形的巧妙方法 (1)  (2008-02-13 11:23:14)
 ·PHP 5.0对象模型深度探索之类的静态成员  (2008-02-05 10:28:11)
 ·PHP 5.0对象模型深度探索之属性和方法  (2008-01-28 14:24:06)
 ·PHP 5.0对象模型深度探索之定义一个类  (2008-01-28 14:23:15)
 ·Linux下Apache 2.2, MySQL 5.0和PHP 5.0的安装与配置  (2007-01-22 02:09:29)
 ·PHP 5.0中多态性的实现方案浅析  (2007-01-22 01:39:52)
 ·PHP 5.0中多态性的实现方案  (2006-06-09 11:44:16)
 ·PHP 5.0对象模型深度探索之绑定  (2006-06-08 22:39:05)