# Circle-line Intersection Points

Hi. As you can see from the image i have a line and start/end points of that line. how can i find the 2 points where line intersects with the circle.

i've found a post on a website and a video tutorial but i can't implement it in Unity because i am noob. can someone help please? website post, video tutorial

**Answer** by Ferriss
·
Aug 16, 2019 at 08:47 PM

I have found an code from internet and converted into Unity C#. Big thanks to this website. It works just as i wanted. And here is the Unity C# version of the code:

```
public Vector3[] IntersectionPoint(Vector3 p1, Vector3 p2, Vector3 center, float radius)
{
Vector3 dp = new Vector3();
Vector3[] sect;
float a, b, c;
float bb4ac;
float mu1;
float mu2;
// get the distance between X and Z on the segment
dp.x = p2.x - p1.x;
dp.z = p2.z - p1.z;
// I don't get the math here
a = dp.x * dp.x + dp.z * dp.z;
b = 2 * (dp.x * (p1.x - center.x) + dp.z * (p1.z - center.z));
c = center.x* center.x + center.z * center.z;
c += p1.x * p1.x + p1.z * p1.z;
c -= 2 * (center.x * p1.x + center.z * p1.z);
c -= radius * radius;
bb4ac = b * b - 4 * a * c;
if(Mathf.Abs(a) < float.Epsilon || bb4ac < 0)
{
// line does not intersect
return new Vector3[] {Vector3.zero, Vector3.zero};
}
mu1 = (-b + Mathf.Sqrt(bb4ac)) / (2 * a);
mu2 = (-b - Mathf.Sqrt(bb4ac)) / (2 * a);
sect = new Vector3[2];
sect[0] = new Vector3(p1.x + mu1 * (p2.x - p1.x), 0, p1.z + mu1 * (p2.z - p1.z));
sect[1] = new Vector3(p1.x + mu2 * (p2.x - p1.x), 0, p1.z + mu2 * (p2.z - p1.z));
return sect;
}
```

**Answer** by Vega4Life
·
Aug 16, 2019 at 02:38 PM

Here is an answer from Hellium that is well put together that may help you.

https://answers.unity.com/questions/1657918/i-have-a-line-that-goes-through-a-circle-how-can-i.html

Thanks for your comment but actually I am the one who asked that question you linked. And how is that a answer to this question? Aren't they different problems? What i need is intersection points not other side of point. I am missing something because i am really bad at math? :/

**Answer** by jamesvhyde
·
Nov 08, 2019 at 01:33 AM

Why do math when Unity will do it for you? You can use CircleCollider2D and Physics2D.Raycast.

Put an object in your scene positioned at the circle center.

Add a Circle Collider 2D component. Set the radius value to your circle radius.

Uncheck the checkbox on the component so it doesn't interfere with the rest of the game.

Create a script component with this code in it:

`CircleCollider2D cc = GetComponent<CircleCollider2D>(); cc.enabled = true; Debug.DrawRay(A, B-A, Color.red); RaycastHit2D hit = Physics2D.Raycast(A, B-A,(B-A).magnitude); if (hit.collider) Debug.Break(); cc.enabled = false;`

Replace

`Debug.Break()`

with whatever you need to happen when there is an intersection.`hit.point`

will give you the intersection point near A. You can find the other point by doing another raycast, switching the role of A and B.You might need a Layer to put your center object on, so you can filter the collisions by layer.

Check the documentation for more details:

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.