我想弄清楚如何确定两条线是否彼此平行。我有一个 Point 类和一个 Segment 类。
到目前为止,我的坡度方法是在我的 Point 类中。
public double slopeTo (Point anotherPoint)
{
double totalSlope;
double slope1;
double slope2;
slope1 = (anotherPoint.y - this.y);
slope2 = (anotherPoint.x - this.x);
if(slope2 == 0)
{
slope2 = Double.POSITIVE_INFINITY;
return slope2;
}
else if (slope1 == 0)
{
slope1 = 0;
return slope1;
}
else
{
totalSlope = (slope1 / slope2);
return totalSlope;
}
}
我的并行方法是在我的 Segment 类中。
public boolean isParallelTo (Segment s1)
{
double pointSlope;
pointSlope = (point1.slopeTo (point2));
if (s1 .equals(pointSlope))
return true;
else
return false;
}
我的教授为我们提供了一个测试器,在测试器中,他创建了四个新点,其中两个用于一个片段,另外两个用于第二个片段。
s1 = new Segment(3,6,4,1); //<---(x1,y1,x2,y2)
s2 = new Segment(4,7,5,2);
originals1ToString = s1.toString();
originals2ToString = s2.toString();
System.out.println("\nTest6.1: Testing isParallelTo with " + s1 + " and " + s2);
System.out.print("expected: true \ngot: ");
boolean boolAns = s1.isParallelTo(s2);
System.out.println(boolAns);
当我运行测试器时,我得到一个错误,但它应该是正确的。所以我的并行方法是错误的。我知道这不可能是我的斜率方法,因为我已经反复测试过,一切都是正确的。
请帮助我。我将不胜感激。
最佳答案
if (s1 .equals(pointSlope))
return true;
else
return false;
您正在将 Segment 与 double 进行比较,并且它永远不会返回 true?
另一点是,坡度应该针对该线段,而不是针对组合的 2 条线段。您可以比较线段 1 和线段 2 的斜率,如果它们相等,则可以说线段相等。您需要更改slope 和parallelTo 方法。
关于java - (Java) 卡在方法上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35448920/