public bool Intersects(Line other) { Vector2f lineSegmentStart = new Vector2f(other.GetX1(), other.GetY1()); Vector2f lineSegmentEnd = new Vector2f(other.GetX2(), other.GetY2()); Vector2f circleCenter = new Vector2f(GetCenterX(), GetCenterY()); Vector2f closest; Vector2f segv = lineSegmentEnd.Cpy().Sub(lineSegmentStart); Vector2f ptv = circleCenter.Cpy().Sub(lineSegmentStart); float segvLength = segv.Len(); float projvl = ptv.Dot(segv) / segvLength; if (projvl < 0) { closest = lineSegmentStart; } else if (projvl > segvLength) { closest = lineSegmentEnd; } else { Vector2f projv = segv.Cpy().Scale(projvl / segvLength); closest = lineSegmentStart.Cpy().Add(projv); } bool intersects = circleCenter.Cpy().Sub(closest).LengthSquared() <= GetRadius() * GetRadius(); return(intersects); }
/// <summary> /// ��鵱ǰԲ���Ƿ����ָ��ֱ�� /// </summary> /// /// <param name="line"></param> /// <returns></returns> public bool Contains(Line line) { return Contains(line.GetX1(), line.GetY1()) && Contains(line.GetX2(), line.GetY2()); }
public bool Intersects(Line other) { Vector2f lineSegmentStart = new Vector2f(other.GetX1(), other.GetY1()); Vector2f lineSegmentEnd = new Vector2f(other.GetX2(), other.GetY2()); Vector2f circleCenter = new Vector2f(GetCenterX(), GetCenterY()); Vector2f closest; Vector2f segv = lineSegmentEnd.Cpy().Sub(lineSegmentStart); Vector2f ptv = circleCenter.Cpy().Sub(lineSegmentStart); float segvLength = segv.Len(); float projvl = ptv.Dot(segv) / segvLength; if (projvl < 0) { closest = lineSegmentStart; } else if (projvl > segvLength) { closest = lineSegmentEnd; } else { Vector2f projv = segv.Cpy().Scale(projvl / segvLength); closest = lineSegmentStart.Cpy().Add(projv); } bool intersects = circleCenter.Cpy().Sub(closest).LengthSquared() <= GetRadius() * GetRadius(); return intersects; }
/// <summary> /// 检查当前圆形是否包含指定直线 /// </summary> /// /// <param name="line"></param> /// <returns></returns> public bool Contains(Line line) { return(Contains(line.GetX1(), line.GetY1()) && Contains(line.GetX2(), line.GetY2())); }