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 6221509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:08:47+00:00 2026-05-24T08:08:47+00:00

What would be an efficient algorithm for calculating the intersection point of a line

  • 0

What would be an efficient algorithm for calculating the intersection point of a line starting a (x,y), with an angle of θ, and a bounding rectangle with co-orindates (0,0 to w,h)? Assuming the line origin is within the box.

In basic ascii art (for a line starting at 2,6 and θ approx 9π/8):

 h---------------...h,w
 .               ...
 .               ... 
 .               ...
 7               ...
 6   x           ...
 5  /            ...
 4 /             ...
 3/              ...
 2               ...
/1               ...
 0 1 2 3 4 5 6 7 ... w

All variables, except θ, are integers.

  • 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-24T08:08:47+00:00Added an answer on May 24, 2026 at 8:08 am

    Assume we parametrize the line with a variable t, i.e. each point on the line can be written as

    [x(t),y(t)] = [x,y] + t * [dx,dy]
    

    with constants

    dx = cos(theta)
    dy = sin(theta)
    

    Then you can first calculate the cut through the vertical boundaries:

    if dx<0 then        // line going to the left -> intersect with x==0
      t1 = -x/dx
      x1 = 0
      y1 = y + t1*dy
    else if dx>0 then   // line going to the right -> intersect with x==w
      t1 = (w-x)/dx
      x1 = w
      y1 = y + t1*dy
    else                // line going straight up or down -> no intersection possible
      t1 = infinity
    end
    

    where t1 is the distance to the intersection point and [x1,y1] the point itself. Second you do the same for the upper and lower boundaries:

    if dy<0 then        // line going downwards -> intersect with y==0
      t2 = -y/dy
      x2 = x + t2*dx
      y2 = 0
    else if dy>0 then   // line going upwards -> intersect with y==h
      t2 = (h-y)/dy
      x2 = x + t2*dx
      y2 = h
    else
      t2 = infinity
    end
    

    Now you just have to select the point with less distance from your origin [x,y].

    if t1 < t2 then
      xb = x1
      yb = y1
    else
      xb = x2
      yb = y2
    end
    

    Note that this algorithm only works if the starting point is inside the bounding box, i.e. 0 <= x <= w and 0 <= y <= h.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if there's an efficient algorithm to find the greatest
I would like to know if there is an efficient algorithm S = F(v,G)
What's the most efficient algorithm to find the rectangle with the largest area which
What would be the efficient algorithm to find if two given binary trees are
How you would code an efficient algorithm which can return a social 'distance' between
How would I write an efficient algorithm to search for a subset array of
I'm trying to come up with an efficient algorithm that would work in JavaScript
How much less efficient would it be to store some fields as a BigDecimal
What would be the most efficient way of recording to a log (.txt) from
What would be the most efficient data type to store a UUID/GUID in databases

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.