2017-08-02 92 views
0

我在学校做一个项目,其中一个方法找到两条线的交点。它用一行的mx + b值代替其他方程的y值,然后求解x。我试图设置一个“扔”,如果两条线平行,它会抛出一个非法的ArgumentException。下面是我试图安装方法(从Line类):如何检查并测试两条线(线性方程)是否通过抛出非法行为非法argumentException

public Point(double x, double y) { 
     setLocation (x, y); 
    } 

没有人有任何建议,如何正确地做到这一点:

public Point findIntersect(Line otherLine) { 
     double slope2 = otherLine.getSlope(); 
     double yIntercept2 = otherLine.getIntercept(); 

     double newX = (intercept - yIntercept2)/(slope2 - slope); 
     double newY = slope * newX + intercept; 
     Point aPoint = new Point(newX, newY); 
     return aPoint; 
    } 

和方法就是BEING从属实?

+0

不要抛出异常。这应该保留用于特殊情况。我认为平行线不符合该标准。 – duffymo

+0

这个_same_行应该是什么回报?这里的问题是有三种类型的交叉点;没有相交(平行但是不同的线),单个点(非平行线)和整个线(线与它自己相交)。返回'Point'并不总是足够。 – user2478398

+0

我得抛出illegalArgumentException。赋值如下所示:•如果两行平行,findIntersect()将抛出IllegalArgumentException。 – Gerald

回答

0

检查线的斜率是否相等。这表明这些线是平行的。

你可以有一个类似的检查:

if(slope == slope2){ 
    throw new IllegalArgumentException("Lines are parallel."); 
} 
0

第一行后插入此,

if (this.slope == otherLine.getSlope()) { 
    throw new IllegalArgumentException("Parallel lines will never intersect."); 
}