Zend - The PHP Company




3-D

Add Code


a little class to draw real 3D graphics with surface area  

Type: class
Added by: alti
Entered: 14/11/2000
Last modified: 02/12/2000
Rating: ***** (3 votes)
Views: 25589
with this class you can draw on the fly 3D graphics with line and surface area (the best things to understand is to see the image of the example)


<?php
////////////////////////////////////////////////////
// Created By Alti 14/11/2000  altimir@hotmail.com//
//            Modified on 20/02/2001              //
////////////////////////////////////////////////////

// you must name this file  class_new.php3
// vous devez nommer ce fichier class_new.php3

// you can use as free and mofify it at will
// vous pouvez utilisez cette classe a volonte et la
// modifier tout autant

class graph {
var 
$image;
var 
$filename="graph.gif";
var 
$Bheight=600;
var 
$Bwidth=800;
var 
$datay=array();
var 
$nbz=5;       //(1,2,3,4,5)
var $nbcourbe=6;  //begin at 0
var $nbdonnee=19;
var 
$nbx=20;
var 
$nby=40;
var 
$labelX=array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
var 
$labelY=array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40);
var 
$labelZ=array(0,1,2,3,4,5);
var 
$color=array(
'background_color'=>0,
'black'=>0,
'ycolor'=>0,
'xcolor'=>0,
'diagonal1'=>0,
'diagonal2'=>0,
'ligne_Y'=>0,
'ligne_X'=>0,
'crete_color'=>0,
'ligne_Z'=>0
);
var 
$diago='oui';
var 
$courbe_color=array();
var 
$ligne_color=array();
var 
$titre="GRAPH 3D";
var 
$titre_labelY="Label des Y";
var 
$titre_labelX="Label des X";


// init
function graph($Bwidth,$Bheight) {
$this->Bwidth=$Bwidth;
$this->Bheight=$Bheight;
$this->image=ImageCreate($this->Bwidth,$this->Bheight);
$this->color['background_color']=ImageColorAllocate($this->image,255,255,255);
$this->color['black']=ImageColorAllocate($this->image,0,0,0);
$this->color['ycolor']=ImageColorAllocate($this->image,228,238,244);
$this->color['xcolor']=ImageColorAllocate($this->image,234,245,252);
$this->color['xycolor']=ImageColorAllocate($this->image,206,225,237);
$this->color['diagonal1']=ImageColorAllocate($this->image,133,171,233);
$this->color['diagonal2']=ImageColorAllocate($this->image,115,204,94);
$this->color['ligne_Y']=ImageColorAllocate($this->image,133,171,233);
$this->color['ligne_X']=ImageColorAllocate($this->image,115,204,94);
$this->color['crete_color']=ImageColorAllocate($this->image,192,0,0);
$this->color['ligne_Z']=ImageColorAllocate($this->image,245,197,197);
}





// affect colors

function coloris($num,$red,$green,$blue) {
$this->courbe_color[$num][0]=ImageColorAllocate($this->image,$red,$green,$blue);
$red1=$red-10;
$green1=$green-20;
$blue1=$blue-20;
$this->courbe_color[$num][1]=ImageColorAllocate($this->image,$red1,$green1,$blue1);
}

function 
lignecolor($num,$red,$green,$blue) {
$this->ligne_color[$num][0]=ImageColorAllocate($this->image,$red,$green,$blue);
}

function 
create_graph() {

//----------------------------------
//----------placing graph-----------
//----------------------------------

$lenY=0;
for (
$ii=0;$ii<=$this->nby;$ii++) {
$var=$this->labelY[$ii];
$lenY=($lenY>strlen($var))?$lenY:strlen($var);
};
$startx=$lenY*(5)+50//started plotted area for x
$starty=60//started plotted area for y


// width and height for the area of the graphics
$height=$this->Bheight-2*$starty;
$y=$height/($this->nby+$this->nbz);
$height=$y*$this->nby;
$width=$this->Bwidth-2*$startx;
$x=$width/($this->nbx+$this->nbz);
$width=$x*$this->nbx;



// graph title
ImageString($this->image,3,($this->Bwidth/2)-strlen($this->titre)*5/2,20,$this->titre,$this->color['crete_color']);
//  X label title
ImageString($this->image,2,$width/2+$startx-strlen($this->titre_labelX)*5/2,$this->Bheight-20,$this->titre_labelX,$this->color['crete_color']);
//  Y label title
ImageStringUp($this->image,2,10,$this->Bheight-$y*$this->nby/2-$starty+strlen($this->titre_labelY)*5/2,$this->titre_labelY,$this->color['crete_color']);

$HeightLigneX            =   $this->Bheight-$starty//will be line of label Y
$decalageTotalZpourX     =   $this->nbz*$x;
$decalageTotalZpourY     =   $this->nbz*$y;
$HauteurTotalY           =   $this->nby*$y;
$LongeurTotalX           =   $this->nbx*$x;

// Y background
ImageFilledpolygon($this->image,array($startx,$HeightLigneX,$decalageTotalZpourX+$startx,$HeightLigneX-$decalageTotalZpourY,$decalageTotalZpourX+$startx,$HeightLigneX-$HauteurTotalY-$decalageTotalZpourY,$startx,$HeightLigneX-$HauteurTotalY),4,$this->color['ycolor']);
// Z(xy) background
ImageFilledpolygon($this->image,array($decalageTotalZpourX+$startx,$HeightLigneX-$y*$this->nbz,$LongeurTotalX+$decalageTotalZpourX+$startx,$HeightLigneX-$y*$this->nbz,$LongeurTotalX+$decalageTotalZpourX+$startx,$HeightLigneX-$HauteurTotalY-$decalageTotalZpourY,$decalageTotalZpourX+$startx,$HeightLigneX-$HauteurTotalY-$decalageTotalZpourY),4,$this->color['xycolor']);
// X background
ImageFilledpolygon($this->image,array($startx,$HeightLigneX,$startx+$LongeurTotalX,$HeightLigneX,$LongeurTotalX+$decalageTotalZpourX+$startx,$HeightLigneX-$decalageTotalZpourY,$decalageTotalZpourX+$startx,$HeightLigneX-$decalageTotalZpourY),4,$this->color['xcolor']);
//Y axe
Imagefilledrectangle($this->image,$startx,$HeightLigneX-$height,$startx+2,$HeightLigneX,$this->color['black']);
// X axe
Imagefilledrectangle($this->image,$startx,$HeightLigneX,$startx+$width,$HeightLigneX+2,$this->color['black']);

//$sub=0;
for($p=0;$p<=$this->nbx;$p++) {
// X label
if ($sub<=$p) {
$i=($i==0)?1:0;
if (
$i==1) {
$espaceX=strlen($this->labelX[$sub])*sqrt(3);
ImageLine($this->image,$startx+$x*$sub,$HeightLigneX,$startx+$x*$sub,$HeightLigneX+5,$this->color['black']);
imageString($this->image1$startx+$x*$sub-$espaceX,$HeightLigneX+12 $this->labelX[$sub], $this->color['black']);
$sub++;
};
if (
$i==0) {
$espaceX=strlen($this->labelX[$sub])*sqrt(3);
ImageLine($this->image,$startx+$x*$sub,$HeightLigneX,$startx+$x*$sub,$HeightLigneX+8,$this->color['black']);
imageString($this->image1$startx+$x*$sub-$espaceX,$HeightLigneX+22 $this->labelX[$sub], $this->color['black']);
$sub++;
};
}
//x diagonal
ImageLine($this->image,$startx+$x*$p,$HeightLigneX,$decalageTotalZpourX+$startx+($x*$p),$HeightLigneX-$decalageTotalZpourY,$this->color['ligne_Z']);
// x de fond vertical
ImageLine($this->image,$decalageTotalZpourX+$startx+($x*$p),$HeightLigneX-$decalageTotalZpourY,$decalageTotalZpourX+$startx+($x*$p),$HeightLigneX-$y*$this->nbz-$this->nby*$y,$this->color['ligne_Z']);
}
for(
$p=0;$p<=$this->nby;$p++) {
//  Y label
imageString($this->image1$startx-strlen($this->labelY[$p])*5-5,$this->Bheight-$y*$p-$starty-$this->labelY[$p], $this->color['black']);
//y diagonale
ImageLine($this->image,$startx,$HeightLigneX-$y*$p,$decalageTotalZpourX+$startx,$HeightLigneX-($y*$p)-$decalageTotalZpourY,$this->color['ligne_Y']);
// y de fond horizontal
ImageLine($this->image,$decalageTotalZpourX+$startx,$HeightLigneX-($y*$p)-$decalageTotalZpourY,$startx+($LongeurTotalX+$x*$this->nbz),$HeightLigneX-($y*$p)-$decalageTotalZpourY,$this->color['ligne_Y']);
}
for(
$p=0;$p<=$this->nbz;$p++) {
// Z label
imageString($this->image1,$startx+($LongeurTotalX+$x*$p)+10 $this->Bheight-$y*$p-$starty$this->labelZ[$p], $this->color['black']);
// y vertical
ImageLine($this->image,$startx+$x*$p,$this->Bheight-$y*$p-$starty,$startx+$x*$p,$this->Bheight-$y*$p-$this->nby*$y-$starty,$this->color['ligne_X']);
// x horizontal
ImageLine($this->image,$startx+$x*$p,$this->Bheight-$y*$p-$starty,$startx+($LongeurTotalX+$x*$p),$this->Bheight-$y*$p-$starty,$this->color['ligne_X']);
};

//---------------------------------------
//-------------drawing lignes------------
//---------------------------------------


$m=1;
$tri=0;
for (
$h=($this->nbcourbe-1);$h>0;$h--) {    // chaque courbe on commence par la derniere
$l=(($h-1)>=0)?($h-1):$h;                   // l courbe inferieure
$tri=($tri==0)?1:0;
$decal[0]=($h)*$x-$x;
$decal[1]=($h)*$x;
$decal[2]=($h)*$y-$y;
$decal[3]=($h)*$y;
$this->nbdonnee=(count($this->datay[$h])<count($this->datay[$l]))?count($this->datay[$l]):count($this->datay[$h]);
for (
$j=0;$j<$this->nbdonnee;$j++) {    // each courb data
$kk=(($j+1)<=$this->nbdonnee)?($j+1):($j);   // kk next data

$courbpointDapres=!isset($this->datay[$h][$kk])?-99:($this->datay[$h][$kk]);
$courbpoint=!isset($this->datay[$h][$j])?-99:($this->datay[$h][$j]);

$courbInfPointDapres=!isset($this->datay[$l][$kk])?-99:($this->datay[$l][$kk]);
$courbInfPoint=!isset($this->datay[$l][$j])?-99:($this->datay[$l][$j]);

if (
$h!=0) {   // as it draw two line at one time, this test is to stop before last line
$coul=($courbpointDapres<$courbpoint)?1:0;

$PinfmoinsX=$startx+$j*$x+$decal[0];    //X coordonate from point (same x as normal x point) from inferior line
$PinfplusX=$startx+$kk*$x+$decal[0];    //X coordonate from next point inferior line
$PsupmoinsX=$startx+$j*$x+$decal[1];    //X coordonate from point
$PsupplusX=$startx+$kk*$x+$decal[1];    //X coordonate from next point
$PinfmoinsY=$HeightLigneX-($courbInfPoint*$y)-$decal[2];          //Y coordonate from point (same y as normal x point) from inferior line
$PinfplusY=$HeightLigneX-($courbInfPointDapres*$y)-$decal[2];     //Y coordonate from next point inferior line
$PsupmoinsY=$HeightLigneX-($courbpoint*$y)-$decal[3];             //Y coordonate from point
$PsupplusY=$HeightLigneX-($courbpointDapres*$y)-$decal[3];        //Y coordonate from next point
if ($courbpoint>=&& $courbpointDapres>=&& $courbpoint!=-99 && $courbpointDapres!=-99) {
// drawing the line with 3 pixels
ImageLine($this->image,$PsupmoinsX,$PsupmoinsY,$PsupplusX,$PsupplusY,$this->ligne_color[$h][0]);
ImageLine($this->image,$PsupmoinsX,$PsupmoinsY-1,$PsupplusX,$PsupplusY-1,$this->ligne_color[$h][0]);
ImageLine($this->image,$PsupmoinsX,$PsupmoinsY-2,$PsupplusX,$PsupplusY-2,$this->ligne_color[$h][0]);
};
if (
$courbpoint>=&&  $courbInfPoint>=&& $courbpoint!=-99 && $courbInfPoint!=-99 ) {
// drawing the top line from point to point with same x coordonate
ImageDashedLine($this->image,$PinfmoinsX,$PinfmoinsY-1,$PsupmoinsX,$PsupmoinsY-1,$this->color['crete_color']);
ImageDashedLine($this->image,$PinfmoinsX,$PinfmoinsY,$PsupmoinsX,$PsupmoinsY,$this->color['crete_color']);
}
if (!isset(
$gama)) { $gama=0; }
if (!isset(
$teta)) { $teta=0; }
if (!isset(
$beta)) { $beta=0; }
if (!isset(
$alpha)) { $alpha=0; }

//
//     A     B
//
//  C     D
//
// first if all points exists and if gama=0 or tri=0 it draw CBA triangle and change alpha to 1
if ($courbpoint>=&& $courbpointDapres>=&& $courbInfPoint>=0  && $courbpoint!=-99 && $courbpointDapres!=-99 && $courbInfPoint!=-99 && ($tri==|| $gama==0)) {
$alpha=ImageFilledPolygon($this->image,array(
$PinfmoinsX,$PinfmoinsY,  //
$PsupplusX,$PsupplusY,
$PsupmoinsX,$PsupmoinsY),
3,$this->courbe_color[$l][$coul]);
$coul=($courbInfPointDapres<$courbInfPoint)?1:0;
}
// next if all points exists and if gama=0 or tri=0 it draw CDB and change beta to 1
if ($courbInfPoint>=&& $courbInfPointDapres>=&& $courbpointDapres>=0  && $courbInfPoint!=-99 && $courbpointDapres!=-99 && $courbInfPointDapres!=-99 && ($tri==|| $teta==0)) {
$beta=ImageFilledPolygon($this->image,array(
$PinfmoinsX,$PinfmoinsY,
$PinfplusX,$PinfplusY,
$PsupplusX,$PsupplusY),
3,$this->courbe_color[$l][$coul]);
}
// reset gama and teta
$gama=0$teta=0;
// if all points exists and if alpha=1 or beta=1 or tri=0 draw CB diagonal

if ($this->diago=='oui' && ($tri==|| $alpha==|| $beta==1) && $courbpoint!=-99 && $courbInfPoint!=-99 && $courbInfPointDapres!=-99 && $courbpointDapres!=-99) {
ImageDashedLine($this->image,$PinfmoinsX,$PinfmoinsY,$startx+($j+1)*$x+$decal[1],$HeightLigneX-($this->datay[$h][$j+1]*$y)-$decal[3],$this->color['diagonal1']);
}

$coul=($courbInfPointDapres<$courbInfPoint)?1:0;

// if all points exists and if alpha=0 or tri=1 it draw CDA and change gama to 1
if ($courbpoint>=&& $courbInfPointDapres>=&& $courbInfPoint>=0  && $courbpoint!=-99 && $courbInfPointDapres!=-99 && $courbInfPoint!=-99 && ($tri==|| $alpha==0)) {
$gama=ImageFilledPolygon($this->image,array(
$PinfmoinsX,$PinfmoinsY,
$PinfplusX,$PinfplusY,
$PsupmoinsX,$PsupmoinsY),
3,$this->courbe_color[$l][$coul]);
$coul=($courbpointDapres<$courbpoint)?1:0;
}
// if all points exists and if beta=0 or tri=1 it draw DBA and change teta to 1
if ($courbpoint>=&& $courbInfPointDapres>=&& $courbpointDapres>=0  && $courbpoint!=-99 && $courbpointDapres!=-99 && $courbInfPointDapres!=-99 && ($tri==|| $beta==0)) {
$teta=ImageFilledPolygon($this->image,array(
$PinfplusX,$PinfplusY,
$PsupplusX,$PsupplusY,
$PsupmoinsX,$PsupmoinsY),
3,$this->courbe_color[$l][$coul]);
}

// if all points exists and if gama=1 or teta=1 or tri=1 draw CB diagonal
if ($this->diago=='oui' && ($tri==|| $gama==|| $teta==1)  && $courbpoint!=-99 && $courbInfPoint!=-99 && $courbInfPointDapres!=-99 && $courbpointDapres!=-99) {
ImageDashedLine($this->image,$startx+($j+1)*$x+$decal[0],$HeightLigneX-($this->datay[$l][$j+1]*$y)-$decal[2],$startx+($j)*$x+$decal[1],$PsupmoinsY,$this->color['diagonal2']);
};
// reset alpha and beta
$alpha=0$beta=0;

};  
// end (if ($h!=0) {   // as it draw two line at one time, this test is to stop before last line)
if ($courbInfPoint>=&& $courbInfPointDapres>=&& $courbInfPoint!=-99 && $courbInfPointDapres!=-99) {
// drawing inferior line
ImageLine($this->image,$PinfmoinsX,$PinfmoinsY-1,$PinfplusX,$PinfplusY-1,$this->ligne_color[$l][0]);
ImageLine($this->image,$PinfmoinsX,$PinfmoinsY-2,$PinfplusX,$PinfplusY-2,$this->ligne_color[$l][0]);
ImageLine($this->image,$PinfmoinsX,$PinfmoinsY,$PinfplusX,$PinfplusY,$this->ligne_color[$l][0]);
};
$tri=($tri==0)?1:0;
};  
// end drawing this data
}  //end drawing this line
}

function 
create() {
Imagegif($this->image$this->filename);
}

function 
destroy() {
ImageDestroy($this->image);

}

}
?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories