Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

reading matrix from a file and drawing a graph

images php coding issues or problems here.

Moderators: macek, egami, gesf

reading matrix from a file and drawing a graph

Postby nemo8130 » Sun Feb 09, 2014 6:48 am

Hi !

I am a newcomer to the world of php. I am trying to code for a script which can draw a graph (network) from a given (1-0) adjacency matrix. In attempt to do so, I have managed to write two separate scripts.
The first one reads the matrix from a file and echos the same in form of an html output.

whereas the second one generates a random graph (edges are randomly generated and thus keeps changing at each run of the program) and does the drawing successfully. I am using 'imagearc' and 'imageline' for the drawing with header("Content-type: image/png"); whenever I am trying to 'echo' some text output from this program it is saying 'errors' !!! Is it possible for a php to read from a file and give text (html) output as well as draw the image. I do not want to change the 'drawing' module unless I find that there's no compatibility atoll with the 'read-and-echo' part of the script.

Please see the codes and output screenshots below.

1. draw.php (This one creates a random graph and does the drawing successfully)

==============================================================================
Code: Select all
<?php
// top left is (0,0)
$array = array();

$N = 12;     // Number of nodes in the graph
for ($i=0;$i<=$N;$i++)
{
        for ($j=0;$j<=$N;$j++)
        {
        $r = rand(0,1);
                if ($i == $j)
                {
                $array[$i][$j] = 0;
                }
                else
                {
                        if ($r >= 0.5)
                        {
                        $array[$i][$j] = 1;
                        $array[$j][$i] = 1;
                        }
                        else
                        {
                        $array[$i][$j] = 0;
                        $array[$j][$i] = 0;
                        }
                }
        }
}


$c = 0;
$tot = 0;

for ($i=0;$i<=$N;$i++)
{
        for ($j=0;$j<=$N;$j++)
        {
        $tot++;
                if ($array[$i][$j] == $array[$j][$i])
                {
                $c++;
                }
//        echo $array[$i][$j]," \n";
        }
//echo "<br>\n";
}

if ($tot == $c)
{
//print "Symmetric<br>\n";
}

// create a 200*200 image
$img = imagecreatetruecolor(750, 750);

// Description : imagearc($img,$cx,$cy,$hight,$width,$start,$end,$color);

// allocate some colors
$white = imagecolorallocate($img, 255, 255, 255);
$red   = imagecolorallocate($img, 255,   0,   0);
$green = imagecolorallocate($img,   0, 255,   0);
$blue  = imagecolorallocate($img,   0,   0, 255);
$yellow  = imagecolorallocate($img,   255,   255, 0);
$silver = imagecolorallocate($img,   192,   192, 192);
$snow = imagecolorallocate($img,   255,   250, 250);
$hotpink = imagecolorallocate($img, 255, 105, 180);
$pink = imagecolorallocate($img, 255, 192, 203);
$orange = imagecolorallocate($img, 255, 165, 0);
$coral = imagecolorallocate($img, 255, 127, 80);


$col = array($white,$red,$green,$blue,$yellow,$silver,$snow,$hotpink,$pink,$orange,$coral);

$style = array($red,$red,$red,$red,$red,$white,$white,$white,$white,$white,$white);

$cnt = 0;

$delth = round(floatval(360/$N));
$rad = 300;
$cx = array();
$cy = array();

for ($i = 0; $i <= 360; $i+=$delth)
{
$cx1 = 350+$rad*cos(deg2rad(floatval($i)));
$cy1 = 350+$rad*sin(deg2rad(floatval($i)));
array_push($cx,$cx1);
array_push($cy,$cy1);
$color = $col[$cnt];
imagearc($img, $cx1, $cy1, 25, 25,  0, 360, $white);
//imagefilledarc($img, $cx1, $cy1, 25, 25,  0, 360, $white, $style);
//imagearc($img, $cx1, $cy1, 50, 50,  0, 360, $col[$cnt]);
//imageline($img, $cx1, $cy1, $cx1, $cy1, $white);
//imagefill ($img, $cx1, $cy1, $blue);
$cnt++;
}


// connect nodes by edges

for ($i=0;$i<=$N;$i++)
{
   for ($j=0;$j<=$N;$j++)
   {
      if ($array[$i][$j] == 1)
      {
      // connect
      imageline ($img,$cx[$i],$cy[$i],$cx[$j],$cy[$j],$white);
      }
   }
}

// output image in the browser
header("Content-type: image/png");
imagepng($img);
//imagefill ($img,0,0,$white);

// free memory
imagedestroy($img);

?>


graph1.png
screenshot of the image output
graph1.png (89.83 KiB) Viewed 292 times

===============================================================================




2. mat.php (this script reads the adjacency matrix from a text file and echoes it in an html page

Code: Select all
<html>
<body>


<?php
$file = fopen("graph.adj","r") or exit ("Unable to open file !");
if (feof($file)) echo "End of file";
// Output a line of the file until the end is reached

$arr1 = array();
$i = 0;
   while (!feof($file))
   {
   $elm = fgets($file);
   //echo $elm. "<br>";
   $arr1[$i] = $elm;   
   $i++;
   }
fclose($file);


   $M = $i-1;
//   echo $M,"<br>\n";

   for ($i=0; $i < $M-1; $i++)
   {
//   $arr2 = array();
   $str = $arr1[$i];
//   echo $str,"<br><br>\n";
   $arr2 = explode(" ",$str);
   $N = sizeof($arr2);
//   echo "$i~~~~~$N==========<br>\n";
      for ($j=0;$j<=sizeof($arr2);$j++)
      {
      $narr[$i][$j] = $arr2[$j];
//      echo $arr2[$j],"<br>\n";
      }
//   echo "<br><br>\n";
   }

//   echo $N,"==========<br>\n";

//   echo "$M  <br>  $N  <br>\n";

   for ($i=0;$i<=$M;$i++)
   {
      for ($j=0;$j<=$N;$j++)
      {
      echo $narr[$i][$j]," ";
      }
   echo "<br>\n";
   }

?>
</body>
</html>


adjmat.png
screenshot of the echoed matrix in html
adjmat.png (34.06 KiB) Viewed 292 times

===============================================================================

I repeat, I have to combine the two scripts. i.e., the same program should be able to read the matrix and draw the graph.

Any suggestion would be of great help.

thanks and regards, sankar
nemo8130
New php-forum User
New php-forum User
 
Posts: 1
Joined: Sun Feb 09, 2014 6:07 am

Re: reading matrix from a file and drawing a graph

Postby seandisanti » Sat Apr 12, 2014 8:13 pm

You're over thinking it man, just put your code into functions in the same file and call them when you need them to do their thing.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => Images

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.