2DWorlds Forums
Contest: rectangle intersection - Printable Version

+- 2DWorlds Forums (http://2dworlds.buildism.net/forum)
+-- Forum: Off Topic (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=5)
+--- Forum: Programming (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=30)
+--- Thread: Contest: rectangle intersection (/showthread.php?tid=9523)



Contest: rectangle intersection - noob007 - 06-18-2012

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!


RE: Contest: rectangle intersection - Qwertygiy - 06-18-2012

So axis-aligned means that the edges are parallel to the x and y axes?


RE: Contest: rectangle intersection - noob007 - 06-18-2012

(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.


RE: Contest: rectangle intersection - Qwertygiy - 06-18-2012

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.


RE: Contest: rectangle intersection - noob007 - 06-27-2012

This is just a friendly bump. :O :O :O


RE: Contest: rectangle intersection - Franco30557 - 06-27-2012

I don't think anyone is going to enter this. =/


RE: Contest: rectangle intersection - Qwertygiy - 06-27-2012

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;
    }
}