|
创建 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论坛讨论 |
|