线与圆的交点 [英] line and circle intersection
问题描述
可能重复:
不同斜率的直线和圆的交点
我有由 pp=randi([-400 400],2,2)
然后 x=pp(:,1)
和 绘制的线y=pp(:,2)
.我有一个圆心 (a,b),半径为 r
I have line which plotted by pp=randi([-400 400],2,2)
then x=pp(:,1)
and y=pp(:,2)
. I have a circle with centre (a,b) with radius r
我想检查圆和直线的交点.
I want to check the intersection point of circle and the line.
我使用 polyfit
命令检查斜率和截距.然后我使用了 lincirc
命令,但问题是如果线只穿过一个点,那么另一个点也会显示出来.
I have used polyfit
command to check the slope and intercept. Then I used lincirc
command but the problem is if the line crosses only one point then the other point is also shown.
例如,如果线越过一侧并停在中间,它也会显示另一点,不会越过边界
For example, if the line crosses one side and stops in the middle, it shows the other point as well which will not cross the boundary
推荐答案
你有一个以 (a,b) 为中心的圆半径 r.你有一条线.如果您绘制了这些点,则必须将数据存储在 x 和 y 向量中,因此您将每个元素的第一个和最后一个元素作为 (x,y) 坐标.第一对形成线的起点,最后一对形成终点.将这些点称为 (c1,d1) 和 (c2,d2).假设你的 lincirc 函数告诉你直线和圆之间有 2 个交点,计算
You have a circle radius r centred at (a,b). You have a line. If you have plotted these points, you must have your data stored in x and y vectors, so you take the first and last of elements each as (x,y) coordinates. The first pair form the line start point and last pair the end point. Refer to these points as (c1,d1) and (c2,d2). Assuming that your lincirc function tells you there are 2 intersection points between line and circle, calculate
A1 = (c1-a,d1-b)
A2 = (c2-a,d2-b)
现在如果
norm(A1,2) < r
那么端点 (c1,d1) 在你的圈子内,如果
then endpoint (c1,d1) is inside your circle, if
norm(A2,2) < r
那么端点 (c2,d2) 在你的圈子内.
then endpoint (c2,d2) is inside your circle.
如果其中一个点在圆内,那么你只有一个交点.
If one of the points is inside the circle, then you only have one intersection point.
如果两个点都不在圆内,那么您知道您的线两次穿过圆(假设您的 lincirc 函数告诉您有 2 个点)
If neither point is inside the circle, then you know that your line crosses the circle twice (assuming that your lincirc function tells you there are 2 points)
如果两个点都在圆圈内,那么你的 lincirc 函数就是在骗你.
If both points are inside the circle, then your lincirc function is lying to you.
这篇关于线与圆的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!