如何在Android中找到两条折线之间的相交? [英] How to find intersect between two polylines in android?

查看:52
本文介绍了如何在Android中找到两条折线之间的相交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在android中找到两条折线之间的相交?

How to find intersect between two polylines in android?

我尝试了以下所有选项

  1. PolyUtil.isLocationOnPath();

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

推荐答案

/**
     * 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;
    }

这篇关于如何在Android中找到两条折线之间的相交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆