Desafío Poligonator
#3 Solución de Arias
Ver código
|
Ver comentarios (6)
|
Descargar código
Fecha: 22 agosto 2005
Tamaño: 9664 caracteres
Comentarios: 6
Solución online:
http://desafios.php5.cz/poligonator/
Valoración









9.12
(8 votos)
"Muy facilito :D"
Valora esta solución
Archivo "index.html":
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="es">
<head>
<title>Poligonator - Por Iván Arias</title>
<style type="text/css">
body { background: #F5F5F5; text-align: center; font-family: cursive; font-size: 13px; }
body div { margin: auto; width: 600px; background: #CCC; border: 1px solid black; padding: 20px; text-align: left; }
body div img { float: left; border: 1px black solid; }
body div fieldset { float: right; width: 350px; }
body div fieldset legend { border: 1px black solid; padding: 1px 10px; background: white; font-weight: bold; }
body div fieldset p { margin: 10px 0px; }
body div fieldset label { border-bottom: 1px green dashed; cursor: help; font-weight: bold; }
</style>
<script type="text/javascript">
<!--
function loadImage() {
var sizex = document.getElementById('sizex').value;
var sizey = document.getElementById('sizey').value;
var radio = document.getElementById('radio').value;
var lados = document.getElementById('lados').value;
document.getElementById('image').src = 'poligono.php?sizex='+sizex+'&sizey'+sizey+'&radio='+radio+'&lados='+lados;
}
-->
</script>
</head>
<body>
<div>
<img id="image" src="poligono.php" alt="" />15:50 22/08/2005
<form action="poligono.php" method="get" onsubmit="loadImage(); return false;">
<fieldset>
<legend>Párametros</legend>
<p><label for="sizex" title="Min: 100x100 px, Max: 200x200 px">Tamaño imagen:</label> <input id="sizex" type="text" maxlength="3" size="3" value="200" /> <label for="sizey" title="Min: 100x100 px, Max: 200x200 px">x</label> <input id="sizey" type="text" maxlength="3" size="3" value="200" /> pixeles</p>
<p><label for="radio" title="Min: 50 px, Max: tamaño / 2 px">Radio:</label> <input id="radio" type="text" maxlength="3" size="3" value="50" /> pixeles</p>
<p><label for="lados" title="Min: 2">Nº de lados:</label> <input id="lados" type="text" maxlength="3" size="3" value="5"/></p>
<input type="submit" value="Generar polígono" onclick="loadImage();" />
</fieldset>
</form>
<br style="clear: both;" />
</div>
</body>
</html>
----------------------------------------------------------------
Archivo 'poligono.php':
<?php
function get($var, $default=null) {
return isset($_GET[$var]) ? $_GET[$var] : $default;
}
$sizex = (int)max(100, min(200, (int)get('sizex', 200)));
$sizey = (int)max(100, min(200, (int)get('sizey', 200)));
$lados = (int)max(3, get('lados', 5));
$radio = (int)max(50, min(min($sizex, $sizey) / 2, (int)get('radio', 50)));
$image = imagecreate($sizex, $sizey);
$angulo = 360 / $lados;
$puntos = array();
imagecolorallocate($image, 255, 255, 255);
$negro = imagecolorallocate($image, 0, 0, 0);
for ($i=0; $i<$lados; $i++) {
$puntos[] = round($sizex / 2 + $radio * cos(deg2rad($i * $angulo - 90)));
$puntos[] = round($sizey / 2 + $radio * sin(deg2rad($i * $angulo - 90)));
}
imagepolygon($image, $puntos, $lados, $negro);
header('Content-Type: image/png');
imagepng($image);
?>