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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:20:33+00:00 2026-05-18T02:20:33+00:00

I’m building an Asteroids Game for a class assignement. To finish it I need

  • 0

I’m building an Asteroids Game for a class assignement. To finish it I need a
line-intersection algorithm/code. I found one that works, but i do not understand
the math behind it. How does this work?

point* inter( point p1, point p2, point p3, point p4)
{
point* r;

//p1-p2 is the first edge. 
//p3-p4 is the second edge.
r = new point;
float x1 = p1.x, x2 = p2.x, x3 = p3.x, x4 = p4.x;
float y1 = p1.y, y2 = p2.y, y3 = p3.y, y4 = p4.y;

//I do not understand what this d represents.
float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
// If d is zero, there is no intersection
if (d == 0) return NULL;

// I do not understand what this pre and pos means and
// how it's used to get the x and y of the intersection
float pre = (x1*y2 - y1*x2), pos = (x3*y4 - y3*x4);
float x = ( pre * (x3 - x4) - (x1 - x2) * pos ) / d;
float y = ( pre * (y3 - y4) - (y1 - y2) * pos ) / d;

// Check if the x and y coordinates are within both lines
if ( x < min(x1, x2) || x > max(x1, x2) ||
        x < min(x3, x4) || x > max(x3, x4) ) return NULL;
if ( y < min(y1, y2) || y > max(y1, y2) ||
        y < min(y3, y4) || y > max(y3, y4) ) return NULL;

cout << "Inter X : " << x << endl;
cout << "Inter Y : " << y << endl;

// Return the point of intersection
r->x = x;
r->y = y;
return r; 
}
  • 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-18T02:20:33+00:00Added an answer on May 18, 2026 at 2:20 am
    point* inter( point p1, point p2, point p3, point p4)
    {
        point* r;
    
        //p1-p2 is the first edge. 
        //p3-p4 is the second edge.
        r = new point;
    

    As others have already commented, the above can/will leak memory. Better use a smart pointer, e.g. std::auto_ptr.

        float x1 = p1.x, x2 = p2.x, x3 = p3.x, x4 = p4.x;
        float y1 = p1.y, y2 = p2.y, y3 = p3.y, y4 = p4.y;
    
        //I do not understand what this d represents.
        float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
    

    First you need to know about dot product of two vectors.

    Let X and Y be vectors of length 1 in the directions of x-axis and y-axis, respectively. Then define X*X = 1, Y*Y = 1, and X*Y = 0, which is a kind of multiplication (or just an operation) yielding the length of the projection of one vector onto the other, times the other’s length (or in other words, the product of the lengths of the vectors times cosine of angle between them). Then if two vectors a and b are a = a.x*X + a.y*Y and b = b.x*X + b.y*Y, then a*b = a.x*b.x + a.y*b.y, because those terms with X*Y are zero, dropping out.

    And amazingly that generalization yields the lengths of the vectors times cosine of angle between them for vectors in arbitrary directions, which means for parallel vectors, the product of their lengths, and for perpendicular vectors, 0. 🙂

    Let edge1 = p1-p2, and edge2 = p3-p4. Then the above computes edge1.x*edge2.y – edge1.y*edge2.x = [edge1.x, edge1.y]*[edge2.y, -edge2.x]. The latter vector is edge2 rotated 90 degrees. Now if that rotated edge2 is perpendicular to edge1 then edge1 and edge2 must be parallel and can’t intersect. And in that case this dot product is 0.

        // If d is zero, there is no intersection
        if (d == 0) return NULL;
    
        // I do not understand what this pre and pos means and
        // how it's used to get the x and y of the intersection
        float pre = (x1*y2 - y1*x2), pos = (x3*y4 - y3*x4);
        float x = ( pre * (x3 - x4) - (x1 - x2) * pos ) / d;
        float y = ( pre * (y3 - y4) - (y1 - y2) * pos ) / d;
    
    
        // Check if the x and y coordinates are within both lines
        if ( x < min(x1, x2) || x > max(x1, x2) ||
                x < min(x3, x4) || x > max(x3, x4) ) return NULL;
        if ( y < min(y1, y2) || y > max(y1, y2) ||
                y < min(y3, y4) || y > max(y3, y4) ) return NULL;
    
        cout << "Inter X : " << x << endl;
        cout << "Inter Y : " << y << endl;
    
        // Return the point of intersection
        r->x = x;
        r->y = y;
        return r; 
    }
    

    Oh dear. I don’t feel like analyzing that… But if it works, then it’s solving set of two equations in two unknowns to determine intersection point of the infinite lines through the edges, then checking whether that intersection point is within edges.

    Cheers & hth.,

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.