How can I tell if a closed path contains a given point?

前端 未结 5 1224
广开言路
广开言路 2020-11-29 00:19

In Android, I have a Path object which I happen to know defines a closed path, and I need to figure out if a given point is contained within the path. What I was hoping for

5条回答
  •  眼角桃花
    2020-11-29 00:43

    Here is what I did and it seems to work:

    RectF rectF = new RectF();
    path.computeBounds(rectF, true);
    region = new Region();
    region.setPath(path, new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));
    

    Now you can use the region.contains(x,y) method.

    Point point = new Point();
    mapView.getProjection().toPixels(geoPoint, point);
    
    if (region.contains(point.x, point.y)) {
      // Within the path.
    }
    

    ** Update on 6/7/2010 ** The region.setPath method will cause my app to crash (no warning message) if the rectF is too large. Here is my solution:

    // Get the screen rect.  If this intersects with the path's rect
    // then lets display this zone.  The rectF will become the 
    // intersection of the two rects.  This will decrease the size therefor no more crashes.
    Rect drawableRect = new Rect();
    mapView.getDrawingRect(drawableRect);
    
    if (rectF.intersects(drawableRect.left, drawableRect.top, drawableRect.right, drawableRect.bottom)) {
       // ... Display Zone.
    }
    

提交回复
热议问题