Desafío Poligonator
#3 Solución de EnZo
Ver código
|
Ver comentarios (2)
|
Descargar código
Fecha: 24 agosto 2005
Tamaño: 26675 caracteres
Comentarios: 2
Solución online:
Lo siento, pero no está disponible
Valoración









5.00
(3 votos)
"Para ser sincero me ha costado, aunque la dificultad sea baja. Mi problema era que no sabia solucionar el problema matematico, pero con ayuda de un amigo lo saque y pasarlo a php fue pan comido."
Valora esta solución
Esta dividido en dos archivos uno el formulario (poligonator.php) y otro la foto (img.php)
poligonator.php
---------------------------------------------------------------------------
<?
$expreg='^[a-fA-F0-9]{6}$';
$vertices = ((!$_GET['ver']) || $_GET['ver']<3) ? 3 : $_GET['ver'];
$radio = ((!$_GET['radio'])) ? 50 : $_GET['radio'];
$fondo = (!ereg($expreg,$_GET['fondo'])) ? 'DDDDDD' : $_GET['fondo'];
$figura = (!ereg($expreg,$_GET['figura'])) ? '333333' : $_GET['figura'];
$puntos = (!ereg($expreg,$_GET['puntos'])) ? '00FF00' : $_GET['puntos'];
$circulo = (!ereg($expreg,$_GET['circulo'])) ? '910BFF' : $_GET['circulo'];
?>
<script>
document.onmousemove = posiraton;
x=0; y=0
function posiraton(e) {
if (navigator.appName == "Netscape") { x=e.pageX; y=e.pageY }
else { x=event.clientX + document.body.scrollLeft; y=event.clientY + document.body.scrollTop }
document.getElementById("x").value=x + 'x'
document.getElementById("y").value=y + 'y'
}
reg=/^[a-fA-F0-9]{6}$/
function color (id,color) {
if (reg.test(color)) document.getElementById(id).style.backgroundColor=color;
}
function carga () {
color("fondo","<?=$fondo;?>");
color("figura","<?=$figura;?>");
color("puntos","<?=$puntos;?>");
color("circulo","<?=$circulo;?>");
}
</script>
<body style="margin:0px" onLoad="carga()">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top"><img src="img.php?<?=$_SERVER['QUERY_STRING'];?>"></td>
<td style="padding-left:20px">
<form name="poli" method="get" action="poligonator.php">
Lados <input name="ver" value="<?=$vertices;?>"><br>
Radio <input name="radio" value="<?=$radio;?>"><br><br>
<b>Colores</b><br>
Fondo <input id="fondo" name="fondo" value="<?=$fondo;?>" onKeyUp="color(this.name,this.value)"><br>
Figura <input id="figura" name="figura" value="<?=$figura;?>" onKeyUp="color(this.name,this.value)"><br>
Puntos <input id="puntos" name="puntos" value="<?=$puntos;?>" onKeyUp="color(this.name,this.value)"><br>
Circulo <input id="circulo" name="circulo" value="<?=$circulo;?>" onKeyUp="color(this.name,this.value)"><br>
<input type="submit" value="Crear">
</form>
</td>
</tr>
<tr>
<td colspan="2" style="padding-left:10px"><b>Lienzo: <?=$radio*4 . 'x' . $radio*4;?></b><br><input id="x" style="border:0px; width:30px"> <input id="y" style="border:0px"></td>
</tr>
</table>
---------------------------------------------------------------
img.php
----------------------------------------------------------------
<?
Header("Content-type: image/png");
$expreg='^[a-fA-F0-9]{6}$';
$vertices = ((!$_GET['ver']) || $_GET['ver']<3) ? 3 : $_GET['ver'];
$radio = ((!$_GET['radio'])) ? 50 : $_GET['radio'];
$cfondo = (!ereg($expreg,$_GET['fondo'])) ? 'DDDDDD' : $_GET['fondo'];
$cfigura = (!ereg($expreg,$_GET['figura'])) ? '333333' : $_GET['figura'];
$cpuntos = (!ereg($expreg,$_GET['puntos'])) ? '00FF00' : $_GET['puntos'];
$ccirculo = (!ereg($expreg,$_GET['circulo'])) ? '910BFF' : $_GET['circulo'];
$pi=3.1415926535897932384626433832795;
$grados=360/$vertices;
$cos=cos($grados*$pi/180);
$sen=sin($grados*$pi/180);
$x[0]=$radio*2;
$y[0]=$radio;
$cord=array($x[0],$y[0]);
for ($i=1;$i<$vertices;$i++) {
$x[$i] = ((($x[$i-1]-$radio*2) * $cos) + (($y[$i-1]-$radio*2) * $sen))+($radio*2);
$y[$i] = ((($x[$i-1]-$radio*2) * -$sen) + (($y[$i-1]-$radio*2) * $cos))+($radio*2);
$cord[]=round($x[$i]);
$cord[]=round($y[$i]);
}
$im = imagecreate($radio*4,$radio*4);
$fondo=imagecolorallocate ($im, hexdec(substr($cfondo,0,2)), hexdec(substr($cfondo,2,2)), hexdec(substr($cfondo,4,2)));
$figura=imagecolorallocate ($im, hexdec(substr($cfigura,0,2)), hexdec(substr($cfigura,2,2)), hexdec(substr($cfigura,4,2)));
$circulo=imagecolorallocate ($im, hexdec(substr($ccirculo,0,2)), hexdec(substr($ccirculo,2,2)), hexdec(substr($ccirculo,4,2)));
$puntos=imagecolorallocate ($im, hexdec(substr($cpuntos,0,2)), hexdec(substr($cpuntos,2,2)), hexdec(substr($cpuntos,4,2)));
Imagefill ($im, 0, 0, $fondo);
imagefilledpolygon ($im, $cord, $vertices, $figura); //poligono
imagefilledarc($im, $radio*2, $radio*2, 7, 7, 0, 360, $puntos, IMG_ARC_PIE); //centro
imagearc ($im, $radio*2, $radio*2, $radio*2, $radio*2, 0, 360, $circulo); //fuera
imagearc ($im, $radio*2, $radio*2, ($radio*2)+2, ($radio*2)+2, 0, 360, $circulo); //fuera2
for ($j=0;$j<$vertices;$j++) {
imagefilledarc($im, $x[$j], $y[$j], 5, 5, 5, 360, $puntos, IMG_ARC_PIE); //puntos de vertice
}
Imagepng($im);
Imagedestroy($im);
?>