We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Newbie question: how to use a script from hack
Page Index Toggle Pages: 1
Newbie question: how to use a script from hack (Read 174 times)
Newbie question: how to use a script from hack
Feb 16th, 2009, 11:48am
 
Hi forum,

I ve found a script at processing hacks, but I have no idea how to use it.
I like to generate two rectangles and check if they intersect.

http://processing.org/hacks/hacks:collision_detection#rectangle-collision

Can someone help me?
Re: Newbie question: how to use a script from hack
Reply #1 - Feb 16th, 2009, 12:44pm
 
Example:
Code:
float x1, y1, w1, h1;
float x2, y2, w2, h2;

void setup()
{
size(500, 500);
noStroke();
MakeRectangles();
}

void draw()
{
if (AreIntersecting())
{
background(200);
}
else
{
background(255);
}
fill(0x8000FF00);
rect(x1, y1, w1, h1);
fill(0x800000FF);
rect(x2, y2, w2, h2);
}

void MakeRectangles()
{
float xbr, ybr;
x1 = random(5, 2*width/3);
y1 = random(5, 2*height/3);
xbr = random(x1 + 5, 4*width/5);
ybr = random(y1 + 5, 4*height/5);
w1 = xbr - x1;
h1 = ybr - y1;

x2 = random(5, 2*width/3);
y2 = random(5, 2*height/3);
xbr = random(x2 + 5, width - 5);
ybr = random(y2 + 5, height - 5);
w2 = xbr - x2;
h2 = ybr - y2;
}

boolean AreIntersecting()
{
return x1 < x2 + w2 && y1 < y2 + h2 &&
x2 < x1 + w1 && y2 < y1 + h1;
}

void keyPressed()
{
MakeRectangles();
}
Re: Newbie question: how to use a script from hack
Reply #2 - Feb 16th, 2009, 1:27pm
 
Thank you PhiLho!
Page Index Toggle Pages: 1