麻烦线段之间的交叉线段、麻烦

2023-09-07 23:51:45 作者:终于我开窍i

我作为一个ArrayList点存放的路径,我想检查线段相交。对于一些未知的原因,它不工作!我不明白,在LogCat中的任何消息,尽管我画相交的形状。 preciate如果有人可以看到我做错了,或有任何建议如何提高code。

  //路口监控
    如果(touchActionUp){

        //循环抛出的所有点除了最后3
        的for(int i = 0; I< points.size() -  3;我++){
            线一号线=新线(points.get(I),points.get第(i + 1));

            //开始上面的行之后,之后检查所有点
            对于(INT J = + 2; J< points.size() -  1; J ++){
                线2号线=新线(points.get(J),points.get(J + 1));

                //调用方法检查路口
                如果(checkIntersection(一号线,2号线)){
                    Log.i(路口,是的!);
                }
            }
        }
    }
 

和方法:

  //方法来检查线的交点
私人布尔interceptionControl(线一号线,线2号线){
    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;

    //检查线平行

    INT DENOM =(Y4  -  Y3)*(X2  -  X1) - (X4  -  X3)*(Y2  -  Y1);

    如果(DENOM == 0){//线是平行的
        //?
    }

    双A =((X4  -  X3)*(Y1  -  Y3) - (Y4  -  Y3)*(X1  -  X3))/ DENOM;
    双B =((2次 -  1次)*(Y1  -  Y3) - (Y2  -  Y1)*(X1  - ×3))/ DENOM;

    //检查交集
    如果(A> = 0.0&功放;&安培; A< = 1.0F和放大器;和B> = 0.0&功放;和B< = 1.0F){
        返回true;
    }
    返回false;
}
 

解决方案 线段全集 收藏版 一文读懂所有原文关于线段的内容

正在使用 INT 的坐标,它确实是这样的整数除法(即3/2 = 1 )。这可能是原因,当你被 DENOM 分裂。您可以通过分((双)DENOM)而不是简单地修复它 DENOM

I have a path stored as points in an arraylist and I want to check if the line segments are intersecting. For some unknown reason it's not working! I don't get any message in the LogCat despite that I'm drawing a shape that intersects. Preciate if someone could see what I have done wrong or have suggestions how to improve code.

    // 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!");
                }
            }
        }
    }

And the method:

    // Method to check for intersection between lines
private 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;
}

解决方案

You are using int for coordinates and so it does integer division (i.e., 3/2 = 1). This might be the reason when you are dividing by denom. You can fix it by dividing by ((double)denom) instead of simply denom.