コード例 #1
0
  public static char rayTypePointInPolygon(Point q, DoublyConnectedEdgeList p) {
    double width = 5000;
    int i, i1; // point index; i1 = 1-1 mod n;
    int n = p.edges.size();
    int d = 2; // dimension index
    int Rcross = 0; // number of right edge/ray crossings
    int Lcross = 0; // number of left edge/ray crossings
    boolean Rstrad, Lstrad; // flags that indicate the edge straddles the x axis
    // Vector<CompPoint> p = new Vector<CompPoint>();

    // for each edge in poly, see if crosses rays
    /*for(i=0;i<n;i++){
    	DCHalfEdge edge = p.edge.get(i);
    	CompPoint newPoint = new CompPoint(edge.start.getX()-q.getX(),edge.start.getY()-q.getY());

    }*/

    // check if p is a vertex of the polygon
    for (i = 0; i < n; i++) {
      i1 = (i + n - 1) % n;
      DCHalfEdge edge = p.edges.get(i);
      if (q.compareTo(edge.start) == 0 || q.compareTo(edge.end) == 0) {
        return 'v';
      }
    }

    DCHalfEdge rEdge = new DCHalfEdge(q, new Point(width, q.getY()));
    DCHalfEdge lEdge = new DCHalfEdge(q, new Point(0, q.getY()));

    Rcross = Geom.edgeIntersectsPolygon(rEdge, p).size();
    Lcross = Geom.edgeIntersectsPolygon(lEdge, p).size();
    // System.out.println("Rcross = "+Rcross+",Lcross="+Lcross);
    /*i1=(i+n-1)%n;
    	DCHalfEdge edge1 = p.edges.get(i1);
    	Rstrad = (edge.start.getY()>q.getY())!=(edge1.start.getY()>q.getY());
    	Lstrad = (edge.start.getY()<q.getY())!=(edge1.start.getY()<q.getY());

    	if(Rstrad|| Lstrad){
    		double x = (edge.start.getX()*edge1.start.getY()-edge1.start.getX()*edge.start.getY())/(edge1.start.getY()-edge.start.getY());

    		if(Rstrad && x> q.getX()){
    			Rcross++;
    		}
    		if(Lstrad && x> q.getX()){
    			Lcross++;
    		}

    	}
    }
    */
    // q is on an edge if L/Rcross counts are not the same parity
    if ((Rcross % 2) != (Lcross % 2)) {
      return 'e';
    }

    if ((Rcross % 2) == 1) {
      return 'i';
    } else {
      return 'o';
    }
  }