Line class intersectionWith方法实现

问题描述 投票:-4回答:1

您好我需要在java中实现Line类的以下方法:

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

我担心我不熟悉所涉及的数学计算。有人可以帮助或让我到一个我能理解的地方吗?问候!

java line
1个回答
0
投票

我将假设您使用自己创建的线条和点,并使用2D。如果你的线由方程y = a * x + b定义,并且存储系数a和b,那么两条线之间的交点将是(x,y),使得y == a1 * x + b1 == a2 * x + b2

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

© www.soinside.com 2019 - 2024. All rights reserved.