Android/Java如何在不使用Line2D的情况下检查矩形和线段是否相交

3
在我的Android游戏中,我需要检查矩形和线段之间的交点。由于Android不支持Line2D,所以我不能使用它。我查看了处理线的类似问题并尝试修改它们,但失败了。我也意识到了这个Q/A,基本上就是我想要的,但我失败了。以下是我的Line类代码,包括我尝试交点的部分。结果是一些非交点返回true,一些交点返回false。编辑:Oli Charlesworth帮助了我,下面的代码可以用于任何人使用。
   package com.example.HelloAndroid;

import android.graphics.Rect;

public class Segment {
    int x1;
    int y1;
    int x2;
    int y2;
    double m;
    double b;
    boolean ishoriz;
    boolean isvert;

    public Segment(int x1s, int y1s, int x2s, int y2s) {
        if (x1s > x2s) {
            this.x1 = x2s;
            this.x2 = x1s;
        } else {
            this.x1 = x1s;
            this.x2 = x2s;
        }
        if (y1s > y2s) {
            this.y1 = y2s;
            this.y2 = y1s;
        } else {
            this.y1 = y1s;
            this.y2 = y2s;
        }
        int ydif = y2s - y1s;
        int xdif = x2s - x1s;
        if (ydif == 0) {
            this.ishoriz = true;
            this.m = 0;
            this.b = x1s;
        } else if (xdif == 0) {
            this.isvert = true;
        } else {
            this.m = (double) ydif / xdif;
            double r = (double) ydif / xdif;
            this.b = y1s - (r * x1s);
            this.isvert = false;
            this.ishoriz = false;
        }
    }

    public final boolean intersected(Segment s, Segment s2) {
        if (s.ishoriz && s2.ishoriz) {
            //parallel
            return false;
        }

        if (s.isvert && s2.isvert) {
            //parallel

            return false;
        }

        if (s.isvert) {
            //x is constant see if the x is on the other line
            int x = s.x1;
            //add 2 for round-off error
            if (s2.x1 <= x + 2 && s2.x2 + 2 >= x) {
                //solve and check if y is on both segments
                int y = (int) ((s.m * x) + s.b);
                if(s.y1<=y+2&&s.y2+2>=y)
                {
                    if(s2.y1<=y+2&&s2.y2+2>=y)
                    {
                return true;}
            }
            }
            return false;
        }

        if (s2.isvert) {
            //x is constant see if the x is on the other line
            int x = s2.x1;
            //add 2 for round-off error
            if (s.x1 <= x + 2 && s.x2 + 2 >= x) {
                //solve and check if y is on both segments
                int y = (int) ((s.m * x) + s.b);
                if(s.y1<=y+2&&s.y2+2>=y)
                {
                    if(s2.y1<=y+2&&s2.y2+2>=y)
                    {
                return true;}
            }
            }
            return false;
        }

        if (s.ishoriz) {
            //y is constant see if the y is on the other line
            int y = s.y1;
            //add 2 for round-off error
            if (s2.y1 <= y + 2 && s2.y2 + 2 >= y) {
                //solve and check if x is on both segments
                int x=(int) ((y-s.b)/s.m);
                if(s.x1<=x+2&&s.x2+2>=x)
                {
                    if(s2.x1<=x+2&&s2.x2+2>=x)
                return true;}
            return false;
        }}

        if (s2.ishoriz) {
            //y is constant see if the y is on the other line
            int y = s2.y1;
            //add 2 for round-off error
            if (s.y1 <= y + 2 && s.y2 + 2 >= y) {
                //solve and check if x is on both segments
                int x=(int) ((y-s.b)/s.m);
                if(s.x1<=x+2&&s.x2+2>=x)
                {
                    if(s2.x1<=x+2&&s2.x2+2>=x)
                return true;}
            }
            return false;
        }

        if (s.m == s2.m) {
            //parallel
            return false;
        }

        // use substitution
        // (s.m-s2.m)x=s2.b-s.b
        int x = (int) (s.m - s2.m);
        x = (int) ((s2.b - s.b) / x);
        // find y
        int y = (int) ((x * s.m) + s.b);
        //check if the values are in between for both lines
        //add 2 for round-off error
        if (s.y1 <= y + 2) {
            if (s.y2 + 2 >= y) {
                if (s2.y1 <= y + 2) {
                    if (s2.y2 + 2 >= y) {
                        if (s.x1 <= x + 2) {
                            if (s.x2 + 2 >= x) {
                                if (s2.x1 <= x + 2) {
                                    if (s2.x2 + 2 >= x) {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

    public final boolean intersects2(Segment s, Rect r) {
        //created lines of the rect
        Segment top = new Segment(r.left, r.top, r.right, r.top);
        Segment left = new Segment(r.left, r.top, r.left, r.bottom);
        Segment bottom = new Segment(r.left, r.bottom, r.right, r.bottom);
        Segment right = new Segment(r.right, r.top, r.right, r.bottom);
        boolean topp = s.intersected(s, top);
        if (topp) {
            return true;
        }
        boolean leftp = s.intersected(s, left);
        if (leftp) {
            return true;
        }
        boolean bottomp = s.intersected(s, bottom);
        if (bottomp) {
            return true;
        }
        boolean rightp = s.intersected(s, right);
        if (rightp) {
            return true;
        } else {
            return false;
        }
    }

}


那么结果是什么?当你尝试调试它时,你学到了什么? - Oliver Charlesworth
当我将成员变量声明为final时,如果我在初始化时对它们进行赋值,就会出现无法分配的错误,或者出现变量可能未被初始化的错误。 - jersam515
没错。这些几乎肯定是您应该解决的问题。 - Oliver Charlesworth
@Oli Charlesworth:这没有意义,你想让我将其声明为final,但仍然能够重新初始化这些变量?除非我漏掉了什么,否则这是不可能的。 - jersam515
没关系,我很高兴能够帮助。我已经将我的评论以答案的形式放在下面了。 - Oliver Charlesworth
显示剩余7条评论
1个回答

1

你可能没有正确初始化所有的成员变量。通常情况下,你应该尽可能地将许多成员变量声明为final,原因如下:

  • 编译器会强制要求在构造函数中对它们进行初始化。
  • 编译器会防止你在普通成员函数中意外地覆盖它们。

换句话说,你应该始终努力让编译器帮你发现错误!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接