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 & HelpPrograms › Area detection HELP
Page Index Toggle Pages: 1
Area detection HELP (Read 1133 times)
Area detection HELP
Mar 31st, 2009, 1:29pm
 
Hello
check this out:

int px,py;

void setup() {
 size(400,400);
 background(150);
 stroke(0,0,50);
 noFill();
 rectMode(CENTER);

 px = 200;
 py = 200;
 rect(px,py,50,50);
}

void draw() {

 if(mouseX>px-20 &&
    mouseX<px+20 &&
    mouseY>py-20 &&
    mouseY<py+20) {
   
     stroke(0,200,250);
     rect(px,py,50,50);
 }
 else {
   stroke(0,0,50);
   rect(px,py,50,50);
 }
}

move mouse curcor inside rectangle,
it is simple when point (px,py) environment is a rectangle
But what if i want circle, ellipse or triangle area ??

THX
Re: Area detection HELP
Reply #1 - Mar 31st, 2009, 2:22pm
 
for a circle, you just have to check the distance.
Btw you dont have to call the first rec/ellipse in the setup.


int px,py;

void setup() {
size(400,400);
background(150);
stroke(0,0,50);
noFill();
ellipseMode(CENTER);

px = 200;
py = 200;
//ellipse(px,py,50,50);
}

void draw() {

if(dist(mouseX,mouseY,px,py)<=25) {
 
    stroke(0,200,250);
    ellipse(px,py,50,50);
}
else {
  stroke(0,0,50);
  ellipse(px,py,50,50);
}
}
Re: Area detection HELP
Reply #2 - Mar 31st, 2009, 2:31pm
 
checking triangles seems to be a bit more complicated, especially when you have different kind of triangles...
i found this explanation of how it could be done. Maybe it helps

http://www.director-online.com/buildArticle.php?id=1088

Re: Area detection HELP
Reply #3 - Mar 31st, 2009, 10:52pm
 
It is indeed non trivial for triangles (surprisingly!), you will find lot of articles on the topic on the Net (eg. on Paul Bourke's site, a good source of geometric information). And it becomes increasingly hard for other shapes.
The solution generally used is to render the shape with a monochrome color on a different background in a test PImage. Then you check the color at mouse position: if it is of background color, it is outside of the shape.
Re: Area detection HELP
Reply #4 - Apr 1st, 2009, 9:27am
 
You could always use a polygon.

http://processing.org/hacks/hacks:using-awt-s-polygon-class-to-test-for-insideness

In the example, to get a triangle, change the x and y arrays and the polygon constructor to have 3 points instead of 6.
Re: Area detection HELP
Reply #5 - Apr 1st, 2009, 10:38am
 
I believe that the generally accepted math solution to this problem is to take each side of a polygon, figure out the formulas for those lines, and use that particular formulation of the distance-from-point-to-line formula that yields positive numbers on the "left" side of the line and negative numbers on the "right" side.

If you arrange all the sides of the polygon in clockwise order (or counterclockwise, as long as you're consistent), then points that are inside will yield the same sign of the answer from that distance formula.  Points that are on the outside will yield mixed positive/negative results.
Re: Area detection HELP
Reply #6 - Apr 1st, 2009, 11:06pm
 
@NoahBuddy, thats nice. thanks for pointing that out.
Page Index Toggle Pages: 1