如何在java中解决线性系统问题

时间:2018-04-22 14:14:44

标签: java

public Point intersects(Line line) {
    Line holder = new Line(slope, yintercept);
    double x;
    double y;
    if (Math.abs(slope - line.slope) < 0.0000000001) {
        return null;
// because can't be parallel and if they don't intercept it has to return null
    } else {
        //no clue what to do here
        y = (holder.slope*x+yintercept);
    }
    Point interception = new Point(x, y);
    return interception;

我不知道如何计算x等于提前感谢帮助

2 个答案:

答案 0 :(得分:0)

使用f(x)= m * x + c ...

试用此版本
class LineIntersectException extends Exception
{
    LineIntersectException(String message)
    {
        super(message);
    }
}

public static Point intersect(Line line1, Line line2)
{
    if(line1.m == line2.m)
    {
        throw new LineIntersectException("The two lines don't intersect");
    }

    double x = (line1.c - line2.c) / (line1.m - line2.m);
    double y = this.m * x + this.b;

    return new Point(x, y);
}

答案 1 :(得分:0)

当你有L1 = ax + cL2 = bx + d时(a和b是斜率,c和d是截距)。 在线与L1 = L2所以ax + c = bx + d相交的点处 ax - bx = d - c x = (d-c)/(a-b) y = a * ((d-c)/(a-b) + c。 如果我们想要找到y然后使用上面的x计算,我们有: P((d-c)/(a-b), (ad-bc)/(a-b)) , 因此交点是:

let lstLength = {
  'propA': 0,
  'propB': 0,
};

sum = 0
for (prop in lstLength) {
  sum += lstLength[prop]
}

if (sum === 0) {
  alert('empty list')
} else {
  alert('valid list')
}