计算圆与直线的交点,返回 0、1 或 2 个交点的 vector。
Description: Finds the intersection between a circle and a line. Returns a vector of either 0, 1, or 2 intersection points. P is intended to be Point
. Status: unit tested
-
函数签名(据实现)
- template
vector circleLine(P c, double r, P a, P b)
- c 为圆心,r 为半径;直线由点 a,b 定义。
- template
-
核心计算
- ab = b - a;投影点 p = a + ab * (c-a).dot(ab) / ab.dist2()(c 在直线上的垂足)。
- s = a.cross(b, c);h2 = r^2 - s^2 / ab.dist2()(弦心距平方)。
- 判定:
- h2 < 0:无交点,返回 {}。
- h2 == 0:相切,返回 {p}。
- h2 > 0:两交点,h = ab.unit() * sqrt(h2);返回 {p-h, p+h}。