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

标签 android google-maps

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

我尝试了以下所有选项

  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);

请帮我解决问题。提前致谢

最佳答案

/**
     * 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 - 如何在android中找到两条折线之间的交点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49535476/

相关文章:

android - 通过 Phonegap 更新手机联系人 (Android)

android - 无法再次执行 dex : Multiple dex files define Lcom/. ...

android - 无法在 cordova 3.3 中添加 android 平台

java - 如何在对话期间以编程方式将号码输入到android中的电话应用程序屏幕

javascript - 扩展 Object.prototype 打破了 JavaScript for..in 循环,它在 Google Maps API v3 中被大量使用

javascript - 传单:将同一层添加到两个不同的 map

android.view.inflateexception 二进制 xml 文件第 6 行错误或膨胀类 fragment

android - 在 webview 中播放本地视频

google-maps - 给定一个 lat/lng 坐标,计算 10 公里区域的最小和最大 lat/lng 值

javascript - 在 href Click 上加载 Google map 中的位置