1

EDIT: THIS IS NOT A DUPLICATE, please read the description of the problem

Just to be clear, these line segments does not have an end point. They start at a point and goes to infinity based on a direction vector. I've found solutions for finite line segments, but they do not apply in this particular case (I think).

So, the title is basically my entire question - I've got

  1. point p1
  2. point p2
  3. direction vector n1 (a normalized vector)
  4. direction vector n2 (a normalized vector)

The first line segment starts at p1 and points towards n1

The second line segment starts at p2 and points towards n2

I need two answers:

  1. If they intersects and
  2. What is the point of intersection

I've found these two answers, but I'm so bad at math that I could not adapt them to fit my problem, but it could help you guys, I hope.

How do you detect where two line segments intersect?

and

Given two points and two vectors, find point of intersection

Thanks a lot!

Edit: (BTW, I'm working at 2D Space, so you don't have to worry about the z axis, thanks)

Community
  • 1
  • 1
Ramon Amorim
  • 111
  • 1
  • 1
  • 7
  • possible duplicate of [Given two points and two vectors, find point of intersection](http://stackoverflow.com/questions/14256525/given-two-points-and-two-vectors-find-point-of-intersection) - the answer there tells you exactly how to do it. – Oliver Charlesworth Dec 13 '14 at 13:08
  • You'll be able to tell _if_ they do intersect if their gradients are **not** equal. To tell _where_ they interest you need to solve `m1 * x + c1 = m2 * x + c2` where `m`s are the gradients of your lines and `c`s are the intersection with the `y` axis. You can then plug the `x` value back into `m1 * x + c1 = y` to get the `y` intersection point. – Rich Dec 13 '14 at 13:08
  • Guys, the raw equation of the line does not work here. I don't have an infinite line in both directions. Only in ONE direction. This is not a line. It's a line segment. Thanks. – Ramon Amorim Dec 13 '14 at 15:18
  • It does work, just check whether the solution is positive. – Oliver Charlesworth Dec 13 '14 at 18:14
  • It's not a line segment it's a ray. Take a look at this: http://stackoverflow.com/questions/2931573/determining-if-two-rays-intersect – jbeck Dec 13 '14 at 18:14
  • Thanks jbeck! It is working perfectly! It was a duplicate, after all.... – Ramon Amorim Dec 14 '14 at 21:05

1 Answers1

1

So, I've used the info provided by this post:

Determining if two rays intersect

And the info provided by a friend to solve this problem.

The first post stated that, given two points (p1 and p2) and two direction vectors (n1 and n2), the following formula applies:

bool DoesRaysIntersects(Point p1, Point p2, Point n1, Point n2)
{
    float u = (p1.y * n2.x + n2.y * p2.x - p2.y * n2.x - n2.y * p1.x) / (n1.x * n2.y - n1.y * n2.x);
    float v = (p1.x + n1.x * u - p2.x) / n.x;

    return u > 0 && v > 0;
}

if both u and v are greater than 0, it's because the two rays collide.

If they collide, we can use the equation of the line to provide the point of collision. I don't know if this is the best way to achieve it, but it worked for me:

First, we need the slope of each of the lines. With their slopes, we can calculate the y-intercept of each line. And with this two datas, we can calculate the point of collision:

Point GetPointOfIntersection(Point p1, Point p2, Point n1, Point n2)
{
    Point p1End = p1 + n1; // another point in line p1->n1
    Point p2End = p2 + n2; // another point in line p2->n2

    float m1 = (p1End.y - p1.y) / (p1End.x - p1.x); // slope of line p1->n1
    float m2 = (p2End.y - p2.y) / (p2End.x - p2.x); // slope of line p2->n2

    float b1 = p1.y - m1 * p1.x; // y-intercept of line p1->n1
    float b2 = p2.y - m2 * p2.x; // y-intercept of line p2->n2

    float px = (b2 - b1) / (m1 - m2); // collision x
    float py = m1 * px + b1; // collision y

    return new Point(px, py); // return statement
}

Thanks everyone!

Community
  • 1
  • 1
Ramon Amorim
  • 111
  • 1
  • 1
  • 7
  • 1
    it doesn't work with vertical and horizontal line, slope = 0 and slope = infinity – uray Jan 29 '18 at 04:31