Ejercicios (sin formularios) - for (2) - Soluciones

En esta página se muestran una parte de las soluciones de los ejercicios for (2), concretamente el fragmento PHP, sin el principio ni el final de la página que se puede dejar en un fragmento HTML. La solución completa se puede descargar de la página de soluciones.

for (2) 01 - Círculos en fila (1)

<?php
$circulos = rand(1, 10);

if ($circulos == 1) {
    print "  <h2>$circulos círculo</h2>\n";
} else {
    print "  <h2>$circulos círculos</h2>\n";
}
print "\n";
print "  <p>\n";
print "    <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" "
    . "width=\"" . (90 * $circulos - 30) . "\" height=\"60\" style=\"border: black 1px solid\">\n";

for ($i = 1; $i <= $circulos; $i++) {
    print "      <circle cx=\"" . (90 * $i - 60). "\" cy=\"30\" r=\"30\" fill=\"black\" />\n";
}

print "    </svg>\n";
print "  </p>\n";
?>
<?php

for ($i = 0; $i < $circulos; $i++) {
    print "      <circle cx=\"" . (90 * $i + 30). "\" cy=\"30\" r=\"30\" fill=\"black\" />\n";
}

?>

for (2) 02 - Círculos en fila (2)

<?php
$circulos = rand(1, 10);

if ($circulos == 1) {
    print "  <h2>$circulos círculo</h2>\n";
} else {
    print "  <h2>$circulos círculos</h2>\n";
}
print "\n";
print "  <p>\n";
print "    <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" "
    . "width=\"" . (90 * $circulos - 30) . "\" height=\"60\" style=\"border: black 1px solid\">\n";

for ($i = 1; $i <= $circulos; $i++) {
    print "      <circle cx=\"" . (90 * $i - 60). "\" cy=\"30\" r=\"30\" fill=\"black\" />\n";
}

print "    </svg>\n";
print "  </p>\n";
?>

for (2) 03 - Círculos en fila (3)

<?php
$circulos = rand(1, 10);
$radio = rand(10, 50);

if ($circulos == 1) {
    print "  <h2>$circulos círculo de $radio px</h2>\n";
} else {
    print "  <h2>$circulos círculos de $radio px</h2>\n";
}
print "\n";
print "  <p>\n";
print "    <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" "
    . "width=\"" . (3* $radio * $circulos - $radio) . "\" height=\"" . 2 * $radio . "\" style=\"border: black 1px solid\">\n";

for ($i = 1; $i <= $circulos; $i++) {
    print "      <circle cx=\"" . (3 * $radio * $i - 2 * $radio). "\" cy=\"$radio\" r=\"$radio\" fill=\"black\" />\n";
}

print "    </svg>\n";
print "  </p>\n";
?>

for (2) 04 - Líneas (1)

<?php
$lineas = rand(3, 10);

print "  <h2>$lineas líneas</h2>\n";
print "\n";
print "  <p>\n";
print "    <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" \n";
print "      width=\"500\" height=\"100\" style=\"border: black 1px solid\" >\n";

for ($i = 1; $i <= $lineas; $i++) {
    print "      <line x1=\"10\" y1=\"0\" x2=\"" . (50 * $i - 40). "\" y2=\"100\" stroke=\"red\" stroke-width=\"1\" />\n";
}

print "    </svg>\n";
print "  </p>\n";
?>

for (2) 05 - Líneas (1)

<?php
$lineas = rand(3, 10);

print "  <h2>$lineas líneas</h2>\n";
print "\n";
print "  <p>\n";
print "    <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" \n";
print "      width=\"" . 50 * $lineas . "\" height=\"120\" "
    . " viewbox=\" -10 -10 " . 50 * $lineas . " 120\" style=\"border: black 1px solid\" >\n";

for ($i = 1; $i <= $lineas; $i++) {
    print "      <line x1=\"0\" y1=\"0\" x2=\"" . (50 * $i - 50). "\" y2=\"100\" stroke=\"green\" stroke-width=\"1\" />\n";
}

print "    </svg>\n";
print "  </p>\n";
?>

for (2) 06 - Líneas (1)

<?php
$numero = rand(3,10);

print "  <h2>$numero líneas</h2>\n";
print "\n";
print "  <p>\n";
print "    <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" \n";
print "      width=\"" . (50 * $numero - 25). "\" height=\"120\" "
    . " viewbox=\" -10 -10 " . (50 * $numero - 25) . " 120\" style=\"border: black 1px solid\" >\n";

for ($i = 1; $i <= $numero; $i++) {
    print "      <line x1=\"" . (25 * $numero - 25). "\" y1=\"0\" x2=\"" . (50 * $i - 50) . "\" y2=\"100\" stroke=\"blue\" stroke-width=\"1\" />\n";
}

print "    </svg>\n";
print "  </p>\n";
?>

for (2) 07 - Arco iris circular

<?php
$numero = rand(3,10);
$paso = 360 / $numero;

print "  <h2>$numero colores</h2>\n";
print "\n";
print "  <p>\n";
print "    <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" \n";
print "      width=\"" . 60 * $numero . "\" height=\"" . 60 * $numero . "\" "
    . " viewbox=\"" . - 30 * $numero . " " . - 30 * $numero . " " . 60 * $numero . " " . 60 * $numero . "\">\n";

for ($i = 0; $i < $numero; $i++) {
    print "      <circle cx=\"0\" cy=\"0\" r=\"" . (-30 * $i + 30 * $numero) . "\" fill=\"hsl(" . round($i * $paso) . ", 75%, 50%)\" />\n";
}

print "    </svg>\n";
print "  </p>\n";
?>

for (2) 08 - Arco iris semicircular

<?php
$numero = rand(3,10);
$paso = 360 / $numero;

print "  <h2>$numero franjas</h2>\n";
print "\n";
print "  <p>\n";
print "    <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" \n";
print "      width=\"" . (60 * $numero + 20) . "\" height=\"" . (30 * $numero + 20). "\" "
    . " viewbox=\"" . (- 30 * $numero - 10) . " " . (- 30 * $numero - 10) . " " . (60 * $numero + 20). " " . (30 * $numero + 20)
    . "\" style=\"border: black 1px solid; background-color: white;\">\n";

for ($i = 0; $i < $numero; $i++) {
    $valor =  -30 * $i + 30 * $numero;
    print "      <path d=\"M -$valor,0 L $valor,0 A $valor $valor 0 0 0 -$valor,0 z\" fill=\"hsl(" . round($i * $paso) . ", 75%, 50%)\" />\n";
}

print "    </svg>\n";
print "  </p>\n";
?>

for (2) 09 - Arco iris

<?php
$numero = rand(3,10);
$paso = 360 / $numero;

print "  <h2>$numero franjas</h2>\n";
print "\n";
print "  <p>\n";
print "    <svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" \n";
print "      width=\"" . (60 * $numero + 20) . "\" height=\"" . (30 * $numero + 20). "\" "
    . " viewbox=\"" . (- 30 * $numero - 10) . " " . (- 30 * $numero - 10) . " " . (60 * $numero + 20). " " . (30 * $numero + 20)
    . "\" style=\"border: black 1px solid; background-color: white;\">\n";

for ($i = 0; $i < $numero; $i++) {
    $valor =  30 * $numero - 15 * $i;
    print "      <path d=\"M -$valor,0 L $valor,0 A $valor $valor 0 0 0 -$valor,0 z\" fill=\"hsl(" . round($i * $paso) . ", 75%, 50%)\" />\n";
}
$valor = 15 * $numero;
print "      <path d=\"M -$valor,0 L $valor,0 A $valor $valor 0 0 0 -$valor,0 z\" fill=\"white\" stroke=\"white\"/>\n";
print "    </svg>\n";
print "  </p>\n";
?>