Advertisement
Guest User

Crab Example

a guest
Dec 14th, 2012
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.06 KB | None | 0 0
  1. <html>
  2.     <head>
  3.         <title>Crab Example</title>
  4.         <style>
  5.             #crabSection { position:relative; }
  6.             .dot { margin: 0px 0px 0px 0px; position:absolute; }
  7.         </style>
  8.     </head>
  9.     <body>
  10.         <div>
  11.             <h1>Dots on the crab example</h1>
  12.         </div>
  13.         <div id="crabSection">
  14.             <img src="crab.png">
  15. <?php
  16.             $crab = imagecreatefrompng("crab.png");
  17.             $dot = imagecreatefrompng("dot.png");
  18.  
  19.             $numDesiredDots = 10;
  20.             $numCreatedDots = 0;
  21.  
  22.             $crabWidth = imagesx($crab);
  23.             $crabHeight = imagesy($crab);
  24.  
  25.             $dotWidth = imagesx($dot);
  26.             $dotHeight = imagesy($dot);
  27.  
  28.             $spawnableWidth = $crabWidth - $dotWidth;
  29.             $spawnableHeight = $crabHeight - $dotHeight;
  30.  
  31.             srand(time());
  32.  
  33.             $testingForDotSubpart = imagecreatetruecolor($dotWidth, $dotHeight);
  34.  
  35.             $validCoordinates = array();
  36.             $invalidCoordinates = array();
  37.  
  38.             $colorWereLookingFor = 0xFF; // ARGB - our crab is blue
  39.  
  40.             while($numCreatedDots < $numDesiredDots)
  41.             {
  42.                 $randomX = rand() % $spawnableWidth;
  43.                 $randomY = rand() % $spawnableHeight;
  44.  
  45.                 imagecopy($testingForDotSubpart, $crab, 0, 0, $randomX, $randomY, $dotWidth, $dotHeight);
  46.                 $valid = true;
  47.                 for($x = 0; $x < $dotWidth; $x++)
  48.                 {
  49.                     for($y = 0; $y < $dotHeight; $y++)
  50.                     {
  51.                         if(imagecolorat($testingForDotSubpart, $x, $y) != $colorWereLookingFor)
  52.                         {
  53.                             $valid = false;
  54.                             break 2;
  55.                         }
  56.                     }
  57.                 }
  58.  
  59.                 if($valid)
  60.                 {
  61.                     array_push($validCoordinates, array('x' => $randomX, 'y' => $randomY));
  62.                     $numCreatedDots++;
  63.                 }
  64.                 else
  65.                 {
  66.                     // you can get rid of this else, it's just to show you how many fails there are
  67.                     array_push($invalidCoordinates, array('x' => $randomX, 'y' => $randomY));
  68.                 }
  69.             }
  70.  
  71.             imagedestroy($testingForDotSubpart);
  72.             imagedestroy($dot);
  73.             imagedestroy($crab);
  74.  
  75.             echo "<p>Valid Coords: <br>";
  76.  
  77.             foreach($validCoordinates as $coord)
  78.             {
  79.                 echo "X: " . $coord['x'] . " Y: " . $coord['y'] . "<br>\n";
  80.             }
  81.  
  82.             echo "<br>Invalid Coords " . count($invalidCoordinates) . "</p>\n";
  83.  
  84.             // Now add the dots on the crab!
  85.             for($i = 0; $i < count($validCoordinates); $i++)
  86.             {
  87.                 $coord = $validCoordinates[$i];
  88.                 echo "<div class='dot' style='left:".$coord['x'].";top:".$coord['y'].";'><a href='javascript:alert(".$i.");'><img src='dot.png'></a></div>\n";
  89.             }        
  90. ?>
  91.         </div>
  92.     </body>
  93. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement