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 › check if a point is inside or outside of a shape
Page Index Toggle Pages: 1
check if a point is inside or outside of a shape (Read 384 times)
check if a point is inside or outside of a shape
Jul 10th, 2008, 1:34pm
 
hi.

i want to check wheter or not an object with the coordianates xy is within a vertex shape or not. is there a simple way to calculate this?

for instance i have a shape like this
Code:
beginShape();
vertex(30, 20);
vertex(53, 20);
vertex(85, 75);
vertex(85, 75);
vertex(30, 55);
vertex(30, 20);
endShape();


and want to check if the point(23, 76) is within the shape.
Re: check if a point is inside or outside of a sha
Reply #1 - Jul 10th, 2008, 2:00pm
 
java.awt.Polygon provides a contains(int x, int y) method which is appropriate for what you want to do.

javadoc reference :
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Polygon.html

Quote:
// create the polygon
java.awt.Polygon p = new java.awt.Polygon();
p.addPoint(30, 20);
p.addPoint(53, 20);
p.addPoint(85, 75);
p.addPoint(85, 75);
p.addPoint(30, 55);
p.addPoint(30, 20);

// check if point is inside
println(p.contains(23, 76));

// draw the polygon
beginShape();
for (int i = 0; i < p.npoints; i++) {
 vertex(p.xpoints[i], p.ypoints[i]);
}
endShape();
Page Index Toggle Pages: 1