Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Contest: rectangle intersection
#1
In this contest the contestants will submit a function that takes two (axis-aligned) rectangle objects as parameters and returns whether they intersect or not.

The rectangle object may be defined as either two points, or one point (the center) and the width and height.

On my computer, I will construct benchmarks by creating lots of random rectangles and testing if they intersect using each contestant's method. Whichever method takes the least amount of time wins.

Your method/function may be in any programming language you want. Of course I need to be able to run it on my computer so don't use imaginary languages or ones whose runtimes are otherwise unobtainable.

In the end I will submit my own method too.

The contest will end whenever enough entries have been submitted. Get started!
Reply
#2
So axis-aligned means that the edges are parallel to the x and y axes?
Reply
#3
(06-18-2012, 12:59 PM)Qwertygiy Wrote: So axis-aligned means that the edges are parallel to the x and y axes?

Yes, which can be simpler to handle.
Reply
#4
In Lua:

[lua]
function c(cx1, cy1, w1, h1, cx2, cy2, w2, h2)
tlx1 = cx1 - (w1 * 1/2)
tly1 = cy1 + (h1 * 1/2)
brx1 = cx1 + (w1 * 1/2)
bry1 = cy1 - (h1 * 1/2)
tlx2 = cx2 - (w2 * 1/2)
tly2 = cy2 + (h2 * 1/2)
brx2 = cx2 + (w2 * 1/2)
bry2 = cy2 - (h2 * 1/2)

if (tlx1 < brx2 and tly1 > bry2) or (tlx2 < brx1 and tly2 > brx2) then
return true
else
return false
end
end

function d(tlx1, tly1, brx1, bry1, tlx2, tly2, brx2, bry2)
if (tlx1 < brx2 and tly1 > bry2) or (tlx2 < brx1 and tly2 > brx2) then
return true
else
return false
end
end
[/lua]


c(Rectangle1CenterX, Rectangle1CenterY, Rectangle1Width, Rectangle1Height, Rectangle2CenterX, Rectangle2CenterY, Rectangle2Width, Rectangle2Height)

d(Rectangle1TopLeftX, Rectangle1TopLeftY, Rectangle1BottomRightX, Rectangle1BottomRightY, Rectangle2TopLeftX, Rectangle2TopLeftY, Rectangle2BottomRightX, Rectangle2BottomRightY)

I don't think that default Lua has a coordinates object. If it does I have a slightly simpler one that works in Buildism Lua.
Reply
#5
This is just a friendly bump. :O :O :O
Reply
#6
I don't think anyone is going to enter this. =/
Reply
#7
In Java:

Code:
public boolean c(int cx1, int cy1, int w1, int h1, int cx2, int cy2, int w2, int h2)
{
    int tlx1 = cx1 - (w1 * 1/2);
    int tly1 = cy1 + (h1 * 1/2);
    int brx1 = cx1 + (w1 * 1/2);
    int bry1 = cy1 - (h1 * 1/2);
    int tlx2 = cx2 - (w2 * 1/2);
    int tly2 = cy2 + (h2 * 1/2);
    int brx2 = cx2 + (w2 * 1/2);
    int bry2 = cy2 - (h2 * 1/2);
    
    if ((tlx1 < brx2 and tly1 > bry2) || (tlx2 < brx1 and tly2 > brx2))
    {
        return true;
    }
    else
    {
        return false;
    }
}

public boolean d(int tlx1, int tly1, int brx1, int bry1, int tlx2, int tly2, int brx2, int bry2)
{
    if ((tlx1 < brx2 and tly1 > bry2) || (tlx2 < brx1 and tly2 > brx2))
    {
        return true;
    }
    else
    {
        return false;
    }
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)