Working with Geometric Datatypes


Up to now we have used PHP and the Ming module for displaying fields that contain ordinary texts . Because PostgreSQL supports a lot of geometric objects and a lot of functions related to those objects, you should combine PostgreSQL and Flash to design applications for displaying geometric data.

PHP can be used as an object-oriented programming (OOP) language. In this section, you explore PHP classes that you can use in combination with Flash and PostgreSQL.

Let's start with a simple example for displaying rectangles stored in a PostgreSQL database. We compiled a small table, which will be used by the program that follows . Here is the data structure of the table:

 myflash=#  \   d   boxes  Table "boxes"  Attribute   Type    Modifier -----------+---------+----------  label      text      data       box       color      text      trans      integer  Constraint: ((trans <= 100) AND (trans >= 0)) 

The table contains four columns . The second column contains the definition of the rectangle. We inserted three records into the table:

 myflash=#  SELECT * FROM boxes;  label         data           color    trans --------+-------------------+-----------+-------  first   (40,40),(10,10)    255,0,0       80  third   (200,180),(30,60)  0,0,255       10  second  (150,80),(65,42)   0,200,128     60 (3 rows) 

The first column contains a label, and the second column contains the definition of the rectangle. The third column stores the color of the rectangle.

As mentioned earlier, we implemented a class for processing rectangles with the help of PHP. Implementing a class is possibly the easiest way of dealing with geometric objects. Here is the code of the class:

 <?php class box {         var $x1, $y1, $x2, $y2;      var $color, $trans;      # constructor      function box($a, $b, $c, $d, $e)      {              $this->x1=$a;              $this->y1=$b;              $this->x2=$c;              $this->y2=$d;              $this->color=$e;      }      function box($x, $c)      {              $search = array("'\ s'","'\ ('", "'\ )'");              $x = preg_replace ($search,"", $x);              $c = preg_replace ($search,"", $c);              $array = split(",", $x);              $this->x1=$array[0];              $this->y1=$array[1];              $this->x2=$array[2];              $this->y2=$array[3];              $this->color=$c;      }      # drawing function      function draw()      {              $c=split(",", $this->color);              $s=new SWFShape();              $s->setLine(20, $c[0], $c[1], $c[2]);              $s->setRightFill($s->addFill($c[0], $c[1], $c[2]));              $s->movePenTo($this->x1, $this->y1);              $s->drawLineTo($this->x1, $this->y2);              $s->drawLineTo($this->x2, $this->y2);              $s->drawLineTo($this->x2, $this->y1);              $s->drawLineTo($this->x1, $this->y1);              return($s);      } } 

The class is very simple. We implemented two constructors. The first constructor needs five parameters, which are directly assigned to the variables our class has to contain. The second constructor extracts the coordinates of the rectangle from the data returned by the database. Extracting the data from the result of the query is propably the most difficult task, because we use regular expressions to parse and transform the string. All brackets and all whitespace characters are removed from the string so that all further transformations are not hampered.

After the two constructors, a method called draw is implemented. We will use this method to draw the rectangles.

Here is the main program:

 <?php   dl('php_ming.so');   include("class.boxes.php");   # connecting to the database   $dbh=pg_connect("dbname=myflash user=hs");   if (!$dbh)   {     echo ("ERROR: connecting failed");     exit;   }   # selecting data from the PostgreSQL   $result=pg_exec($dbh, "SELECT * FROM boxes");   if (!$result)   {     echo ("ERROR: cannot failed");     exit;   }   # creating font and movie object   $m = new SWFMovie();   $m->setDimension(320, 240);   # computing number of lines   $num_res=pg_numrows($result);   # processing the data line by line   for ($i=0;$i<$num_res;$i++)   {     $line=pg_fetch_row($result,$i);     $mybox=new box($line[1], $line[2]);     $s[$i]=$mybox->draw();     $res[$i] = $m->add($s[$i]);   }   $m->nextFrame();   # displaying results   header('Content-type: application/x-shockwave-flash');   $m->output(); ?> 

First we connect to the database, as we have in the previous examples. We select all records and all columns from table boxes and compute the number of lines returned.

In the next step, we process the result of the query line by line and create an object for every rectangle. The object is drawn and added to the scenery .

Figure 14.6 shows the result.

Figure 14.6. Three rectangles.

graphics/14fig06.gif

All three rectangles in the table are displayed.

Sometimes you want your objects to look a bit nicer. In that case, using gradients might be a good choice. Gradients are smooth transitions between colors. To show you how gradients can be used, we changed the draw method:

 function draw() {         $c=split(",", $this->color);         $s=new SWFShape();         $s->setLine(20, $c[0], $c[1], $c[2]);         $g = new SWFGradient();         $g->addEntry(0.0, $c[0], $c[1], $c[2], 20);         $g->addEntry(0.5, $c[0], $c[1], $c[2], 90);         $g->addEntry(1.0, $c[0], $c[1], $c[2], 10);         $f = $s->addFill($g, SWFFILL_RADIAL_GRADIENT);         $f->scaleTo(0.12);         $f->moveTo(($this->x1+2*$this->x2)/3, ($this->y1+2*$this->y2)/3);         $s->setRightFill($f);         $s->movePenTo($this->x1, $this->y1);         $s->drawLineTo($this->x1, $this->y2);         $s->drawLineTo($this->x2, $this->y2);         $s->drawLineTo($this->x2, $this->y1);         $s->drawLineTo($this->x1, $this->y1);         return($s); } 

First we create an instance of the SWFGradient object and add some entries to it. At least two entries have to be added; otherwise , the rectangle will contain only one color. In the next step, we add the fill to the object. In the example, we used SWFFILL_RADIAL_GRADIENT as a flag for the addFill method. If we wanted a linear gradient, we would use SWFFILL_LINEAR_GRADIENT . Figure 14.7 shows the result of the modified draw function.

Figure 14.7. Three more beautiful rectangles.

graphics/14fig07.gif

As you can see from this section, it is easy to generate Flash movies with the help of PHP.



PostgreSQL Developer's Handbook2001
PostgreSQL Developer's Handbook2001
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 125

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net