How do I find all the Points in a Path in Android?

StartingGroovy picture StartingGroovy · Nov 1, 2011 · Viewed 21.3k times · Source

Awhile back I asked a question to see if I was able to find a pair of specific points in a path; however, this time I want to know if there is a way to know all points in a path? (I couldn't find a method that did so, which is unfortunate because Java provides a way to do this, just not Android?)

The reason I ask this is because I have multiple geometric graphs and I want to compare the points to see where they intersect.

I appreciate any helpful responses

Answer

hasanghaforian picture hasanghaforian · Jan 15, 2013

You can use PathMeasure to get coordinates of arbitrary point on the path.For example this simple snippet(that I saw here) returns coordinates of the point at the half of the path:

PathMeasure pm = new PathMeasure(myPath, false);
//coordinates will be here
float aCoordinates[] = {0f, 0f};

//get point from the middle
pm.getPosTan(pm.getLength() * 0.5f, aCoordinates, null);

Or this snippet returns an array of FloaPoints.That array involves coordinates of 20 points on the path:

private FloatPoint[] getPoints() {
        FloatPoint[] pointArray = new FloatPoint[20];
        PathMeasure pm = new PathMeasure(path0, false);
        float length = pm.getLength();
        float distance = 0f;
        float speed = length / 20;
        int counter = 0;
        float[] aCoordinates = new float[2];

        while ((distance < length) && (counter < 20)) {
            // get point from the path
            pm.getPosTan(distance, aCoordinates, null);
            pointArray[counter] = new FloatPoint(aCoordinates[0],
                    aCoordinates[1]);
            counter++;
            distance = distance + speed;
        }

        return pointArray;
    }

In above snippet,FloatPoint is a class that encapsulate coordinates of a point:

class FloatPoint {
        float x, y;

        public FloatPoint(float x, float y) {
            this.x = x;
            this.y = y;
        }

        public float getX() {
            return x;
        }

        public float getY() {
            return y;
        }
    }

References:
stackoverflow
Animating an image using Path and PathMeasure – Android