<?php

function position($nbdep)
{global $x_pt,$y_pt;
mt_srand((double)microtime()*1000000);
$x_pt=250;$y_pt=250;
for ($i=1;$i<=$nbdep;$i++)
{$dep=ceil(mt_rand(0,4));
switch($dep)
{CASE 0: $x_pt=$x_pt-2;break;
CASE 1: $x_pt=$x_pt+2;break;
CASE 2: $y_pt=$y_pt-2;break;
CASE 3: $y_pt=$y_pt+2;break;}
}}

function faire_image($nbdep,$nbessais)
{global $x_pt,$y_pt,$num;
$image=imagecreate(500,500);
$col_fond = ImageColorAllocate($image, 200,200,200);
$col1 = ImageColorAllocate($image,255,255,255);
$col2 = ImageColorAllocate($image,255,0,0);
$col3 = ImageColorAllocate($image,0,255,0);
imagefill($image,0, 0,$col_fond);
imagefilledrectangle ($image,20,20,480,480,$col1);
for ($j=1;$j<=$nbessais;$j++)
{position($nbdep);
imagerectangle($image,$x_pt-1,$y_pt-1, $x_pt+1,$y_pt+1,$col2); 
}
$diam=sqrt(30*$nbdep/M_PI);
imagearc ($image,250,250,$diam,$diam,0,360,$col3);
ImagePng($image);
}

function faire_page()
{global $nbdep,$nbessais,$num;
echo "<html><body>";
echo "<form METHOD='GET' ACTION='ivrogne.php3?action=niv&'>\n";
echo "<table border='1' width='100%'>\n";
echo "<tr><td width='25%'><b>Nombres de déplacements</b></td>\n";
echo "<td width='20%'><input type='text' name='nbdep' size='15' value='".$nbdep."'></td>\n";
echo "<td width='25%'><b>Nombre d'essais</b></td><td width='20%'>";
echo "<input type='text' name='nbessais' size='15' value='".$nbessais."'></td>\n";
echo "<td width='10%'><INPUT TYPE=SUBMIT VALUE='OK'>\n";
echo "</tr></table></form>\n";
echo "<BR>";
echo "<img src='ivrogne.php3?action=img&nbdep=".$nbdep."&nbessais=".$nbessais."' width='500' height='500'>";
echo "</body></html>";
}
if (isset($_GET["action"]))
	$action = $_GET["action"]; else $action = "niv";
if (isset($_GET["nbdep"]))
	$nbdep = $_GET["nbdep"]; else $nbdep = 100;
if (isset($_GET["nbessais"]))
	$nbessais = $_GET["nbessais"]; else $nbessais = 10;
if ($action=="img") {faire_image($nbdep,$nbessais);} else {faire_page();}


?>