Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 294737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:23:39+00:00 2026-05-12T06:23:39+00:00

I have a line from A to B and a circle positioned at C

  • 0

I have a line from A to B and a circle positioned at C with the radius R.

Image

What is a good algorithm to use to check whether the line intersects the circle? And at what coordinate along the circles edge it occurred?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:23:39+00:00Added an answer on May 12, 2026 at 6:23 am

    Taking

    1. E is the starting point of the ray,
    2. L is the end point of the ray,
    3. C is the center of sphere you’re testing against
    4. r is the radius of that sphere

    Compute:
    d = L – E ( Direction vector of ray, from start to end )
    f = E – C ( Vector from center sphere to ray start )

    Then the intersection is found by..
    Plugging:
    P = E + t * d
    This is a parametric equation:
    Px = Ex + tdx
    Py = Ey + tdy
    into
    (x – h)2 + (y – k)2 = r2
    (h,k) = center of circle.

    Note: We’ve simplified the problem to 2D here, the solution we get applies also in 3D

    to get:

    1. Expand
      x2 – 2xh + h2 + y2 – 2yk + k2 – r2 = 0
    2. Plug
      x = ex + tdx
      y = ey + tdy
      ( ex + tdx )2 – 2( ex + tdx )h + h2 +
      ( ey + tdy )2 – 2( ey + tdy )k + k2 – r2 = 0
    3. Explode
      ex2 + 2extdx + t2dx2 – 2exh – 2tdxh + h2 +
      ey2 + 2eytdy + t2dy2 – 2eyk – 2tdyk + k2 – r2 = 0
    4. Group
      t2( dx2 + dy2 ) +
      2t( exdx + eydy – dxh – dyk ) +
      ex2 + ey2 –
      2exh – 2eyk + h2 + k2 – r2 = 0
    5. Finally,
      t2( d · d ) + 2t( e · d – d · c ) + e · e – 2( e · c ) + c · c – r2 = 0
      Where d is the vector d and · is the dot product.
    6. And then,
      t2( d · d ) + 2t( d · ( e – c ) ) + ( e – c ) · ( e – c ) – r2 = 0
    7. Letting f = e – c
      t2( d · d ) + 2t( d · f ) + f · f – r2 = 0

    So we get:
    t2 * (d · d) + 2t*( f · d ) + ( f · f – r2 ) = 0

    So solving the quadratic equation:

    float a = d.Dot( d ) ;
    float b = 2*f.Dot( d ) ;
    float c = f.Dot( f ) - r*r ;
    
    float discriminant = b*b-4*a*c;
    if( discriminant < 0 )
    {
      // no intersection
    }
    else
    {
      // ray didn't totally miss sphere,
      // so there is a solution to
      // the equation.
      
      discriminant = sqrt( discriminant );
    
      // either solution may be on or off the ray so need to test both
      // t1 is always the smaller value, because BOTH discriminant and
      // a are nonnegative.
      float t1 = (-b - discriminant)/(2*a);
      float t2 = (-b + discriminant)/(2*a);
    
      // 3x HIT cases:
      //          -o->             --|-->  |            |  --|->
      // Impale(t1 hit,t2 hit), Poke(t1 hit,t2>1), ExitWound(t1<0, t2 hit), 
    
      // 3x MISS cases:
      //       ->  o                     o ->              | -> |
      // FallShort (t1>1,t2>1), Past (t1<0,t2<0), CompletelyInside(t1<0, t2>1)
      
      if( t1 >= 0 && t1 <= 1 )
      {
        // t1 is the intersection, and it's closer than t2
        // (since t1 uses -b - discriminant)
        // Impale, Poke
        return true ;
      }
    
      // here t1 didn't intersect so we are either started
      // inside the sphere or completely past it
      if( t2 >= 0 && t2 <= 1 )
      {
        // ExitWound
        return true ;
      }
      
      // no intn: FallShort, Past, CompletelyInside
      return false ;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 264k
  • Answers 264k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Unfortunately I didn't have an IIS 7 installation to test… May 13, 2026 at 12:17 pm
  • Editorial Team
    Editorial Team added an answer According to the API specification, a servlet container is not… May 13, 2026 at 12:17 pm
  • Editorial Team
    Editorial Team added an answer I would say, in this case caller should be responsible… May 13, 2026 at 12:17 pm

Related Questions

Running sql server 2005 I have database A. I am trying to restore from
Summary: How do I expand a property with value download\${bulidmode}\project\setup.msi to download\Debug\project\setup.msi if the
I have to make a rudimentary FSM in a class, and am writing it
I have two models, A and B, and one light, L. I would like

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.