1

arraylist にポイントとして保存されているパスがあり、線分が交差しているかどうかを確認したいと考えています。なんらかの理由で機能していません。交差する形状を描いているにもかかわらず、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 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;
}
4

1 に答える 1

0

座標に使用intしているため、整数除算を行います (つまり、3/2 = 1)。で割っているときはこれが原因かもしれませんdenom((double)denom)単純に ではなく で割ることで修正できますdenom

于 2013-05-03T15:17:15.993 に答える