Network topology layout algorithm based on repulsion-tension model (PHP code)

Source: Internet
Author: User

Overwrite according to the previous C + + code:

<?php//**********************************************************////date:2014.11.07 @ bupt//This php file includes vertex class point , graph graph, and related functions the input for the constructor of the//graph object is (vertex number: $_nv,x axis min: $_xmin,x axis maximum: $_xmax,y axis min: $_ymin,y axis maximum: $_ymax, adjacency matrix $_mat)// The Initlayoutproc () method of the graph object is to initialize the layout process//graph the object's layout () method is to make a layout once///when the graph object's member variable temperature T is less than 1 o'clock, stop the layout and pan and zoom according to the canvas size//****** The sample program is as follows://$tNv = number of 4;//vertices//$txmin = 0;//x axis minimum//$txmax = 400;//x axis Maximum//$tymin = 0;//y Axis min//$tymax = 400;//y axis maximum//////adjacency matrix//$tmat = Array (//0=>array (0,1,1,0),//1=>array ( 1,0,1,1),//2=>array (1,1,0,1),//3=>array (0,1,1,0)//);//////$g = new Graph ($tNv, $txmin, $txmax, $tymin, $tymax, $ Tmat);//Initialize topology diagram//$g->initlayoutproc ();//Initialize layout process//$g->output ();//output Diagram//while ($g->t >= 1) {////$g Layout ();////}//$g->layout ();//$this->t<1 when the layout is complete, pan and zoom//$g->output ();////************** Required function init3 ($v, $x, $y) {//Initialize $v[0]= $x; $v [1]=$y; return $v;} function Len3 ($v) {//Calculation $len=sqrt ($v [0]* $v [0]+ $v [1]* $v [1]); if ($len ==0) $len =0.001;return $len;} function Len3_2 ($v) {//Calculate squared and return $v [0]* $v [0]+ $v [1]* $v [1];} function sub3 ($a, $b, $rs) {//The difference $rs[0]= of the calculated vector $a [0]-$b [0]; $rs [1]= $a [1]-$b [1];return $rs;} function add3 ($a, $b, $rs) {//$rs[0]= $a [0]+ $b [0]; $rs [1]= $a [1]+ $b [1];return $rs;} function Mul3 ($k, $v, $rs) {//The product of the calculated vector and constant $rs[0]= $k * $v [0]; $rs [1]= $k * $v [1];return $rs;} Function fa ($d, $K) {//Gravity shift return $d * $d/$K;} function fr ($d, $K) {//Repulsion displacement return $K * $K/$d;} Vertex class Point{var $pos;//point position var $offset;//point displacement var $weight;// The weight of the point var $degree;//The degree of the point var $force;//resultant function __construct ()//constructor {$this->pos = array (0,0); $this->offset = array (0,0); $this->weight=0; $this->degree=0; $this->force=array (0,0);}} Graph class Graph{var $Nv;//number of vertices var $xmin;//x Axis minimum var $xmax//x Axis maximum var $ymin//y axis minimum var $ymax//y axis maximum var $mat;// Adjacency matrix vector<vector<bool> > Mat;var $vlist;//Vertex list vector<cvertex> vlist;//gravity-Repulsion model Var $K;//gravity constant var $T;//current temperature var $Jf;//The previous target function value var $decay;//attenuation constant function __construct ($_nv,$_xmin,$_xmax,$_ymin,$ _ymax,$_mat)//complex constructor {$this->nv=$_nv; $this->xmin=$_xmin; $this->xmax=$_xmax; $this->ymin=$_ymin;$ this->ymax=$_ymax; $this->mat=$_mat; $this->decay=0.85;for ($i =0; $i <$_Nv; $i +) {$this->vlist[$i]= New Point (); $this->vlist[$i]->pos[0] = rand ($this->xmin, $this->xmax); $this->vlist[$i]->pos[1] = Rand ($this->ymin, $this->ymax);}} function Shutlayoutproc () {//close layout process $this->t=0;} function Initlayoutproc () {//Initialize layout procedure//Initialize gravitational constant $inf=100000000; $area = ($this->xmax-$this->xmin) * ($this ymax-$this->ymin); $this->k=pow ($area/$this->nv,0.5);//Initialize temperature $this->t= ($this->xmax-$this->xmin );//Initialize the value of the target function $this->jf= $INF;//initialize attenuation coefficient $decay=0.9;} function layout () {//Once layout $inf=100000000;if ($this->t<=1) {//t for current temperature//At this time the layout has been completed, panning and zooming//seeking the external rectangle $left= $INF; $ right=-$INF $up =-$INF $down = $INF; for ($i =0; $i <= $this->nv-1; $i + +) {if ($this->vlist[$i]>pos[0]< $left) $left = $this->vlist[$i]->pos[0];if ($this->vlist[$i]->pos[0]> $right) $right =$ this->vlist[$i]->pos[0];if ($this->vlist[$i]->pos[1]> $up) $up = $this->vlist[$i]->pos[1];if ($ this->vlist[$i]->pos[1]< $down) $down = $this->vlist[$i]->pos[1];} Get Left,right,up,downecho "</br>left:". $left. ", right:". $right. ", Up:". $up. ", Down:". $down. " </br></br> "//For external Rectangle Center $_cx= ($left + $right)/2;$_cy= ($up + $down)/2;//Center $cx= ($this->xmin+ $this- >xmax)/2; $cy = ($this->ymin+ $this->ymax)/2;//the translation vector from the center of the external rectangle to the center of the Artboard $vec = Array ($cx-$_cx, $cy-$_cy);// Pans the graphic by a translation vector, but moves only one of several points, not one step, for the animated effect for ($i =0; $i <= $this->nv-1; $i + +) {$this->vlist[$i]->pos[0]+= $vec [ 0]; $this->vlist[$i]->pos[1]+= $vec [1];} The maximum margin of the bounding rectangle is $r=max (($this->xmax-$this->xmin)/2, ($this->ymax-$this->ymin)/2), and/or the maximum amount of margin to be added $dr=min ( ($this->xmax-$this->xmin)/2-($right-$left)/2, ($this->ymax-$this->ymin)/2-($up-$down)/2); $dr = $dr-($ this->xmax-$thisxmin)/15;//prevent the Sky//$_dr= $DR/1000;//only to take the maximum amount of one of the points, for the animation effect//Zoom factor $k= ($r + $dr)/$r;//press K to zoom the graph, for ($i =0; $i <=$ this->nv-1; $i + +) {$this->vlist[$i]->pos[0]= ($this->vlist[$i]->pos[0]-$_cx) * $k +$_cx; $this vlist[$i]->pos[1]= ($this->vlist[$i]->pos[1]-$_cy) * $k +$_cy;} return;} Restores the offset of each vertex to 0for ($i =0; $i <= $this->nv-1; $i + +) {$this->vlist[$i]->offset = init3 ($this->vlist[$i] ->offset,0,0);} Displacement for ($i =0; $i <= $this->nv-1; $i + +) under Repulsion action {for ($j =0; $j < $i; $j + +) {//Calculate displacement vector offset and _offset$diff=array (0,0) ;/*echo "I:". $i. ", J:". $j, echo "</br>vlist_i_pos:"; Echo $this->vlist[$i]->pos[0];echo ","; Echo $this- >vlist[$i]->pos[1];echo "</br>", echo "</br>vlist_j_pos:", Echo $this->vlist[$j]->pos[0]; echo ","; Echo $this->vlist[$j]->pos[1];echo "</br>"//*/$diff = sub3 ($this->vlist[$i]->pos, $this- >vlist[$j]->pos, $diff), $len _diff = Len3 ($diff);//|diff|/*echo "</br>diff:"; echo $diff [0];echo], "; Echo $ Diff[1];echo "</br> "; echo" </br>len_diff: "Echo $len _diff;echo" </br> "//*/$e _diff = Array (0,0);//diff/|diff| $e _ diff = mul3 (1.0/$len _diff, $diff, $e _diff); $offset = Array (0,0);//(Diff/|diff|) *FR (|diff|) $offset = Mul3 (FR ($len _diff, $this->k), $e _diff, $offset); $_offset = Array (0,0);//-offset$_offset = Mul3 ( -1, $offset, $_offset)///Cumulative displacement $this->vlist[$i]->offset = add3 ($this->vlist[$i]->offset, $offset, $this->vlist[$i ]->offset); $this->vlist[$j]->offset = add3 ($this->vlist[$j]->offset,$_offset, $this->vlist[$j] >offset);}} Displacement for ($i =0; $i <= $this->nv-1; $i + +) under gravitational action {for ($j =0; $j < $i; $j + +) {if ($this->mat[$i] [$j]) {// Calculate displacement vectors offset and _offset$diff=array (0,0), $diff =sub3 ($this->vlist[$i]->pos, $this->vlist[$j]->pos, $diff) ; $len _diff=len3 ($diff);//|diff| $e _diff=array (0,0);//diff/|diff| $e _diff=mul3 (1.0/$len _diff, $diff, $e _diff); $ Offset=array (0,0);//(Diff/|diff|) *FR (|diff|) $offset =mul3 (FA ($len _diff, $this->k), $e _diff, $offset); $_offset=array (0,0);//-offset$_Offset=mul3 ( -1, $offset, $_offset);//Cumulative displacement $this->vlist[$i]->offset=add3 ($this->vlist[$i]->offset,$_ Offset, $this->vlist[$i]->offset) $this->vlist[$j]->offset=add3 ($this->vlist[$j]->offset,$ Offset, $this->vlist[$j]->offset);}}} ($i =0; $i <= $this->nv-1; $i + +) {$len _offset=array (0,0);/*echo "</br>vlist_offset"; echo $this->vlist[$i]->offset[0];echo ","; Echo $this->vlist[$i]->offset[1];echo "</br>"//*/$len _offset=len3 ($this->vlist[$i]->offset);//The displacement of the modulus $e_offset=array (0,0);//direction of displacement $e_offset=mul3 (1.0/$len _ Offset, $this->vlist[$i]->offset, $e _offset);//Get E_offset$len_offset=min ($len _offset, $this->t);//Get Len_ Offset$offset=array (0,0)///Displacement $offset=mul3 ($len _offset, $e _offset, $offset);//Get Offset/*echo "</br>len_ Offset: "Echo $len _offset;echo" </br> "echo" </br>e_offset: "Echo $e _offset[0];echo", "Echo $e _offset[1 ];echo "</br>", echo "</br>offset:", Echo $offset [0];echo ","; Echo $oFfset[1];echo "</br>",//*/$this->vlist[$i]->pos=add3 ($this->vlist[$i]->pos, $offset, $this vlist[$i]->pos);} Cooling $this->t= $this->decay* $this->t;} function OutPut () {echo "point number:". $this->nv;echo "</br>"; echo "</br>"; echo "adjacent Matrix:"; echo "</br>"; for ($i =0; $i < $this->nv; $i +) {for ($j =0; $j < $this->nv; $j + +) {echo $this->mat[$i] [$j ];echo "";} echo "</br>";} echo "</br>", echo "x:". $this->xmin. ",". $this->xmax;echo "</br>"; echo "y:". $this->ymin. ",". $ This->ymax;echo "</br>", echo "</br>", echo "repulsive Const:". $this->k;echo "</br>"; echo "now Temperature: ". $this->t;echo" </br> "echo" Prev Function Value: ". $this->jf;echo" </br> "; echo" Attenuation Const: ". $this->decay;echo" </br> "echo" </br> "echo" point Pos: "; echo" </br> "; for ($ I=0, $i < $this->nv; $i + +) {echo $i, echo ":", Echo $this->vlist[$i]->pos[0];echo ","; Echo $thisvlist[$i]->pos[1];echo "</br>";}} echo $INF; $tNv = number of 4;//vertices $txmin = 0;//x Axis min $txmax = 400;//x Axis maximum $tymin = 0;//y Axis min $tymax = 400;//y Axis maximum//adjacency matrix $tmat = Arra Y (0=>array (0,1,1,0), 1=>array (1,0,1,1), 2=>array (1,1,0,1), 3=>array (0,1,1,0)); $g = new Graph ($tNv, $txmin , $txmax, $tymin, $tymax, $tmat);//Initialize the topology diagram $g->initlayoutproc ();//Initialize the layout process $g->output ();//output diagram while ($g->t >= 1) {$g->layout ();//Layout} $g->output ();//$g->layout ();//$this->t<1 when the layout part is complete, panning and zooming $g-> OutPut ();//?>

Network topology layout algorithm based on repulsion-tension model (PHP code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.