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

旧时模样 提交于 2019-11-27 01:27:09

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/7972780/how-do-i-find-all-the-points-in-a-path-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!