Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android/Java如何在没有line2d的情况下检查矩形和线段是否相交_Java_Android_Intersection_Lines - Fatal编程技术网

Android/Java如何在没有line2d的情况下检查矩形和线段是否相交

Android/Java如何在没有line2d的情况下检查矩形和线段是否相交,java,android,intersection,lines,Java,Android,Intersection,Lines,在我的Android游戏中,我需要检查矩形和线段之间的交点。我不能使用,因为android不支持。我已经研究过类似的关于行的问题,并试图修改它们,但失败了。我也意识到这基本上是我想要的,但是我失败了。例如,这里是一个Line类的代码,其中包括我在交叉点的尝试。结果是一些非交点返回true,而一些交点返回false。编辑:奥利·查尔斯沃思帮了我的忙,这是谷歌的工作代码 package com.example.HelloAndroid; import android.graphics.Rec

在我的Android游戏中,我需要检查矩形和线段之间的交点。我不能使用,因为android不支持。我已经研究过类似的关于行的问题,并试图修改它们,但失败了。我也意识到这基本上是我想要的,但是我失败了。例如,这里是一个Line类的代码,其中包括我在交叉点的尝试。结果是一些非交点返回true,而一些交点返回false。编辑:奥利·查尔斯沃思帮了我的忙,这是谷歌的工作代码

   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;
        }
    }
package com.example.HelloAndroid;
导入android.graphics.Rect;
公共课部分{
int-x1;
int y1;
int-x2;
int y2;
双m;
双b;
布尔ishoriz;
布尔isvert;
公共段(整数x1s、整数y1s、整数x2s、整数y2s){
如果(x1s>x2s){
这是1.x1=x2s;
这是x.x2=x1s;
}否则{
这1.x1=x1s;
这是x2=x2s;
}
如果(y1s>y2s){
这1.y1=y2s;
这1.y2=y1s;
}否则{
这1.y1=y1s;
这1.y2=y2s;
}
int-ydif=y2s-y1s;
int xdif=x2s-x1s;
如果(ydif==0){
this.ishoriz=true;
这是m=0;
这个.b=x1s;
}else if(xdif==0){
this.isvert=true;
}否则{
这个.m=(双)ydif/xdif;
双r=(双)ydif/xdif;
这个.b=y1s-(r*x1s);
this.isvert=false;
this.ishoriz=false;
}
}
公共最终布尔相交(段s、段s2){
如果(s.ishoriz&&s2.ishoriz){
//平行的
返回false;
}
if(s.isvert&&s2.isvert){
//平行的
返回false;
}
如果(s.isvert){
//x是常数,看看x是否在另一条线上
int x=s.x1;
//为舍入误差添加2
如果(s2.x1=x){
//求解并检查y是否在两个线段上
int y=(int)((s.m*x)+s.b);
如果(s.y1=y)
{
如果(s2.y1=y)
{
返回true;}
}
}
返回false;
}
if(s2.isvert){
//x是常数,看看x是否在另一条线上
int x=s2.x1;
//为舍入误差添加2
如果(s.x1=x){
//求解并检查y是否在两个线段上
int y=(int)((s.m*x)+s.b);
如果(s.y1=y)
{
如果(s2.y1=y)
{
返回true;}
}
}
返回false;
}
如果(s.ishoriz){
//y是常数,看看y是否在另一条线上
int y=s.y1;
//为舍入误差添加2
如果(s2.y1=y){
//求解并检查x是否在两个线段上
int x=(int)((y-s.b)/s.m);
如果(s.x1=x)
{
如果(s2.x1=x)
返回true;}
返回false;
}}
如果(s2.ishoriz){
//y是常数,看看y是否在另一条线上
int y=s2.y1;
//为舍入误差添加2
如果(s.y1=y){
//求解并检查x是否在两个线段上
int x=(int)((y-s.b)/s.m);
如果(s.x1=x)
{
如果(s2.x1=x)
返回true;}
}
返回false;
}
如果(s.m==s2.m){
//平行的
返回false;
}
//使用替代
//(s.m-s2.m)x=s2.b-s.b
int x=(int)(s.m-s2.m);
x=(int)((s2.b-s.b)/x);
//找到y
int y=(int)((x*s.m)+s.b);
//检查两行的值是否介于两者之间
//为舍入误差添加2
如果(s.y1=y){
如果(s2.y1=y){
如果(s.x1=x){
如果(s2.x1=x){
返回true;
}
}
}
}
}
}
}
}
返回false;
}
公共最终布尔相交2(段s,矩形r){
//创建了矩形的行
段顶=新段(右左、右上、右右上);
左段=新段(右左、右上、右左、右下);
段底=新段(右左、右下、右下);
右段=新段(右、右上、右右右下);
布尔topp=s.相交(s,top);
如果(topp){
返回true;
}
布尔leftp=s.相交(s,左);
if(leftp){
返回true;
}
布尔bottomp=s.相交(s,底部);
if(bottomp){
返回true;
}
布尔rightp=s.相交(s,右);
if(rightp){
返回true;
}否则{
返回false;
}
}

}可能是您没有正确初始化所有成员变量。一般来说,您应该尽可能多地创建成员变量
final
,至少有两个原因:

  • 编译器将强制它们必须在构造函数中初始化
  • 编译器将防止您在普通成员函数中意外重写它们
换句话说,您应该始终让编译器为您发现bug

那么结果是什么呢?Wh