0

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

Community
  • 1
  • 1
Justin Oommen
  • 13
  • 1
  • 7
  • So when you get 2 points why not simply check to see if the endpoints of your line are inside the circle? If so, you have only 1 intersection point, if not then you have 2? You have the line equation and the circle equation so you have all the information you require. – mathematician1975 Jul 16 '12 at 17:02
  • How will I check that?Can you please help – Justin Oommen Jul 16 '12 at 17:06

1 Answers1

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.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101