Jump to content

johnnyfortune

New Members
  • Posts

    2
  • Joined

  • Last visited

Posts posted by johnnyfortune

  1. Thanks for your reply. This is kinda what I came up with so far. It generates a pixel grid for X & Y coordinates. 

    IDK maybe someone someday will find this useful? I figured I would share. it works for what I needed. 

     

    GRID FUNCTION

    <?php
    public static function battleShipGrid($w, $h, $size)
    {
      $totX = intval(floor($w / $size));
      $totY = intval(floor($h / $size));
    
      $xRanges = array();
      $yRanges = array();
    
      $xLimit = 0;
      while ($xLimit <= $totX) {
         $start = intval($xLimit * $size);
         $end = intval(($start + $size) - 1);
    
         if ($end > $w)
           $end = $w;
    
         $xRanges[] = range($start, $end);
    
         $xLimit++;
      }
    
      $yLimit = 0;
      while ($yLimit <= $totY) {
         $start = intval($yLimit * $size);
         $end = intval(($start + $size) - 1);
    
         if ($end > $h)
           $end = $h;
    
         $yRanges[] = range($start, $end);
    
         $yLimit++;
      }
    
      return array("x" => $xRanges, "y" => $yRanges, "xTotal" => $totX, "yTotal" => $totY);
    }

     

    SEARCH

    public static function searchPoint($xTest, $yTest, $xRanges, $yRanges)
    {
      $xResult = self::searchSquare($xTest, $xRanges);
      $yResult = self::searchSquare($yTest, $yRanges);
    
      return array("x" => $xResult, "y" => $yResult);
    }
    
    public static function searchSquare($needle, $haystack)
    {
      $found = false;
      $current = 0;
    
      while ($found === false) {
        $x = $haystack[$current];
        if (in_array($needle, $x))
          $found = $current;
    
        $current++;
        if ($current > count($haystack))
          $found = "not_found";
      }
    
      if ($found !== "not_found")
        $result = $haystack[$found];
      else
        return false;
    
      $min = $result[0];
      $max = end($result);
    
      return array("key" => $found, "min" => $min, "max" => $max, "items" => $result);
    }

     

    USAGE EXAMPLE

    <?php
    // Getting example coordinates like this
    $examplePoint = "1910,1070";
    
    $points = explode(",", $examplePoint);
    $xTest = intval($points[0]);
    $yTest = intval($points[1]);
    
    // We can find out which X & Y Square they can be found in.
    $xResult = self::searchSquare($xTest, $xRanges);
    $yResult = self::searchSquare($yTest, $yRanges);
    
    ?>

     

  2. Hello All!

    I am struggling to build a script that is generating random images. I want to place a bunch of circles on a rectangular image. However I want those circles to appear in random locations.

    I have been using the range($min,$max) function to keep track of "used" spaces on the X axis, based on the circle size, but I was wondering if there is a class somewhere that could simplify this.

    Ideally, something that takes a given width and height, and breaks that into a group of "cells". 

    IDK if any of you have played the game battle ship, but something like that. Id like to be able to do placeCircle("A1"); 

     

    Does anyone have any suggestions? 

     

    Thanks for reading!

    JF

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.