codice:
<?php
error_reporting(E_ALL);
# COORDINATE STRUTTURA LETTERA A
$punti=array(
array(
0,200,
25,100,
50,0,
75,100,
25,100,
),
array(
75,100,
100,200
)
);
/*
# COORDINATE STRUTTURA LETTERA B
$punti=array(
array(
0,50,
0,0,
50,0,
75,25,
50,50,
0,50
),
array(
50,50,
75,75,
50,100,
0,100,
0,50
)
);
*/
//-------------------------------------
function coordinate($punti,&$x,&$y){
foreach($punti as $chiavesottonodo=>$sottonodo){
foreach($sottonodo as $chiave=>$valore){
if($chiave % 2 == 0)
$x[$chiavesottonodo][]=$valore;
else
$y[$chiavesottonodo][]=$valore;
}
}
}
//-------------------------------------
$x=array();
$y=array();
coordinate($punti,$x,$y);
$start_x=50;
$start_y=50;
$dim_x=400;
$dim_y=400;
$im=imagecreate($dim_x,$dim_y);
$bianco=imagecolorallocate($im, 255, 255, 255);
$nero=imagecolorallocate($im, 0, 0, 0);
#imagesetthickness($im,2);
$distanza=5;
function estremi($x,$y,$d){
if ($y[1]-$x[1] == 0){
$temp_a[0]=$x[0];
$temp_a[1]=$x[1]-$d;
$temp_b[0]=$x[0];
$temp_b[1]=$x[1]+$d;
$temp_c[0]=$y[0];
$temp_c[1]=$y[1]-$d;
$temp_d[0]=$y[0];
$temp_d[1]=$y[1]+$d;
} elseif ($y[0]-$x[0] == 0) {
$temp_a[0]=$x[0]+$d;
$temp_a[1]=$x[1];
$temp_b[0]=$x[0]-$d;
$temp_b[1]=$x[1];
$temp_c[0]=$y[0]+$d;
$temp_c[1]=$y[1];
$temp_d[0]=$y[0]-$d;
$temp_d[1]=$y[1];
} else {
$a=1;
$b= -($y[0]-$x[0])/($y[1]-$x[1])*$a;
$c=-$a*$x[0]-$b*$x[1];
$m= -1/(($y[1]-$x[1])/($y[0]-$x[0]));
$q=$x[1]-$m*$x[0];
$temp_a[0]=($d*sqrt($a*$a+$b*$b)-$c-$b*$q)/($a+$b*$m);
$temp_a[1]=$m*$temp_a[0]+$q;
$temp_b[0]=(-$d*sqrt($a*$a+$b*$b)-$c-$b*$q)/($a+$b*$m);
$temp_b[1]=$m*$temp_b[0]+$q;
$q=$y[1]-$m*$y[0];
$temp_c[0]=($d*sqrt($a*$a+$b*$b)-$c-$b*$q)/($a+$b*$m);
$temp_c[1]=$m*$temp_c[0]+$q;
$temp_d[0]=(-$d*sqrt($a*$a+$b*$b)-$c-$b*$q)/($a+$b*$m);
$temp_d[1]=$m*$temp_d[0]+$q;
}
return array($temp_a, $temp_b, $temp_c, $temp_d);
}
function imageline_strana($im,$x,$y,$d,$nero){
$riempimento_or_ver=100;
$riempimento_ob=1000;
list($a,$b,$c,$d)=estremi($x,$y,$d);
#imageline($im,$a[0],$a[1],$b[0],$b[1],$nero);
#imageline($im,$a[0],$a[1],$c[0],$c[1],$nero);
#imageline($im,$d[0],$d[1],$c[0],$c[1],$nero);
#imageline($im,$d[0],$d[1],$b[0],$b[1],$nero);
$min_x=min($a[0],$b[0],$c[0],$d[0]);
$max_x=max($a[0],$b[0],$c[0],$d[0]);
$min_y=min($a[1],$b[1],$c[1],$d[1]);
$max_y=max($a[1],$b[1],$c[1],$d[1]);
if ($b[0]-$a[0] == 0 || $b[1]-$a[1] == 0){
for($i=0;$i<$riempimento_or_ver;$i++){
$temp=mt_rand($min_x,$max_x);
$temp_2=mt_rand($min_y,$max_y);
imageline($im, $temp, $temp_2, $temp, $temp_2, $nero);
}
} else {
$m1=($b[1]-$a[1])/($b[0]-$a[0]);
$q1=$a[1]-$m1*$a[0];
$m2=-1/$m1;
$q2=$a[1]-$m2*$a[0];
$m3=$m1;
$q3=$c[1]-$m3*$c[0];
$m4=$m2;
$q4=$d[1]-$m4*$d[0];
if($m1 > 0 && $q1 < 0){
for($i=0;$i<$riempimento_ob;$i++){
$temp=mt_rand($min_x,$max_x);
$temp_2=mt_rand($min_y,$max_y);
if(
( $temp_2 > $m1 * $temp + $q1 ) &&
( $temp_2 < $m2 * $temp + $q2 ) &&
( $temp_2 < $m3 * $temp + $q3 ) &&
( $temp_2 > $m4 * $temp + $q4 )
)
imageline($im, $temp, $temp_2, $temp, $temp_2, $nero);
}
} elseif ($m1 < 0 && $q1 > 0) {
for($i=0;$i<$riempimento_ob;$i++){
$temp=mt_rand($min_x,$max_x);
$temp_2=mt_rand($min_y,$max_y);
if(
( $temp_2 > $m1 * $temp + $q1 ) &&
( $temp_2 > $m2 * $temp + $q2 ) &&
( $temp_2 < $m3 * $temp + $q3 ) &&
( $temp_2 < $m4 * $temp + $q4 )
)
imageline($im, $temp, $temp_2, $temp, $temp_2, $nero);
}
} elseif ($m1 > 0 && $q1 == 0) {
for($i=0;$i<$riempimento_ob;$i++){
$temp=mt_rand($min_x,$max_x);
$temp_2=mt_rand($min_y,$max_y);
if(
( $temp_2 > $m1 * $temp + $q1 ) &&
( $temp_2 < $m2 * $temp + $q2 ) &&
( $temp_2 < $m3 * $temp + $q3 ) &&
( $temp_2 > $m4 * $temp + $q4 )
)
imageline($im, $temp, $temp_2, $temp, $temp_2, $nero);
}
}
elseif ($m1 > 0 && $q1 > 0) {
for($i=0;$i<$riempimento_ob;$i++){
$temp=mt_rand($min_x,$max_x);
$temp_2=mt_rand($min_y,$max_y);
if(
( $temp_2 < $m1 * $temp + $q1 ) &&
( $temp_2 < $m2 * $temp + $q2 ) &&
( $temp_2 > $m3 * $temp + $q3 ) &&
( $temp_2 > $m4 * $temp + $q4 )
)
imageline($im, $temp, $temp_2, $temp, $temp_2, $nero);
}
} else {
for($i=0;$i<$riempimento_ob;$i++){
$temp=mt_rand($min_x,$max_x);
$temp_2=mt_rand($min_y,$max_y);
if(
( $temp_2 < $m1 * $temp + $q1 ) &&
( $temp_2 < $m2 * $temp + $q2 ) &&
( $temp_2 > $m3 * $temp + $q3 ) &&
( $temp_2 > $m4 * $temp + $q4 )
)
imageline($im, $temp, $temp_2, $temp, $temp_2, $nero);
}
}
}
}
$temp=array();
foreach($x as $chiavesottonodo=>$sottonodo){
for($i=0, $j=count($sottonodo)-1; $i<$j;$i++){
$punto_inizio=array($start_x + $x[$chiavesottonodo][$i] ,$start_y + $y[$chiavesottonodo][$i]);
$punto_fine=array($start_x + $x[$chiavesottonodo][$i+1] ,$start_y + $y[$chiavesottonodo][$i+1]);
imageline_strana($im, $punto_inizio, $punto_fine, $distanza, $nero);
}
}
$max=ceil($dim_x * $dim_y / 20);
for($i=0;$i<$max;$i++){
$temp_x=mt_rand(1,$dim_x);
$temp_y=mt_rand(1,$dim_y);
imageline ($im, $temp_x, $temp_y, $temp_x, $temp_y, $nero);
}
header("content-type: image/jpeg");
imagejpeg($im,"",80);
imagedestroy($im);
?>