How do I find if a point is inside a complex polygon?
in
Contributed Library Questions
•
2 years ago
Hello,
I have a polygon created from an SVG file ( you can find this file here).
I want to test if random points are inside this polygon or not. I am using the java.awt.Polygon class as described on the wiki.
However the contains method returns true only if a point is exactly on the border rather than if it's simply inside the polygon. Should I use another method, algorithm or could it come from an error in the polygon definition ? Maybe it has too many points ? (replacing the drawing trough the Polygon class by beginShape()-endShape() sequence leaves the polygon unfilled too).
Thank you in advance.
I have a polygon created from an SVG file ( you can find this file here).
I want to test if random points are inside this polygon or not. I am using the java.awt.Polygon class as described on the wiki.
However the contains method returns true only if a point is exactly on the border rather than if it's simply inside the polygon. Should I use another method, algorithm or could it come from an error in the polygon definition ? Maybe it has too many points ? (replacing the drawing trough the Polygon class by beginShape()-endShape() sequence leaves the polygon unfilled too).
Thank you in advance.
- /**
- program to try inclusion test (java.awt.Polygon) of random points in complex polygon
- */
- //Library import
- import geomerative.*;
- import java.awt.Polygon; //required ?
- //Defines variables
- RShape s;
- RShape polyshp;
- RPoint[] points;
- int [][] coords;
- float minX;
- float minY;
- float maxX;
- float maxY;
- Poly p;
- void setup() {
- //style
- RG.init(this);
- RG.ignoreStyles(true);
- smooth();
- g.smooth = true;
- rectMode(CENTER);
- background(192);
- frameRate(120);
- shapeMode(CORNER);
- ellipseMode(CENTER);
- //load svg
- s = RG.loadShape("BelgiumC.svg");
- points = s.getPoints();
- println("points.length : "+points.length);
- coords = new int[2][points.length] ;
- // translate points into 2D array
- for (int i = 0; i < points.length ; i++) {
- coords[0][i] = round(points[i].x / 2);
- coords[1][i] = round(points[i].y / 2);
- }
- //defines polygon with points
- p=new Poly(coords[0],coords[1],points.length);
- //finds minima and maxima in svg
- minX = min(coords[0]);
- maxX = max(coords[0]);
- minY = min(coords[1]);
- maxY = max(coords[1]);
- //sizes window according to svg coordinates
- size(int((maxX-minX))+50, int((maxY-minY))+50, JAVA2D);
- }
- void draw() {
- //settings to draw shape
- strokeWeight(1);
- fill(0, 128, 255, 255); //doesn't work
- stroke(0, 0, 0);
- //draws shape
- p.drawMe();
- }
- //click to create random point and draw ellipse if within polygon
- void mousePressed() {
- //chooses a random point and draws if it is included in shape
- // random coordinates
- int aleaX = int(random(0, width));
- int aleaY = int(random(0, height));
- if (p.contains(aleaX, aleaY)) {
- println("p.contains = "+p.contains(aleaX, aleaY));
- fill(255, 255, 0);
- stroke(0, 128, 255);
- strokeWeight(2);
- ellipse(aleaX, aleaY, 20, 20);
- } else {
- println("p.contains = "+p.contains(aleaX, aleaY));
- }
- }
- //ADDITIONAL CLASSES
- //class to draw and test polygon
- class Poly extends java.awt.Polygon{
- public Poly(int[] x,int[] y, int n){
- //call the java.awt.Polygon constructor
- super(x,y,n);
- }
- void drawMe(){
- beginShape();
- for(int i=0;i<npoints;i++){
- vertex(xpoints[i],ypoints[i]);
- }
- endShape();
- }
- }
1