Android: How to check if a path contains touched point?

后端 未结 4 856
花落未央
花落未央 2020-12-02 10:45

I would try to develop an application in which I can draw a planimetry. So, each room has got its own ID or name and, if I touch a room, I want to show a T

4条回答
  •  天命终不由人
    2020-12-02 11:18

    As Edward Falk say, best way is to use path.op() becouse Region is square. And one point can be in 2 or 3 Regions. For example: enter image description here All regions will contain blue point, but on fact only path4 contains this point.

    int x, y;
    Path tempPath = new Path(); // Create temp Path
    tempPath.moveTo(x,y); // Move cursor to point
    RectF rectangle = new RectF(x-1, y-1, x+1, y+1); // create rectangle with size 2xp
    tempPath.addRect(rectangle, Path.Direction.CW); // add rect to temp path
    tempPath.op(pathToDetect, Path.Op.DIFFERENCE); // get difference with our PathToCheck
    if (tempPath.isEmpty()) // if out path cover temp path we get empty path in result
    { 
        Log.d(TAG, "Path contains this point");
        return true;
    }
    else
    {
        Log.d(TAG, "Path don't contains this point");
        return false;
    }
    

提交回复
热议问题