www.webdeveloper.com
Results 1 to 5 of 5

Thread: Check If image overlaps another image

  1. #1
    Join Date
    Feb 2006
    Posts
    3

    Check If image overlaps another image

    Hi,

    I'm trying to make a script for a pixel ad site. So far I have come up with this;

    On the index page:

    <form action=buy.php method=post ><td colspan=3 height=1000 valign=top background=images/grid3.gif>
    <input name=whereami type=image class=botborder src=images/px.gif align=top width="1000" height=1000 />
    </form>

    When you click on the image on this page it takes you to buy.php

    on buy.php there is a form with the x location and y location of the image that was clicked on, heres what this looks like:

    <?php
    $xloc = $_POST['whereami_x']; // x co-ordinate
    $yloc = $_POST['whereami_y']; // y co-ordinate

    //this is making the grid give blocks of 10 pixels. so first block is 0,0 next box is 1,0 and so on.
    $x_loc = floor($xloc/10);
    $y_loc = floor($yloc/10);

    echo '<br />x ='.$x_loc;
    echo '<br />y ='.$y_loc;
    ?>

    On the form the customer can choose number of horizontal and vertical blocks for their image. Once they submit form it goes to confirm.php. Now, this is where I'm having a problem. On confirm.php I want it to check if the the number of blocks the customer chose would overlap an image of an existing customer (I have a table in mysql database that stores x and y locations, number of width blocks and height blocks of existing customers). I have absolutely no idea how to do this.
    I would apreciate if anyone can give me any ideas or suggestions on this.

    Many thanks in advance

  2. #2
    Join Date
    Feb 2005
    Location
    BC, Canada
    Posts
    1,098
    I'd extract all of the blocks and who owns them from the database and put it into a 2D array, something like:
    PHP Code:
    // nobody owns it
    $map[$y][$x] == null
    /* OR */
    // somebody owns it
    $map[$y][$x] == $owner 
    Now, you'd need to go through every block and make sure it isn't owned, something like this:

    PHP Code:
    $map = ...; // populate the $map variable
    $good true;
    $width count($map[0]);
    $height count($map);

    for (
    $y=0$y<$height$y++) {
        for (
    $x=0$x<$width$x++) {
            if (
    $map[$y][$x] != null) {
                
    $good false;
                break 
    2;
            }
        }
    }

    if (
    $good) {
        echo 
    "The region is good!";
    } else {
        echo 
    "The region countains already owned blocks. :(";

    Corn is no place for a mighty warrior!

  3. #3
    Join Date
    Feb 2006
    Posts
    3

    confused about code

    Hey SpectreReturns,

    Thank you for your reply. I just have a few questions and a request. I'm not sure how to populate the $map variable - is this the x and y data from the table of existing customers in db?

    Are the variables $x and $y the data of the post variables or from the database?

    Here is the code from confirm.php:

    PHP Code:
    <?php

    include 'pixeldb.php'
    # grab the POST variables from the HTML form, 

    $name $_POST['name']; 
    $email $_POST['email']; 
    $x1 $_POST['x1']; 
    $y1 $_POST['y1']; 
    $wblocks $_POST['wblocks'];
    $hblocks $_POST['hblocks'];
    $url $_POST['url'];
    $message $_POST['message'];
    $amount $_POST['amount'];


    # Any escaped characters? No, not a jailbreak...something that could 
    # cause errors when PHP processes. We check for that here. 
    $name stripslashes($name);  
    $email stripslashes($email); 
    $x1 stripslashes($x1); 
    $y1 stripslashes($y1);  
    $wblocks stripslashes($wblocks); 
    $hblocks stripslashes($hblocks); 
    $url stripslashes($url); 
    $message stripslashes($message);  
    $amount stripslashes($amount); 

    ?>
    I have a table named 'customer' in mysql db, with the fields 'x_pos', 'y_pos', 'width_blocks' and 'height_blocks' and fields for other customer details.

    If you can please show me an example of the code you posted with my variables above, that would clear a lot of confusion.
    Also I'm not sure how to extract all of the blocks from the database and put it into a 2D array.

    Thank you

  4. #4
    Join Date
    Feb 2005
    Location
    BC, Canada
    Posts
    1,098
    The map would be the information for every block, sorted like this:
    PHP Code:
    $map[{from database}y_pos][{from database}x_pos] = {from database}customer
    I'm writing this off my head, so it might work, but I suspect something like this to populate the map:
    PHP Code:
    $map = array();
    $map_width 20// the map is made up as a 20x20 grid
    $map_height 20// change these to suit how big your map should be

    $querytpl "SELECT `customer` FROM `the_table` WHERE `y_pos`=%s"// change the_table to the table you're working in

    for ($y=0$y<$map_height$y++) {
        
    $result mysql_query(sprintf($querytpl"$y")); // typecast so it responds to %s
        
    $row mysql_fetch_row($result);
        foreach (
    $row as $x => $customer) {
            
    $map[$y][$x] = $customer;
        }

    Here's the version of my older code so it'll work with what you said:
    PHP Code:
    include 'pixeldb.php';

    $postfields = array("name""email""x1""y1""wblocks""hblocks""url""amount""message");

    foreach (
    $postfields as $field) {
        $
    $field stripslashes($_POST[$field]);
    }
    // I just cut down all of your manual definitions for the POST variables


    $map = ...; // use the code above
    $good true;

    for (
    $y=$y1$y<($hblocks $y1); $y++) {
        for (
    $x=$x1$x<($wblocks $x1); $x++) {
            if (
    $map[$y][$x] != null) {
                
    $good false;
                break 
    2;
            }
        }
    }

    if (
    $good) {
        echo 
    "The region is good!";
    } else {
        echo 
    "The region countains already owned blocks. :(";

    Last edited by SpectreReturns; 02-26-2006 at 02:16 PM.
    Corn is no place for a mighty warrior!

  5. #5
    Join Date
    Feb 2006
    Posts
    3

    errors

    Hi,

    I've been trying the above code out and now I'm getting errors.

    These are the errors I'm having trouble fixing:

    Notice: Undefined variable: customer in C:\Websites\confirm.php on line 61

    Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\Websites\confirm.php on line 75

    Warning: Invalid argument supplied for foreach() in C:\Websites\confirm.php on line 76

    Line 61:
    PHP Code:
    $map['ypos']['xpos'] = $customer
    Line 75:
    PHP Code:
    $row mysql_fetch_row($result); 
    Line 76:
    PHP Code:
     foreach ($row as $x => $customer) {
            
    $map[$y][$x] = $customer;
        }


    Here's the entire php code from confirm.php:


    PHP Code:
    <?php
    include 'pixeldb.php';

    $query "SELECT * FROM transaction"
    $data mysql_query($query) or die (mysql_error()."<br />Couldn't execute query: $query");  
    $map mysql_fetch_array($data);


    $map['ypos']['xpos'] = $customer


    $map = array();
    $map_width 20// the map is made up as a 20x20 grid
    $map_height 20// change these to suit how big your map should be

    $querytpl "SELECT name FROM transaction WHERE y_pos=%s"// change the_table to the table you're working in

    for ($y=0$y<$map_height$y++) {
        
    $result mysql_query(sprintf($querytpl"$y")); // typecast so it responds to %s
        
    $row mysql_fetch_row($result);
        foreach (
    $row as $x => $customer) {
            
    $map[$y][$x] = $customer;
        }




    $good true;

    for (
    $y=$y1$y<($hblocks $y1); $y++) {
        for (
    $x=$x1$x<($wblocks $x1); $x++) {
            if (
    $map[$y][$x] != null) {
                
    $good false;
                break 
    2;
            }
        }
    }

    if (
    $good) {
        echo 
    "The region is good!";
    } else {
        echo 
    "The region contains already owned blocks. :(";


    ?>
    Last edited by dreamer84; 03-15-2006 at 06:31 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles