1

How to find intersect between two polylines in android?

I tried all below options

  1. PolyUtil.isLocationOnPath();

  2. RectF rectPathBounds=new RectF();
    
    path.computeBounds(rectPathBounds,true);
    
    if(rectPathBounds.contains((int) event.getX(), (int) event.getY())){
    
    }
    

3.boolean res = path.op(path1, Path.Op.INTERSECT);

Please help me with the solutions. Thanks in advance

Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26
AmitCharkha
  • 209
  • 4
  • 18
  • Take a look at https://stackoverflow.com/questions/38430515/intersecting-polygons-and-polylines-using-google-maps-api-v3/38431572#38431572 – antonio Mar 28 '18 at 19:34
  • @antonio Thanks a lot for your help. I converted and removed the unwanted code from the link you shared. Now it is working as I want. – AmitCharkha Mar 29 '18 at 07:22

1 Answers1

1
/**
     * See if two line segments intersect. This uses the
     * vector cross product approach described below:
     * http://stackoverflow.com/a/565282/786339
     *
     * @param {Object} p point object with x and y coordinates
     *  representing the start of the 1st line.
     * @param {Object} p2 point object with x and y coordinates
     *  representing the end of the 1st line.
     * @param {Object} q point object with x and y coordinates
     *  representing the start of the 2nd line.
     * @param {Object} q2 point object with x and y coordinates
     *  representing the end of the 2nd line.
     */
    boolean doLineSegmentsIntersect(Point p,Point p2,Point q,Point q2) {
        Point r = subtractPoints(p2, p);
        Point s = subtractPoints(q2, q);

        float uNumerator = crossProduct(subtractPoints(q, p), r);
        float denominator = crossProduct(r, s);

        if (denominator == 0) {
            // lines are paralell
            return false;
        }

        float u = uNumerator / denominator;
        float t = crossProduct(subtractPoints(q, p), s) / denominator;

        return res = (t >= 0) && (t <= 1) && (u > 0) && (u <= 1);

    }

    /**
     * Calculate the cross product of the two points.
     *
     * @param {Object} point1 point object with x and y coordinates
     * @param {Object} point2 point object with x and y coordinates
     *
     * @return the cross product result as a float
     */
    float crossProduct(Point point1, Point point2) {
        return point1.x * point2.y - point1.y * point2.x;
    }

    /**
     * Subtract the second point from the first.
     *
     * @param {Object} point1 point object with x and y coordinates
     * @param {Object} point2 point object with x and y coordinates
     *
     * @return the subtraction result as a point object
     */
    Point subtractPoints(Point point1,Point point2) {
        Point result = new Point();
        result.x = point1.x - point2.x;
        result.y = point1.y - point2.y;

        return result;
    }
AmitCharkha
  • 209
  • 4
  • 18