14

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 was something along the lines of

path.contains(int x, int y)

but that doesn't seem to exist.

The specific reason I'm looking for this is because I have a collection of shapes on screen defined as paths, and I want to figure out which one the user clicked on. If there is a better way to be approaching this such as using different UI elements rather than doing it "the hard way" myself, I'm open to suggestions.

I'm open to writing an algorithm myself if I have to, but that means different research I guess.

Tom Seago
  • 143
  • 1
  • 1
  • 4

5 Answers5

19

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.
}
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Randy Findley
  • 329
  • 2
  • 6
  • why do I get region.getBounds() as 0,0,0,0 while using the code above? It does not work on my project. Where do I make mistake? – Mustafa Güven Nov 21 '12 at 14:05
  • 3
    Unfortunately if path is not a regular Rect, point touched is seen in path also if it's not in. A simple example is a triangle. Bound of a Rectangle-Triangle is a Rectangle! So, if you, for ex., tap above hypotenuse, point touched is still in bound !! Maybe a better solution can be this: http://stackoverflow.com/questions/7044838/finding-points-contained-in-a-path-in-android – kinghomer Dec 19 '12 at 17:03
  • 1
    Its work but sometimes region take point that are outside of closed path – Tofeeq Ahmad Jan 21 '13 at 07:49
  • Does this really take the path or just the bounding rect of the path as criteria for inside/outside? – NoDataDumpNoContribution Dec 21 '15 at 23:13
6

The android.graphics.Path class doesn't have such a method. The Canvas class does have a clipping region that can be set to a path, there is no way to test it against a point. You might try Canvas.quickReject, testing against a single point rectangle (or a 1x1 Rect). I don't know if that would really check against the path or just the enclosing rectangle, though.

The Region class clearly only keeps track of the containing rectangle.

You might consider drawing each of your regions into an 8-bit alpha layer Bitmap with each Path filled in it's own 'color' value (make sure anti-aliasing is turned off in your Paint). This creates kind of a mask for each path filled with an index to the path that filled it. Then you could just use the pixel value as an index into your list of paths.

Bitmap lookup = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
//do this so that regions outside any path have a default
//path index of 255
lookup.eraseColor(0xFF000000);

Canvas canvas = new Canvas(lookup);
Paint paint = new Paint();

//these are defaults, you only need them if reusing a Paint
paint.setAntiAlias(false);
paint.setStyle(Paint.Style.FILL);

for(int i=0;i<paths.size();i++)
    {
    paint.setColor(i<<24); // use only alpha value for color 0xXX000000
    canvas.drawPath(paths.get(i), paint); 
    }

Then look up points,

int pathIndex = lookup.getPixel(x, y);
pathIndex >>>= 24;

Be sure to check for 255 (no path) if there are unfilled points.

Brian
  • 8,454
  • 5
  • 27
  • 30
  • Ah, okay, I like it. That extra memory wasn't doing anything useful anyway! One problem I had though was that the ALPHA_8 wouldn't ever give me anything other than just 0 back using getPixel. I had to just give in and use an ARGB_8888. I've found almost no documentation regarding the ALPHA_8 format and what it's limitations are, but it sure didn't work for me here. Thanks Brian. – Tom Seago Apr 08 '10 at 06:32
  • Exercise that memory! The whole Skia Android 2D framework is under-documented. Shame about the 4x memory requirement, but at least the Android screens are pretty small. – Brian Apr 08 '10 at 07:20
  • @TomSeago Hi, I got the same problem with you, I have to use ARGB_8888 as well, nothing return if using ALPHA_8! – John Aug 31 '14 at 09:59
  • For an algorithmic alternative see: http://www.geeksforgeeks.org/how-to-check-if-a-given-point-lies-inside-a-polygon/ – NoDataDumpNoContribution Dec 21 '15 at 23:15
  • @TomSeago I know this post is ancient but I found the problem. Alpha values do not work the way that normal colours do - if the bitmap is set to alpha=255, i.e. full opacity to begin with, then the draw commands will not change anything. Solution: use `0x00000000` in `eraseColor` and use `(255-i)<<24` instead of `i<<24` for the colour definiton and `255-(lookup.getPixel(x, y)>>>24` for the lookup. – Sora. Dec 20 '17 at 12:06
4

WebKit's SkiaUtils has a C++ work-around for Randy Findley's bug:

bool SkPathContainsPoint(SkPath* originalPath, const FloatPoint& point, SkPath::FillType ft)
{
  SkRegion rgn;
  SkRegion clip;

  SkPath::FillType originalFillType = originalPath->getFillType();

  const SkPath* path = originalPath;
  SkPath scaledPath;
  int scale = 1;

  SkRect bounds = originalPath->getBounds();

  // We can immediately return false if the point is outside the bounding rect
  if (!bounds.contains(SkFloatToScalar(point.x()), SkFloatToScalar(point.y())))
      return false;

  originalPath->setFillType(ft);

  // Skia has trouble with coordinates close to the max signed 16-bit values
  // If we have those, we need to scale. 
  //
  // TODO: remove this code once Skia is patched to work properly with large
  // values
  const SkScalar kMaxCoordinate = SkIntToScalar(1<<15);
  SkScalar biggestCoord = std::max(std::max(std::max(bounds.fRight, bounds.fBottom), -bounds.fLeft), -bounds.fTop);

  if (biggestCoord > kMaxCoordinate) {
      scale = SkScalarCeil(SkScalarDiv(biggestCoord, kMaxCoordinate));

      SkMatrix m;
      m.setScale(SkScalarInvert(SkIntToScalar(scale)), SkScalarInvert(SkIntToScalar(scale)));
      originalPath->transform(m, &scaledPath);
      path = &scaledPath;
  }

  int x = static_cast<int>(floorf(point.x() / scale));
  int y = static_cast<int>(floorf(point.y() / scale));
  clip.setRect(x, y, x + 1, y + 1);

  bool contains = rgn.setPath(*path, clip);

  originalPath->setFillType(originalFillType);
  return contains;
}
Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
1

I know I'm a bit late to the party, but I would solve this problem by thinking about it like determining whether or not a point is in a polygon.

http://en.wikipedia.org/wiki/Point_in_polygon

The math computes more slowly when you're looking at Bezier splines instead of line segments, but drawing a ray from the point still works.

1

For completeness, I want to make a couple notes here:

As of API 19, there is an intersection operation for Paths. You could create a very small square path around your test point, intersect it with the Path, and see if the result is empty or not.

You can convert Paths to Regions and do a contains() operation. However Regions work in integer coordinates, and I think they use transformed (pixel) coordinates, so you'll have to work with that. I also suspect that the conversion process is computationally intensive.

The edge-crossing algorithm that Hans posted is good and quick, but you have to be very careful for certain corner cases such as when the ray passes directly through a vertex, or intersects a horizontal edge, or when round-off error is a problem, which it always is.

The winding number method is pretty much fool proof, but involves a lot of trig and is computationally expensive.

This paper by Dan Sunday gives a hybrid algorithm that's as accurate as the winding number but as computationally simple as the ray-casting algorithm. It blew me away how elegant it was.

See https://stackoverflow.com/a/33974251/338479 for my code which will do point-in-path calculation for a path consisting of line segments, arcs, and circles.

Community
  • 1
  • 1
Edward Falk
  • 9,991
  • 11
  • 77
  • 112