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

  • Home
  • SEARCH
  • 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 1104965
In Process

The Archive Base Latest Questions

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

How can I check if 2 segments intersect? I’ve the following data: Segment1 [

  • 0

How can I check if 2 segments intersect?

I’ve the following data:

Segment1 [ {x1,y1}, {x2,y2} ]
Segment2 [ {x1,y1}, {x2,y2} ] 

I need to write a small algorithm in Python to detect if the 2 lines are intersecting.

alt text

  • 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-17T01:33:27+00:00Added an answer on May 17, 2026 at 1:33 am

    The equation of a line is:

    f(x) = A*x + b = y
    

    For a segment, it is exactly the same, except that x is included on an interval I.

    If you have two segments, defined as follow:

    Segment1 = {(X1, Y1), (X2, Y2)}
    Segment2 = {(X3, Y3), (X4, Y4)}
    

    The abcisse Xa of the potential point of intersection (Xa,Ya) must be contained in both interval I1 and I2, defined as follow :

    I1 = [min(X1,X2), max(X1,X2)]
    I2 = [min(X3,X4), max(X3,X4)]
    

    And we could say that Xa is included into :

    Ia = [max( min(X1,X2), min(X3,X4) ),
          min( max(X1,X2), max(X3,X4) )]
    

    Now, we need to check that this interval Ia exists :

    if (max(X1,X2) < min(X3,X4)):
        return False  # There is no mutual abcisses
    

    So, we have two line formula, and a mutual interval. Your line formulas are:

    f1(x) = A1*x + b1 = y
    f2(x) = A2*x + b2 = y
    

    As we got two points by segment, we are able to determine A1, A2, b1 and b2:

    A1 = (Y1-Y2)/(X1-X2)  # Pay attention to not dividing by zero
    A2 = (Y3-Y4)/(X3-X4)  # Pay attention to not dividing by zero
    b1 = Y1-A1*X1 = Y2-A1*X2
    b2 = Y3-A2*X3 = Y4-A2*X4
    

    If the segments are parallel, then A1 == A2 :

    if (A1 == A2):
        return False  # Parallel segments
    

    A point (Xa,Ya) standing on both line must verify both formulas f1 and f2:

    Ya = A1 * Xa + b1
    Ya = A2 * Xa + b2
    A1 * Xa + b1 = A2 * Xa + b2
    Xa = (b2 - b1) / (A1 - A2)   # Once again, pay attention to not dividing by zero
    

    The last thing to do is check that Xa is included into Ia:

    if ( (Xa < max( min(X1,X2), min(X3,X4) )) or
         (Xa > min( max(X1,X2), max(X3,X4) )) ):
        return False  # intersection is out of bound
    else:
        return True
    

    In addition to this, you may check at startup that two of the four provided points are not equals to avoid all that testing.

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

Sidebar

Related Questions

I'm looking for an algorithm that can check if nested regex repeats are reducible.
I need an idea for an efficient index/search algorithm, and/or data structure, for determining
I can check if user is Domain Administrator by the following lines of code:
Can check out here alt text http://51hired.com/static/problem.bmp
I can check for iPhone with this code: (navigator.userAgent.match(/iPhone/i)) But I want to target
How I can check the date using a calender , what I want to
With this code I can check the caret position in a textarea in firefox:
I know that I can check whether the linux timestamp in $time is today
I have a check box list in that user can check or uncheck the
Just wondering is there any way I can check whether the url links to

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.