17

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

StartingGroovy
  • 2,802
  • 9
  • 47
  • 66

2 Answers2

42

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

Armen Apresyan
  • 65
  • 1
  • 10
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
  • You can also use the built in [PointF class](https://developer.android.com/reference/android/graphics/PointF) – P Fuster Jun 18 '20 at 10:09
  • I tried using this answer but getLength is producing 0.0 and therefore not entering the while loop that I have. Here's the code: https://stackoverflow.com/questions/65039195/is-there-any-way-to-get-the-endpoints-of-a-line-when-using-path-class?noredirect=1#comment115115814_65039195 Would you have any idea as to why it produces 0.0? – dan Dec 03 '20 at 16:06
0

If you have created a Path that means that in some point of your code, you know the exact (Geo)point. Why don't you put this point(s) on a ArrayList or something similar?

So for example before doing:

path.lineTo(point.x, point.y);

you can do:

yourList.add(point);
path.lineTo(point.x, point.y);

And later you can get all your points from the ArrayList. Note you that you can take advantage of the Enhanced For Loop Syntax of ArrayList that execute up to three times faster.

Manos
  • 2,136
  • 17
  • 28
  • 1
    That was what I was going to do if there wasn't any alternative. I'll leave the question open for a few more days and if there is no other way suggested, I'll accept this as the answer. – StartingGroovy Nov 03 '11 at 20:00
  • @StartingGroovy Ok, see my updated post, in order to take advantage of a performance speedup, if you finally choose this solution. – Manos Nov 03 '11 at 22:59
  • Interesting, I wasn't aware that the enhanced for loop was 3 times faster :) thanks for that tidbit! – StartingGroovy Nov 03 '11 at 23:19
  • If you're carrying round a data structure with all your points in it, why would you need to have a Path object? Duplicating this data violates the DRY principle. – Rich Smith Jan 12 '15 at 12:54
  • you don't have to know the points. For example if you obtain the path with android.graphics.Paint.getTextPath – takluiper Jul 08 '18 at 11:01