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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:09:12+00:00 2026-05-31T10:09:12+00:00

I have the following code to determine the intersection of two 2D lines. It’s

  • 0

I have the following code to determine the intersection of two 2D lines. It’s not working and I’m not sure why; I’ve been copying code from multiple sources without much change.

The lines extend infinitely from a given midpoint. The “vector()” referenced in the below code is a 2D vector of magnitude 1.0 that indicates the direction in which the line extends (the line also extends in the negative direction, it’s not a ray).

Earlier, I was using this method for determining the intersection: Intersection point of two lines (2 dimensions)

After that gave me wrong results, I went with the method on Wikipedia.

float x1 = line1.location().x();
float y1 = line1.location().y();
float x2 = x1 + line1.vector().x();
float y2 = y1 + line1.vector().y();

float x3 = line2.location().x();
float y3 = line2.location().y();
float x4 = x3 + line2.vector().x();
float y4 = y3 + line2.vector().y();

float d = ((x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4));
if(d == 0) // If parallel, defaults to the average location of the lines.
    return new Vector2f((x1 + x3) * 0.5f, (y1 + y3) * 0.5f);
else
{
    float a = (x1 * y2) - (y1 * x2);
    float b = (x3 * y4) - (y3 * x4);
    return new Vector2f(((a * (x3 - x4)) - ((x1 - x2) * b)) / d, 
                        ((a * (y3 - y4)) - ((y1 - y2) * b)) / d);
}

Two examples of how it is wrong (The orange dot is the returned point of intersection):q

Ex 1 Ex 2

It returns a point along the primary line, but it doesn’t return a point on the second. the same bugs happen in both methods, so I’m really not sure what I’m doing wrong.

How can I fix this?


EDIT: Actually, this code works fine; my visualization code had an error.

  • 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-31T10:09:13+00:00Added an answer on May 31, 2026 at 10:09 am

    As it turns out, the code was working; my visualization code had a bug in it. My bad.

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

Sidebar

Related Questions

All, I have the following PHP code to determine how many years apart two
I have the following code to determine the unix timestamp of midnight tomorrow. It
I would like to determine the number of bytes downloaded from the following working
If I have the following code class One: scope = 'first_scope' class Two: scope
All, I have the following code: $fetch = mysql_query(SELECT * FROM calendar_events where event_status='booked'
I have the following code to determine if a file has a string on
I have the following code, which does work: var dataSource = (from p in
I have written the following code where I am trying to determine whether a
I have the following code and it won't compile because the compiler cannot determine
I have the following code from Codeigniter index.php My understanding is that, If /

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.