java-线段之间相交的问题

java-线段之间相交的问题,第1张

概述我有一个存储为点的路径,位于数组列表中,我想检查线段是否相交.由于某些未知原因,它不起作用!尽管我正在绘制相交的形状,但在LogCat中没有收到任何消息.了解是否有人可以看到我做错了什么或对如何改进代码提出建议.//Intersectioncontrolif(touchActionUp){

我有一个存储为点的路径,位于数组列表中,我想检查线段是否相交.由于某些未知原因,它不起作用!尽管我正在绘制相交的形状,但在LogCat中没有收到任何消息.了解是否有人可以看到我做错了什么或对如何改进代码提出建议.

    // Intersection control    if(touchActionUp) {        // Loop throw all points except the last 3        for (int i = 0; i < points.size()-3; i++) {            line line1 = new line(points.get(i), points.get(i+1));            // Begin after the line above and check all points after that            for (int j = i + 2; j < points.size()-1; j++) {                line line2 = new line(points.get(j), points.get(j+1));                // Call method to check intersection                if(checkIntersection(line1, line2)) {                    Log.i("Intersection", "Yes!");                }            }        }    }

和方法:

    // Method to check for intersection between linesprivate boolean interceptionControl(line line1, line line2) {    int x1 = line1.pointX1;    int x2 = line1.pointX2;    int x3 = line2.pointX1;    int x4 = line2.pointX2;    int y1 = line1.pointY1;    int y2 = line1.pointY2;    int y3 = line2.pointY1;    int y4 = line2.pointY2;    // Check if lines are parallel    int denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);    if(denom == 0) { // lines are parallel        // ??    }    double a = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denom;    double b = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denom;    // Check for intersection    if( a >= 0.0f && a <= 1.0f && b >= 0.0f && b <= 1.0f) {        return true;    }    return false;}

解决方法:

您将int用于坐标,因此它进行整数除法(即3/2 = 1).这可能是您用标度除以的原因.您可以通过除以((double)denom)而不是简单的denom来解决它.

总结

以上是内存溢出为你收集整理的java-线段之间相交的问题全部内容,希望文章能够帮你解决java-线段之间相交的问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1093514.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存