r/dailyprogrammer • • Aug 11 '12

[8/10/2012] Challenge #87 [easy] (Rectangle intersection)

Write a function that calculates the [intersection](http://en.wikipedia.org/wiki/Intersection_(set_theory)) of two rectangles, returning either a new rectangle or some kind of null value.

You're free to represent these rectangles in any way you want: tuples of numbers, class objects, new datatypes, anything goes. For this challenge, you'll probably want to represent your rectangles as the x and y values of the top-left and bottom-right points. (Rect(3, 3, 10, 10) would be a rectangle from (3, 3) (top-left) to (10, 10) (bottom-right).)

As an example, rectIntersection(Rect(3, 3, 10 10), Rect(6, 6, 12, 12)) would return Rect(6, 6, 10, 10), while rectIntersection(Rect(4, 4, 5, 5), Rect(6, 6, 10 10)) would return null.

19 Upvotes

46 comments sorted by

View all comments

1

u/f0xwell Aug 18 '12

PHP CLI.

a bit rusty so comments appreciated.

I didn't look at all the other code posted, but the ones I did look at, if I am right, just answered the question and other similar rectangle intersections. I have made a version which covers, I think, all possible intersections.

I'm particularly interested in hearing if there was a simpler way. I thought about using the equation of a rectangle and setting it equal to the equation of another rectange, which would give all the x and y values of the intersections, but I could not work out the equation for a rectangle in 5 minutes so I skipped that plan.

!/usr/bin/php

<?php

class Rect 
{
var $coords = array(
		"x1" => 1,
            "y1" => 1,
            "x2" => 1,
            "y2" => 1);


function __construct($x1, $y1, $x2, $y2)
    	{
    	if (($x1 + $y1) > ($x2 + $y2)) //make sure that the top right coord is x2,y2
		{
		$this->coords["x1"] = $x2;
            $this->coords["y1"] = $y2;
            $this->coords["x2"] = $x1;
            $this->coords["y2"] = $y1;
		}	
	else
		{
		$this->coords["x1"] = $x1;
    	$this->coords["y1"] = $y1;
	        $this->coords["x2"] = $x2;
    	$this->coords["y2"] = $y2;
		}
	}

function get_coord($point)
	{
	return $this->coords["$point"];
	}

//COLLISION DETECTION
function collision_check($b)
	{
	$c = 0;
	//no collision
	if (   	($this->get_coord("y2") < $b->get_coord("y1")) ||
		($this->get_coord("x2") < $b->get_coord("x1")) ||
		($this->get_coord("x1") > $b->get_coord("x2")) ||
 		($this->get_coord("y1") > $b->get_coord("y2")))
		{ echo "no collision\n"; 
		return 0;}
	else
		{ 
		if ($this->get_coord("y2") > $b->get_coord("y2")) $c = $c + 2; //top
    	if ($this->get_coord("x2") > $b->get_coord("x2")) $c = $c + 4; //right
	        if ($this->get_coord("x1") < $b->get_coord("x1")) $c = $c + 8; //left
	        if ($this->get_coord("y1") < $b->get_coord("y1")) $c = $c + 16;//bottom
		}

	echo $c . "\n";

	switch ($c)
		{
	        case 0:// checked ok
    	return array($this->get_coord("x1"), $this->get_coord("y1"), $this->get_coord("x2"), $this->get_coord("y2"));
	        break;

		case 2:// checked ok
		return array($this->get_coord("x1"), $this->get_coord("y1"), $this->get_coord("x2"), $b->get_coord("y2"));
       		break;

		case 4:// checked ok
        	return array($this->get_coord("x1"), $this->get_coord("y1"), $b->get_coord("x2"), $this->get_coord("y2"));
	        break;

		case 6:// checked ok
	        return array($this->get_coord("x1"), $this->get_coord("y1"), $b->get_coord("x2"), $b->get_coord("y2"));
		break;

	        case 8:// checked ok
  		return array($b->get_coord("x1"), $this->get_coord("y1"), $this->get_coord("x2"), $this->get_coord("y2"));
		break;

    	case 10:// checked ok
	        return array($b->get_coord("x1"), $this->get_coord("y1"), $this->get_coord("x2"), $b->get_coord("y2"));
    	break;

		case 12://
    	return array($b->get_coord("x1"), $this->get_coord("y1"), $b->get_coord("x2"), $this->get_coord("y2"));
	        break;

    	case 14:// checked ok
	        return array($b->get_coord("x1"), $this->get_coord("y1"), $b->get_coord("x2"), $b->get_coord("y2"));
	        break;

    	case 16:// checked ok
	        return array($this->get_coord("x1"), $b->get_coord("y1"), $this->get_coord("x2"), $this->get_coord("y2"));
    	break;

	        case 18://checked ok
    	return array($this->get_coord("x1"), $b->get_coord("y1"), $this->get_coord("x2"), $b->get_coord("y2"));
	        break;	

    	case 20:// checked ok
	        return array($this->get_coord("x1"), $b->get_coord("y1"), $b->get_coord("x2"), $this->get_coord("y2"));
        	break;

	        case 22:// checked ok
    	return array($this->get_coord("x1"), $b->get_coord("y1"), $b->get_coord("x2"), $b->get_coord("y2"));
	        break;

    	case 24:// checked ok
	        return array($b->get_coord("x1"), $b->get_coord("y1"), $this->get_coord("x2"), $this->get_coord("y2"));
    	break;

	        case 26:// checked ok
        	return array($b->get_coord("x1"), $b->get_coord("y1"), $this->get_coord("x2"), $b->get_coord("y2"));
		break;

    	case 28:// checked ok
	        return array($b->get_coord("x1"), $b->get_coord("y1"), $b->get_coord("x2"), $this->get_coord("y2"));
        	break;

	        case 30:// checked ok
    	return array($b->get_coord("x1"), $b->get_coord("y1"), $b->get_coord("x2"), $b->get_coord("y2"));
	        break;

		//endswitch;
		}

	}
}

$Rect1 = new Rect(3,3,9,9);
$Rect2 = new Rect(5,2,8,10);

var_dump($Rect1->collision_check($Rect2));
?>