第 class 行 intersectionWith 方法实现

Line class intersectionWith method implemantion

你好,我需要在java中实现第class行的以下方法:

// Returns the intersection point if the lines intersect,
// and null otherwise.
public Point intersectionWith(Line other) { }

恐怕我不是一个会算数的农民。 有人可以帮助或推荐我到我能理解的地方吗? 问候!

我假设您使用的是您自己创建的二维线和点。如果您的线由方程 y = a*x + b 定义,并且您存储系数 ab,则两条线的交点为 (x,y) 使得 y == a1*x + b1 == a2*x + b2

您可以找到 x 为:x = (b2 - b1) / (a1 - a2) 注意如果a1 == a2,即两条直线平行,则无解。 然后,您可以计算 y = a1*x + b1