问题
Possible Duplicate:
intersection of line and circle with different slope
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.
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
回答1:
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)
Now if
norm(A1,2) < r
then endpoint (c1,d1) is inside your circle, if
norm(A2,2) < r
then endpoint (c2,d2) is inside your circle.
If one of the points is inside the circle, then you only have one intersection point.
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)
If both points are inside the circle, then your lincirc function is lying to you.
来源:https://stackoverflow.com/questions/11508932/line-and-circle-intersection