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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T07:05:26+00:00 2026-05-17T07:05:26+00:00

Preferably without using any kind of loop, as this’ll be used in a game.

  • 0

Preferably without using any kind of loop, as this’ll be used in a game.

I wish to intersect a line with a rectangle, of arbitrary size.
But I also wish for the intersection point[s] to be returned.

It’s possible, I’ve done a little googling, but still have not worked it out.

The line is defined using (x1,y1,x2,y2).
The rectangle has these two points too.

  • 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-17T07:05:29+00:00Added an answer on May 17, 2026 at 7:05 am

    I would recommend simply doing a line-segment-line-segment intersection check on each line segment (edge) that makes up the rectangle. Here is a line segment intersection detection algorithm I wrote ages ago, dredged up from one of my old XNA projects:

    // a1 is line1 start, a2 is line1 end, b1 is line2 start, b2 is line2 end
    static bool Intersects(Vector2 a1, Vector2 a2, Vector2 b1, Vector2 b2, out Vector2 intersection)
    {
        intersection = Vector2.Zero;
    
        Vector2 b = a2 - a1;
        Vector2 d = b2 - b1;
        float bDotDPerp = b.X * d.Y - b.Y * d.X;
    
        // if b dot d == 0, it means the lines are parallel so have infinite intersection points
        if (bDotDPerp == 0)
            return false;
    
        Vector2 c = b1 - a1;
        float t = (c.X * d.Y - c.Y * d.X) / bDotDPerp;
        if (t < 0 || t > 1)
            return false;
    
        float u = (c.X * b.Y - c.Y * b.X) / bDotDPerp;
        if (u < 0 || u > 1)
            return false;
    
        intersection = a1 + t * b;
    
        return true;
    }
    

    I will leave inputting each edge into the above method and collecting the results as an exercise to the reader 🙂


    Edit 1 year later now I’ve gone to university and done a Graphics course:

    Take a look at the Cohen–Sutherland algorithm to do this efficiently when you have a large set of lines where most do not intersect the rectangle. It uses a 9 segment grid and you place each endpoint of the line in a region of said grid:

    grid

    Using this we can tell if there will not be any line intersections:

    grid with lines

    For example here CD will not intersect the rectangle (shown in red in the first image) as both C and D are in the top row and neither will AB. For the ones where the line may intersect the the rectangle we have to try the line-line intersections.

    They way the sections are numbered/labelled allows us to simply do x AND y != 0 (where x and y are the labels of the sections for each of the line’s endpoints) to determine if there will not be an intersection.

    Using this method means we have to many, many fewer line-line intersections which speeds up the whole thing massively.

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

Sidebar

Related Questions

I just want a quick way (and preferably not using a while loop)of createing
I'm preferably looking for a SQL query to accomplish this, but other options might
Are there any good (preferably free) tools out there? Can they give accurate estimates
What tool (preferably free) can be used with Visual C++ 2008 Express Edition to
How to calculate the length (in pixels) of a string in Java? Preferable without
Preferably free tools if possible. Also, the option of searching for multiple regular expressions
Preferably in VB.Net, but C# is fine, how can I access the extra properties
Is there a tool (preferably free) which will translate Oracle's PL/SQL stored procedure language
Is anyone aware of a script/class (preferably in PHP) that would parse a given
What is a good (and preferably simple) way to test the rendering performance of

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.